Sponsored Content
Top Forums Shell Programming and Scripting AWK Merge Fields for Print Output Post 302141625 by RacerX on Saturday 20th of October 2007 07:18:36 PM
Old 10-20-2007
AWK Merge Fields for Print Output

I've got a file with each record on a separate line and each record contains 34 fields separated by a colon and i'm trying to re-arrange the order of the fields and merge together certain fields separated by a slash (like field7/field28). I tried using an awk print statement like
Code:
awk -F: 'BEGIN {OFS=":"}{ print $1, $2, $24, $23, $25, $17, $26, $4, $27, $5, $6, $7"/"$28, $8"/"$29, $9"/"$30, $10"/"$31, $11"/"$32, $33, $12, $13, $21, $14, $3, $34, $19, $22, $18, $20, $15, $16 }' ~/Desktop/fake_data2.txt > ~/Desktop/Output.txt

but if one or both of the fields are empty it will print the slash; which is not needed if they are both empty or if the second field is empty.

Being an AWK newbie i need help in writing the AWK statement to do what i meant it to do, not what i told it to do. Can i put an IF statement inside a print statement?

GIVEN INPUT:
Code:
---$1----:--$2-:-----$3----:-$4-:-$5-:--$6-:--$7-:-$8-:-$9-:-$10-:-$11-:-$12:-$13--:--$14--:$15:$16:-$17--:-$18:-$19-:$20:$21:$22:-------$23-----:24:-$25-:-$26:-$27:-----$28----:-$29-:-$30-:-$31-:$32:-$33:-$34

SEP 19-98:SSSSS:       $650:5555:2222:9    :4    :4   :2   :2    :2    :2000:   375:FAKETF :   :   :LAPS  :  2 :  76 :   :   :  1:      $1,550   :FT:NW1LC:0283:1284:            :     :     :3    :3  :    :AAAA
SEP 26-98:TTTTT:           :1111:3333:9    :6    :6   :6   :7    :6    :1000:   380:ROTHE  :   :   :LAPS  :  6 :  86 :   :   :  3:      $2,620   :FT:NW1LC:0300:1330:            :     :     :9Z   :7  :    :BBBB
SEP 28-98:UUUUU:           :7777:6666:7    :4    :3   :4   :5    :9    :2000:  4080:ROTHE  :   :   :LAPS  :  0 :  56 :   :   :  4:      $2,600   :FT:FNWTC:0284:1293:            :     :     :4    :5H :    :CCCC
OCT 12-98:VVVVV:     $3,000:4444:0000:5    :5    :5   :4   :4    :1    :3000:  2490:ROTHE  :   :   :LAPS  :  5 :  50 :   :   :  6:      $6,600   :FX:CCQ3Y:0282:1301:            :     :     :3H   :H  :    :DDDD
OCT 19-98:WWWWW:     $3,000:0101:1010:7    :7    :4   :1   :3    :1    :5000:   800:ROTHE  :   :   :LAPS  :  1 :  59 :   :   : 10:      $6,500   :FT:CCQ3Y:0273:1293:            :     :     :2    :XX :3333:EEEE
OCT 26-98:XXXXX:       $300:0011:0010:2    :5    :5   :3   :3    :5    :2000:   360:ROTHE  :   :   :LAPS  :  3 :  41 :   :   :  5:      $6,250   :FT:CCQ3Y:0291:1294:            :     :     :5H   :7T :    :FFFF
JAN  6-98:YYYYY:     $1,500:8888:3333:2    :4    :4   :2   :1    :1    :1000:   130:VIKING :   :   :LAPS  :  9 :  25 :   :   :  2:      $3,120   :ZZ:CHGNW:0292:1313:       5    :7    :H    :1H   :T  :9999:GGGG

EXPECTED OUTPUT:
Code:
---$1----:--$2-:24:-------$23-----:--$25:-$17--:-$26:-$4-:-$27:-$5-:--$6-:-$7/$28:---$8/$29---:--$9/$30-:$10/$31:$11/$32:-$33:-$12:-$13--:$21:--$14--:----$3--:-$34:-$19-:$22:-$18:$20:$15:$16
		
SEP 19-98:SSSSS:FT:      $1,550   :NW1LC:LAPS  :0283:5555:1284:2222:9    :4      :4           :2        :2/3    :2/3    :    :2000:   375:   :FAKETF :    $650:AAAA:  76 :  1:  2 :   :   :   
SEP 26-98:TTTTT:FT:      $2,620   :NW1LC:LAPS  :0300:1111:1330:3333:9    :6      :6           :6        :7/9Z   :6/7    :    :1000:   380:   :ROTHE  :        :BBBB:  86 :  3:  6 :   :   :   
SEP 28-98:UUUUU:FT:      $2,600   :FNWTC:LAPS  :0284:7777:1293:6666:7    :4      :3           :4        :5/4    :9/5H   :    :2000:  4080:   :ROTHE  :        :CCCC:  56 :  4:  0 :   :   :   
OCT 12-98:VVVVV:FX:      $6,600   :CCQ3Y:LAPS  :0282:4444:1301:0000:5    :5      :5           :4        :4/3H   :1/H    :    :3000:  2490:   :ROTHE  :  $3,000:DDDD:  50 :  6:  5 :   :   :   
OCT 19-98:WWWWW:FT:      $6,500   :CCQ3Y:LAPS  :0273:0101:1293:1010:7    :7      :4           :1        :3/2    :1/XX   :3333:5000:   800:   :ROTHE  :  $3,000:EEEE:  59 : 10:  1 :   :   :   
OCT 26-98:XXXXX:FT:      $6,250   :CCQ3Y:LAPS  :0291:0011:1294:0010:2    :5      :5           :3        :3/5H   :5/7T   :    :2000:   360:   :ROTHE  :    $300:FFFF:  41 :  5:  3 :   :   :   
JAN  6-98:YYYYY:ZZ:      $3,120   :CHGNW:LAPS  :0292:8888:1313:3333:2    :4/5    :4/7         :2/H      :1/1H   :1/T    :9999:1000:   130:   :VIKING :        :GGGG:  25 :  2:  9 :   :   :

