×
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!
  • Students Click Here

*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.

Students Click Here

Jobs

Awk Script Using a Secondary Input File

Awk Script Using a Secondary Input File

Awk Script Using a Secondary Input File

(OP)
Hi,

I have an awk script that I've been using to parse some data from a file. The source data has some things that are inaccurate, so I'd like to replace certain strings with data from another file.

Here's what I originally was using for one section:

/^department:/{departmentnumber = index($2, "-") ? substr($2, 1, index($2, "-")-1) : $2 ; department = substr($2, 5); fulldepartmentname = $2}

This would parse out the ###-Some information. What I'd like to do now is get the department data from a separate file.

My first pass at it was:

/^department:/{departmentnumber = index($2, "-") ? substr($2, 1, index($2, "-")-1) : $2 ; department = system("grep ,departmentnumber$ /tmp/employee_departments.txt | awk -F"," '{print $1}'"); fulldepartmentname = "departmentnumber"-"department"}

But I don't think I've formatted it entirely correctly.

Sample input:

department: 999000-Should really be marketing

Sample output:
"999000","Marketing","999000-Marketing"

The employee_departments.txt has the data in this format:

Department_Name,Number

So it would look like this:

Marketing,999000

I'm executing the script with awk -f script.awk datafile

Any suggestions as to how to best proceed would be greatly appreciated.

Thanks!

RE: Awk Script Using a Secondary Input File

(OP)
Hi,

I've deployed a temporary hacky workaround of using a for loop with the employee_departments.txt file and using sed to replace all of the values, but I'm sure there is a much more efficient way to accomplish this.

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! Already a Member? Login

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