INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Problem passing shell variable to awk

Problem passing shell variable to awk

(OP)
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?


RE: Problem passing shell variable to awk

Hi

Works for me.

Both gawk and mawk 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.
http://feherke.github.com/

RE: Problem passing shell variable to awk

(OP)
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!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close