WHAT I GOT WITH MY BAD CODE:
Code:
SEP 19-98:SSSSS:FT:      $1,550   :NW1LC:LAPS  :0283:5555:1284:2222:9    :4    /            :4   /     :2   /     :2    /3    :2    /3  :    :2000:   375:   :FAKETF :       $650:AAAA:  76 :  1:  2 :   :   :   
SEP 26-98:TTTTT:FT:      $2,620   :NW1LC:LAPS  :0300:1111:1330:3333:9    :6    /            :6   /     :6   /     :7    /9Z   :6    /7  :    :1000:   380:   :ROTHE  :           :BBBB:  86 :  3:  6 :   :   :   
SEP 28-98:UUUUU:FT:      $2,600   :FNWTC:LAPS  :0284:7777:1293:6666:7    :4    /            :3   /     :4   /     :5    /4    :9    /5H :    :2000:  4080:   :ROTHE  :           :CCCC:  56 :  4:  0 :   :   :   
OCT 12-98:VVVVV:FX:      $6,600   :CCQ3Y:LAPS  :0282:4444:1301:0000:5    :5    /            :5   /     :4   /     :4    /3H   :1    /H  :    :3000:  2490:   :ROTHE  :     $3,000:DDDD:  50 :  6:  5 :   :   :   
OCT 19-98:WWWWW:FT:      $6,500   :CCQ3Y:LAPS  :0273:0101:1293:1010:7    :7    /            :4   /     :1   /     :3    /2    :1    /XX :3333:5000:   800:   :ROTHE  :     $3,000:EEEE:  59 : 10:  1 :   :   :   
OCT 26-98:XXXXX:FT:      $6,250   :CCQ3Y:LAPS  :0291:0011:1294:0010:2    :5    /            :5   /     :3   /     :3    /5H   :5    /7T :    :2000:   360:   :ROTHE  :       $300:FFFF:  41 :  5:  3 :   :   :   
JAN  6-98:YYYYY:ZZ:      $3,120   :CHGNW:LAPS  :0292:8888:1313:3333:2    :4    /       5    :4   /7    :2   /H    :1    /1H   :1    /T  :9999:1000:   130:   :VIKING :     $1,500:GGGG:  25 :  2:  9 :   :   :

If anybody could give me some direction, i would appreciate the help!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: How to merge duplicate lines and print in a single

The input file: >cat module1 200611051053 95 200523457498 35 200617890187 57 200726098123 66 200645676712 71 200744556590 68 >cat module2 200645676712 ... (10 Replies)
Discussion started by: winter9
10 Replies

2. Shell Programming and Scripting

awk /nawk :: print the everything except the first and the last fields

format of file1 "file1.txt" 1 2 3 4 A B C XX YY ZZ AA WWW The output must contain except the first and last column the output must be 2 3 B YY ZZ AA (8 Replies)
Discussion started by: centurion_13
8 Replies

3. Shell Programming and Scripting

AWK: merge two files and replace some fields

Need some code tweak: awk 'END { for (i=1; i<=n; i++) if (f2]) print f2] } NR == FNR { f2 = $1] = $0 next } $1 in f2 { delete f2 }1' FS=, OFS=, 2.csv 1.csv > 3.csvfile 1.csv have: $1,$2,$3,$4,$5,$6,$7,$8,$9...... file 2.csv have: $1,$2,$3,$4,$5,$6 (2 Replies)
Discussion started by: u10
2 Replies

4. Shell Programming and Scripting

Print all the fields of record using awk

Hi, i want to generate print statement using awk. i have 20+ and 30+ fields in each line Now its priting only first eight fields print statement as output not all. my record is as shown below filename ... (2 Replies)
Discussion started by: raghavendra.nsn
2 Replies

5. Shell Programming and Scripting

awk - print all fields except for last field

How do I print all the fields of a record except for the $(NF) field? (4 Replies)
Discussion started by: locoroco
4 Replies

6. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

7. Shell Programming and Scripting

How to merge fields in a single file using awk ?

Hi, From a file, using: awk -F" " '{ if (NF == 6) print $1, $2, $3, $4, $5, $6; if (NF == 5) print $1, $2, $3, $4, $5; }' i printed out the required output. But i'm trying to merge the columns. Please look at the desired output. Any suggestions? Thanks Output: 00015 PSA1 ... (5 Replies)
Discussion started by: web2moha
5 Replies

8. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

9. UNIX for Dummies Questions & Answers

Merge two text files by two fields and mixed output

Hello, I'm back again looking for your precious help- This time I need to merge two text files with matching two fields, output only common records with mixed output. Let's look at the example: FILE1 56153;AAA0708;3;TEST1TEST1; 89014;BBB0708;3;TEST2TEST2; 89014;BBB0708;4;TEST3TEST3; ... (7 Replies)
Discussion started by: emare
7 Replies

10. Shell Programming and Scripting

awk print even fields of file

Hello: I want to print out the even number of fields plus the first column as row identifiers. input.txt ID X1 ID X2 ID X3 ID X4 A 700 A 1200 A 400 A 1300 B 2000 B 1000 B 2000 B 600 C 1400 C 200 C 1000 C 1200 D 1300 D 500 D 600 D 200and the output is: output.txt ID X1 X2 X3... (3 Replies)
Discussion started by: yifangt
3 Replies
All times are GMT -4. The time now is 08:57 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy