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


AWK Guideance - Putting it together

AWK Guideance - Putting it together

AWK Guideance - Putting it together

I am a new member here and I am still also fairly new to awk. I am trying to learn and get better but I have a hard time putting everything I want together.

The purpose of the script is to take input date, process it to a standard, and output a new file.

I would like to be able to execute with the following:

# awk -f -v in=INPUTFILE -v out=OUTPUTFILE script.awk

The input file will be setup:


I have the command sed 's/\([0-9]\{3\}\)\",\"/\",\"\1/' i can use to move the 3 digit number to the right, but I would like to replace it with awk and have been unsuccessful.

Z replaced with "first"
Y replaced with "second" <----------- {gsub("Y", "second"); print}
X replaced with "third"
W replaced with "forth"
K replaced with "fifth"

after those processes it would look like this.


How do I set the top row as their own set of columns separate of the columns below so that I can manipulate header placement as desired?

{printf $5 $1 $2 $3 $4 > out}
{printf $4 $3 $2 $5 $1 >> out} Row > 1 for the data

Row = 1 for headers
Row > 1 for data

final desired output

I can do these one line commands but am not good at putting it together in a functional loop.
This is where I need help, putting it together. I am also unsure of how to go about moving the first row of columns separately of all the other rows.

Thank you in advanced for any guidance I may receive.

RE: AWK Guideance - Putting it together

Hi kkings30,
You have to do only some substitutions and reorder the output columns.

For example, for this input file



this script


# Run: awk -f kkings30.awk kkings30.txt
  FS = ","

NR == 1 {
  sub("Z", "first")
  sub("Y", "second")
  sub("X", "third")
  sub("W", "fourth")
  sub("K", "fifth")
  printf("%s,%s,%s,%s,%s\n", $5, $1, $2, $3, $4)

NR > 1 {
  # remove fourth column (which is empty) 
  # divide third column into 2
  $0 = gensub(/\"(.{3})(.{3})\"/, "\"\\1\",\"\\2\"", "g", $0)
  printf("%s,%s,%s,%s,%s\n", $4, $3, $2, $5, $1)

creates this output:


$ awk -f kkings30.awk kkings30.txt

RE: AWK Guideance - Putting it together

Thank you mirom!

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!


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