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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Awk Script Using a Secondary Input File

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
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!
 
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!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top