not sure how to


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting not sure how to
# 8  
Old 06-12-2007
Trying to understand below;

what is this do? NR==3{li=$0"|"li}


a=$(awk 'BEGIN{li=""}NR==1||NR==3{li=$0"|"li}END{print li}' "file")
# 9  
Old 06-12-2007
Quote:
Originally Posted by convenientstore
Trying to understand below;

what is this do? NR==3{li=$0"|"li}


a=$(awk 'BEGIN{li=""}NR==1||NR==3{li=$0"|"li}END{print li}' "file")
NR==1||NR==3{li=$0"|"li} If it is first or third line, then concatenate that row data with value in variable li and store it in variable li.
# 10  
Old 06-12-2007
so, basically, at the begining, put li value to null,

and if first or 3rd line, then... hmm I still dont' understand this part of
how {li=$0"|"li} equals concatenate that row(which is what's in 1st and 3rd line) with value in variable(which is null at this point? but I am assuming this becomes.. first line.. and then.. 3rd line ..

so value becomes 3rdline|1stline <-- separated by | ?

I thought $0 was the entire record.. how does assigning entire record to li becomes this meaning? can you elaborate little further please?



Originally Posted by convenientstore
Trying to understand below;

what is this do? NR==3{li=$0"|"li}


a=$(awk 'BEGIN{li=""}NR==1||NR==3{li=$0"|"li}END{print li}' "file")

NR==1||NR==3{li=$0"|"li} If it is first or third line, then concatenate that row data with value in variable li and store it in variable li.
# 11  
Old 06-12-2007
Quote:
Originally Posted by convenientstore
so, basically, at the begining, put li value to null,

and if first or 3rd line, then... hmm I still dont' understand this part of
how {li=$0"|"li} equals concatenate that row(which is what's in 1st and 3rd line) with value in variable(which is null at this point? but I am assuming this becomes.. first line.. and then.. 3rd line ..

so value becomes 3rdline|1stline <-- separated by | ?

I thought $0 was the entire record.. how does assigning entire record to li becomes this meaning? can you elaborate little further please?



Originally Posted by convenientstore
Trying to understand below;

what is this do? NR==3{li=$0"|"li}


a=$(awk 'BEGIN{li=""}NR==1||NR==3{li=$0"|"li}END{print li}' "file")

NR==1||NR==3{li=$0"|"li} If it is first or third line, then concatenate that row data with value in variable li and store it in variable li.
Quote:
so value becomes 3rdline|1stline <-- separated by | ?
You are correct.

$0 contain the current line read from the file.
Let us consider the following file
Code:
abc
def
ghi
jkl

For first line, li="abc" "|" ""
For the third line, li="ghi" "|" "abc|"
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question