0

the following simple bash script print the row number ( from the file - list.txt )

function get_row

{ 

[[ $1 = 1 ]] &&  awk '{print $1}' list.txt
[[ $1 = 2 ]] &&  awk '{print $2}' list.txt

} 


get_row 1
get_row 2

but I want to wrote it more elegant...

is it possible to set "$1" as variable as awk '{print $val}'

so if I will call function as get_row 1 then $val will get the value 1

  • 1
    This doesn't print rows. It will print the 1st or 2nd column (field) of every line in the input file. Is that what you want? Or do you want the actual row (e.g. line 2) to be printed? – terdon Apr 24 '18 at 11:31
4

Do you want something like this?

function get_column
{ 
    awk -v val=$1 '{print $val}' list.txt
}

Above is returning the column match with $1 passing to the function. if you really need print the line match with line number in $1 from function, instead use below.

function get_row
{ 
    awk -v val=$1 'NR==val{print ; exit}' list.txt
}

Or let shell evaluated and set the val value and print that within awk as following:

function get_column
{ 
    awk '{print $val}' val=$1 list.txt
}
function get_row
{ 
    awk 'NR==val{print ; exit}' val=$1 list.txt
}

Here you are passing val with only numbers and if val was contain backslash escape character you will encounter a problem which awk does C escape sequence processing on values passed via -v val= and a shell variable with val="\\n" will change to value with \n by awk.

  • great , its so easy -:) – yael Apr 24 '18 at 11:33
3

You can use:

field=$1 awk '{print $ENVIRON["field"]}'

it works in all POSIX compliant system, and you don't have to worry about code injection when using -v val=

Also the function should be named get_column instead of get_row.

  • 1
    Ok, I read that it has injection issues when the val contains backslash character, but in OP's question s/he is only passing Numbers. so that will not encounter an issue here. – αғsнιη Apr 26 '18 at 3:35

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.