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

Help Splitting Field of Input File

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
Hi,

I'm writing an awk script to parse an input file. The input file has many fields about a user. There is one field I need to break up into pieces.

...
givenName: John
sn: Smith
st: CA
postalCode: 90210
departmentNumber: 999-IT Department
...

I am already calling the script with awk -F": ", so I need to find a way to print the single field 3 different ways.

I think I need to use split or sub, but I'm not sure what the best way to achieve it is.

Desired output is:

John,Smith,CA,90210,999,IT Department,999-IT Department

In my awk script I was able to extract the first part like this:

/departmentNumber/{departmentnumber = index($2, "-") ? substr($2, 1, index($2, "-")-1) : $2; departmentname = ??; fulldepartmentname = $2}

In that same line I need to set the other 2 variables.

Any help would be greatly appreciated.

Thanks,

John
 
Hi,

I was able to get this one-liner working when it wasn't with other code:

/^departmentNumber:/{FS="-"; gsub("departmentNumber: ", "") ; departmentnumber = $1; department = $2; fulldepartmentname = $1"-"$2}

But when I put it back in my awk script it breaks and doesn't print anything at all.

Does anyone have any suggestions as to how I can accomplish the desired output mentioned above?

Thanks,

John
 
I'm a little confused about what the code you posted is trying to achieve, but the following produces your desired output from your given input

Code:
/departmentNumber/{
  a = $2 " " $3
  j = index(a, "-")
  if (j) {
    print substr(a, 1, j-1) "," substr(a, j+1) "," a
  }
  next
}
{printf ("%s,",$2)}

CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top