Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem passing shell variable to awk 1

Status
Not open for further replies.

psyjung

Technical User
Aug 23, 2012
2
US
I am in the bash shell and have a file "data.txt" that includes names and data-points in rows, like this:

bill/bill_data/point1
bill/bill_data/point2
bill/bill_data/point3

frank/frank_data/point1
frank/frank_data/point2
frank/frank_data/point3

etc.

I want to write the data corresponding to each name in separate files. I can do this with grep, but I would like to know why this line does not work:

for i in bill frank; do awk -v name=$i '$0 ~ name' data.txt > data_${i}.txt; done

When I try this, I get an "unexpected newline or end of string" error directed at the end of '$0 ~ name'. I've tried everything I can think of but can't get it to work. Any ideas?


 
Hi

Works for me.

Both [tt]gawk[/tt] and [tt]mawk[/tt] creates 2 output files, data_bill.txt and data_frank.txt, each having only the 3 lines which contain the given name. No error message.

Feherke.
[link feherke.github.com/][/url]
 
Hi feherke. I've got it working now too, and figured out the problem. Instead of using "name" as the awk variable (as in the example I posted), I had been using "sub" (as shorthand for "research subjects"). I didn't realize that "sub" is an awk function, which is what caused my problem. Would have taken a lot longer for me to figure this out without posting here and getting your feedback. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top