Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A problem in my awk script. -condition : using variable in awk. - aim : print specific column started count variable in text

Tags:

awk

I want to print the whole line with specific column started count variable in text.

  • file.log format
1 101010 101010 4.0001 my home
2 101010 101010 5.0001 my home
3 101010 101010 6.0001 my home
4 101010 101010 7.0001 my home
  • my script
count=4
awk -v cnt="$count" '$4 ~ /^[cnt]\./' file.log
  • I want this result.
1 101010 101010 4.0001 my home
  • But my result is nothing....

Please tell me the wrong point of my script.

like image 774
ccjason Avatar asked Feb 24 '26 12:02

ccjason


1 Answers

You need NOT to use awk variable in a literal regexp, use int function here to convert 4th field as interger and then compare it directly with variable like:

count="4"
awk -v cnt="$count" 'int($4)==cnt' Input_file

Explanation: First passing shell variable count to cnt as an awk variable. Then using int function on 4th field to get only integer part eg--> from 4.0001 to 4 to make an exact comparison with cnt and once its equal it will print that line.

like image 65
RavinderSingh13 Avatar answered Feb 27 '26 01:02

RavinderSingh13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!