Contact US

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.

Students Click Here

Help Splitting Field of Input File

Help Splitting Field of Input File

Help Splitting Field of Input File


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.



RE: Help Splitting Field of Input File


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?



RE: Help Splitting Field of Input File

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

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


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