I was trying to create a bash script to do this

I have a text file...

  • file output might not be in a proper structure..
  • Need to check whether that file contains a string, test.

    If yes, get the numerical value associated with that string....

    "test : 201"  

    compare the value with another reference number 100
    100 is a value which is a user input..

    if 201> 100  

    do this..
    do this..

How can I achieve that?

  • 3
    Not enough information to be clear. I can tell where you are getting the 201. It's coming from the file. However, I can't tell where the 100 is coming from. It may be coming from the file also. If you will give an example of items in the file, it would help. Use an example that will include both the 201 and the 100 that you are trying to use in the instance in your question. – L. D. James May 24 '18 at 2:10
  • Actually this .txt file is an output of a command..In that output there wiil be a string called test and its value- test :210 (test is not the beginning of a line).i wanted to compare that value whether its greater than a particular value that i provide- 100.. – GIG May 24 '18 at 5:12

I think you're looking for a shell script that can take two arguments - a filename and a reference number. Let's call the file F and the reference number R.

If file F contains a line of the form test : N where N is a number, you want to perform some action if N > R and an alternative action if N <= R.

If I have understood the problem correctly, this shell script should do the trick:

matching=$(grep "test" $1 | head -n 1 | cut -d ":" -f 2)                         
if [ -z $matching ]; then                                                        
  echo "no matching lines!" >&2                                                  
  trimmed=$(echo $matching | xargs)                                              
  if [ $trimmed -gt $2 ]; then                                                   
    echo "greater"                                                               
    echo "less than or equal to"                                                 

Suppose you save this in a file called myscript. Then you can run ./myscript input.txt 100 and if input.txt contains a line like test : 52 you'll see less than or equal to echoed to standard output.

Update: The asker lists an alternative scenario in the comments where there may be multiple lines matching test : n in a file. The largest value associated with test needs to be selected and compared with the reference. Just modify the first line in the script:

matching=$(grep "test" $1 | cut -d ":" -f 2 | sort -gr | head -n 1)

The subsequent lines are the same as before.

  • wow great.. it worked for me.. Thank you very much Kartik.. – GIG May 24 '18 at 14:16
  • HI,I have one more scenario.. If i can see more values of test in file..like test :200 and test :150.. i want to take the highest value and compare with the user input value..How can we modify the script.. – GIG May 25 '18 at 3:50
  • I'll update the answer to reflect this requirement – Kartik Sabharwal May 25 '18 at 4:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.