rapha
rapha

Reputation: 23

Print column using while loop in awk

I need to extract the values of the 2nd column from a file while the value from $1 = 2 until $1 = 3. As an example, from the file

1 | 2.158e+06
  | 2.31e+06 
  | 5.008e+06 
2 | 693000
  | 718000 
  | 725000
3 | 2.739e+06 
  | 2.852e+06 
  | 2.865e+06
  | 2.874e+06
4 | 4.033e+06
  | 4.052e+06
  | 4.059e+06

I would like to extract values of the 2nd column from $1=2 until $1=3

693000
718000 
725000

I tried using awk, but I have just figured out how to extract the values from $1=1 until $2=2

  awk -F "|" '{if ($1>1) exit; else print $2}' foo.txt

Output

2.158e+06
2.31e+06 
5.008e+06 

I also tried this

awk -F "|" '{i=2; do {print $2; i++} while ($4); if ($1>i) exit}' foo.txt

But it gives me the whole 2nd column

2.158e+06
2.31e+06 
5.008e+06 
693000
718000 
725000
2.739e+06 
2.852e+06 
2.865e+06
2.874e+06
4.033e+06

Does anyone know how to do this using awk or other command?

Thanks

Upvotes: 2

Views: 665

Answers (6)

dawg
dawg

Reputation: 103714

Just so you don't have to read the entire file, exit when you see a '3':

$ awk -F\| '/^2\s+/ {f=1} /^3\s+/ {exit} f {print $2+0}' file
693000
718000
725000

Upvotes: 1

Jotne
Jotne

Reputation: 41446

Here is another awk

awk -F\| '/^2$/ {f=1} /^3$/ {f=0} f {print $2+0}' file
693000
718000
725000

-F\| set field separator to | /^2/ if file start with 2, set flag f to true.
/^3/ if file start with 2, set flag f to false.
f {print $2+0}' if flag f is true, print filed 2.
$2+0 this is used to remove space in front of number. Remove it if it contains letters.

Upvotes: 2

repzero
repzero

Reputation: 8412

using getline statement in awk tactically

awk -v FS=" [|] "  '$1=="2"{print $2;getline;while(($1==" "||$1==2)){print $2;$0="";getline>0}}' my_file

Upvotes: 2

buydadip
buydadip

Reputation: 9407

This is what I came up with:

awk -F "|" '{if ($1==3) exit} /^2/,EOF {print $2}' file

1) /^2/,EOF {print $2} signifies print everything in second column up to the end of file, starting with a row that begins with a 2

2) {if ($1==3) exit} stops printing once the first column is a number 3

Output

693000
718000 
725000

Upvotes: 2

jas
jas

Reputation: 10865

A range pattern could work nicely here. The pattern $1==2,$1==3 will start executing the action when the first column is 2 and stop when it is 3. (Since the range is inclusive we need to check that the first column is not 3 before printing the second column in this case.)

$ awk -F\| '$1==2,$1==3 { if ($1 != 3) print $2 }' foo.txt
693000
718000 
725000

Upvotes: 2

Haifeng Zhang
Haifeng Zhang

Reputation: 31885

hzhang@dell-work ~ $ cat sample.csv 
1 | 2.158e+06
  | 2.31e+06 
  | 5.008e+06 
2 | 693000
  | 718000 
  | 725000
3 | 2.739e+06 
  | 2.852e+06 
  | 2.865e+06
  | 2.874e+06
4 | 4.033e+06
  | 4.052e+06
  | 4.059e+06
hzhang@dell-work ~ $ awk -F"|" 'BEGIN{c=0}{if($1>=3){c=0} if(c==1 ||($1>=2 && $1<3)){c = 1;print $2}}' sample.csv 
 693000
 718000 
 725000

I set a flag c. If $1 is not between 2 and 3, the flag set to 0, otherwise it is 1, which means we can print $2 out.

Upvotes: 2

Related Questions