Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My goal is to compare the first field of each line whose first field is "2000" till I find a different field.

Let say the different field is located in line number 6, as in the provided example; in that case I'd ask Bash to print line number 5, which is the line before the line "No Living Leaves":

  2000 255    17.    11.     0.    30.  0.05  97. 0.110  6.7  6.7  596. 0.03 0.27  0  0
  2000 256    17.    11.     0.    30.  0.05  97. 0.109  6.7  6.7  596. 0.03 0.22  0  0
  2000 257    17.    11.     0.    30.  0.05  97. 0.109  6.7  6.7  596. 0.02 0.20  0  0
  2000 258    17.    11.     0.    30.  0.05  97. 0.109  6.7  6.7  596. 0.02 0.16  0  0
  2000 259    17.    11.     0.    30.  0.05  97. 0.109  6.6  6.6  596. 0.02 0.18  0  0
  No Living leaves
share|improve this question
    
So you said you want to compare first fields of the file . . .How many fields ? First 3 ? In your example it's up to 13th field the data is the same – Serg Jan 7 at 10:12
1  
Now you lost me, could you simply tell what the script should to froma a to b? – Jacob Vlijm Jan 7 at 10:13
1  
So is this just about printing line 6? Or is this about printing the first line whose first field is not 2000? – kos Jan 7 at 10:16
    
@Serg: each line contains 16 numbers. all lines starts with "2000". I want to compare this "2000" in each line till I find odd value (text or another number). In my case, I compare the "2000" till I reach a text which is "No Living Leaves" – Omar Ali Jan 7 at 10:48
    
@kos : each line contains 16 numbers. all lines starts with "2000". I want to compare this "2000" in each line till I find odd value (text or another number). In my case, I compare the "2000" till I reach a text which is "No Living Leaves" – Omar Ali Jan 7 at 10:49
up vote 5 down vote accepted

From the comments bellow kos's answer it is apparent that you want to print last line before one that differs. AWK approach would be the following.

$ awk ' $1==2000{line=$0} $1!=2000{exit}END{print line} ' OmarAli.txt                                                            
  2000 259    17.    11.     0.    30.  0.05  97. 0.109  6.6  6.6  596. 0.02 0.18  0  0

bash way would be like so:

$ while read LINE; do if [ "${LINE:0:4}" != "2000"  ];then print $PREV; break; fi; PREV=$LINE; done  < OmarAli.txt               
2000 259 17. 11. 0. 30. 0.05 97. 0.109 6.6 6.6 596. 0.02 0.18 0 0
share|improve this answer
    
@ Serg : the lines are in a txt file. I am still new and I wonder how to put the file name in the command. Moreover, the number of lines are changing from file to another and i need to read all the files with a loop as well – Omar Ali Jan 7 at 9:49
    
@OmarAli ok, the way you have it in the example in your question is right - you can feed in the file to while loop at the end. But what exactly do you need to do with those lines ? What kind of processing ? I understand you want to put them into variables, but for what purpose ? Also, can you provide example ? All of these things would be nice to add to your question ( there's edit button ). And example of the text that you have would be nice,too, so we can work with actual data – Serg Jan 7 at 9:52
    
your method works and I could print the file lines. My question is edited and an example is added – Omar Ali Jan 7 at 10:04
    
See my edit please, let me know if this helps – Serg Jan 7 at 10:38
1  
@Serg : it works and it gave me an into to go through awk thegeekstuff.com/2010/01/… – Omar Ali Jan 7 at 11:24

Using Perl:

perl -lane 'if($F[0]!=2000){print $x;exit}else{$x=$_}' in

To store the result into a variable:

var=$(perl -lane 'if($F[0]!=2000){print $x;exit}else{$x=$_}' in)
  • if($F[0]!=2000){print $x;exit}: if the first field is not 2000 prints the content of $x and exits;
  • else{$x=$_}: if the first field is 2000 assigns the current record to $x.

This will print nothing if the first record's first field is not 2000 and the last record whose first field is 2000 otherwise.

% cat in
2000 255    17.    11.     0.    30.  0.05  97. 0.110  6.7  6.7  596. 0.03 0.27  0  0
2000 256    17.    11.     0.    30.  0.05  97. 0.109  6.7  6.7  596. 0.03 0.22  0  0
2000 257    17.    11.     0.    30.  0.05  97. 0.109  6.7  6.7  596. 0.02 0.20  0  0
2000 258    17.    11.     0.    30.  0.05  97. 0.109  6.7  6.7  596. 0.02 0.16  0  0
2000 259    17.    11.     0.    30.  0.05  97. 0.109  6.6  6.6  596. 0.02 0.18  0  0
No Living leaves
% perl -lane 'if($F[0]!=2000){print $x;exit}else{$x=$_}' in
2000 259    17.    11.     0.    30.  0.05  97. 0.109  6.6  6.6  596. 0.02 0.18  0  0
% var=$(perl -lane 'if($F[0]!=2000){print $x;exit}else{$x=$_}' in)
% echo $var
2000 259    17.    11.     0.    30.  0.05  97. 0.109  6.6  6.6  596. 0.02 0.18  0  0
share|improve this answer
    
See edits on the question . . .Apparently it's actually awk-able question, rather than bash-able :) – Serg Jan 7 at 10:09
    
@Serg Thanks for the heads up – kos Jan 7 at 10:15
    
I am trying to apply the code you provided. However, I need to save the last line containing "2000" not the line containing "No Living Leaves" – Omar Ali Jan 7 at 10:54
    
@OmarAli See the updated answer. – kos Jan 7 at 11:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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