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

AWK Guideance - Putting it together

Status
Not open for further replies.

kkings30

Technical User
Feb 3, 2018
2
US
Hello,
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:

# cat INPUTFILE
"Z","Y","X","W","K"
"AAA","BBB","111555","","A10"
"AAA","10D","A01555","","B10"

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.

"first","second","third","fourth","fifth"
"AAA","BBB","111","555","A10"
"AAA","10D","A01","555","B10"

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
#cat OUTPUT
"fifth","first","second","third","fourth"
"555","111","BBB","A10","AAA"
"555","A01","10D","B10","AAA"


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.
 
Hi kkings30,
You have to do only some substitutions and reorder the output columns.

For example, for this input file
kkings30.txt
Code:
"Z","Y","X","W","K"
"AAA","BBB","111555","","A10"
"AAA","10D","A01555","","B10"

this script
kkings30.awk
Code:
[COLOR=#0000ff]# Run: awk -f kkings30.awk kkings30.txt[/color]
[COLOR=#6a5acd]BEGIN[/color] {
  [COLOR=#6a5acd]FS[/color] = [COLOR=#ff00ff]","[/color]
}

[COLOR=#6a5acd]NR[/color] == [COLOR=#ff00ff]1[/color] {
  [COLOR=#008b8b]sub[/color]([COLOR=#ff00ff]"Z"[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]"first"[/color])
  [COLOR=#008b8b]sub[/color]([COLOR=#ff00ff]"Y"[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]"second"[/color])
  [COLOR=#008b8b]sub[/color]([COLOR=#ff00ff]"X"[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]"third"[/color])
  [COLOR=#008b8b]sub[/color]([COLOR=#ff00ff]"W"[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]"fourth"[/color])
  [COLOR=#008b8b]sub[/color]([COLOR=#ff00ff]"K"[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]"fifth"[/color])
  [COLOR=#a52a2a][b]printf[/b][/color]([COLOR=#ff00ff]"[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color][COLOR=#6a5acd],[/color] [COLOR=#6a5acd]$5[/color][COLOR=#6a5acd],[/color] [COLOR=#6a5acd]$1[/color][COLOR=#6a5acd],[/color] [COLOR=#6a5acd]$2[/color][COLOR=#6a5acd],[/color] [COLOR=#6a5acd]$3[/color][COLOR=#6a5acd],[/color] [COLOR=#6a5acd]$4[/color])
}

[COLOR=#6a5acd]NR[/color] > [COLOR=#ff00ff]1[/color] {
  [COLOR=#0000ff]# remove fourth column (which is empty) [/color]
  [COLOR=#008b8b]sub[/color]([COLOR=#ff00ff]/,"",/[/color][COLOR=#6a5acd],[/color][COLOR=#ff00ff]","[/color])
  [COLOR=#0000ff]# divide third column into 2[/color]
  [COLOR=#6a5acd]$0[/color] = [COLOR=#008b8b]gensub[/color]([COLOR=#ff00ff]/[/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff]([/color][COLOR=#6a5acd].{[/color][COLOR=#ff00ff]3[/color][COLOR=#6a5acd]}[/color][COLOR=#ff00ff])([/color][COLOR=#6a5acd].{[/color][COLOR=#ff00ff]3[/color][COLOR=#6a5acd]}[/color][COLOR=#ff00ff])[/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff]/[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\"\\[/color][COLOR=#ff00ff]1[/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]\"\\[/color][COLOR=#ff00ff]2[/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff]"[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]"g"[/color][COLOR=#6a5acd],[/color] [COLOR=#6a5acd]$0[/color])
  [COLOR=#a52a2a][b]printf[/b][/color]([COLOR=#ff00ff]"[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color][COLOR=#6a5acd],[/color] [COLOR=#6a5acd]$4[/color][COLOR=#6a5acd],[/color] [COLOR=#6a5acd]$3[/color][COLOR=#6a5acd],[/color] [COLOR=#6a5acd]$2[/color][COLOR=#6a5acd],[/color] [COLOR=#6a5acd]$5[/color][COLOR=#6a5acd],[/color] [COLOR=#6a5acd]$1[/color])
}

creates this output:
Code:
$ awk -f kkings30.awk kkings30.txt
"fifth","first","second","third","fourth"
"555","111","BBB","A10","AAA"
"555","A01","10D","B10","AAA"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top