How to merge fields in a single file using awk ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to merge fields in a single file using awk ?
# 1  
Old 04-13-2015
How to merge fields in a single file using awk ?

Hi,

From a file, using:

Code:
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:
Code:
00015 PSA1                13     2     1  P
         PSA2                12     1     1  C
00016 PSA1                13     2     1  P
         PSA2                12     1     1  C
00017 PSA1                13     2     1  P
         PSA2                12     1     1  C
00018 PSA1                13     2     1  P
         PSA2                12     1     1  C
00019 PSA1                13     2     1  P
         PSA2                12     1     1  C
00020 sg1                 240     0     1  .
00021 sg1                 240     0     1  .
00022 sg1                 240     0     1  .
00023 sg1                 240     0     1  .
00024 sg1                 240     0     1  .

desired output:

Code:
00015 PSA1,PSA2                13,12     2,1     1,1  P,C
00016 PSA1,PSA2                13,12     2,1     1,1  P,C                     
00017 PSA1,PSA2                13,12     2,1     1,1  P,C
00018 PSA1,PSA2                13,12     2,1     1,1  P,C
00019 PSA1,PSA2                13,12     2,1     1,1  P,C
00020           sg1                   240        0       1     .
00021           sg1                   240        0       1     .
00022           sg1                   240        0       1     .
00023           sg1                   240        0       1     .
00024           sg1                   240        0       1     .


Last edited by web2moha; 04-13-2015 at 07:40 PM.. Reason: Add CODE tags.
# 2  
Old 04-13-2015
and what's your input?
Looks like you're not just merging columns - you're also merging rows as well....
# 3  
Old 04-13-2015
When i run a system specific command, i get the input which is attached. I add

Code:
<system command> | sed '1,2d' | sed '$d' | awk -F" " '{ if (NF == 6) print $1, $2, $3, $4, $5, $6; if (NF == 5) print $1, $2, $3, $4, $5; }'

to get the output.
# 4  
Old 04-13-2015
this isn't helping - I'll ask again. Can you provide a sample (made up if it's confidential) representative set of input data you're trying to "merge". And a desired matching output.
# 5  
Old 04-13-2015
I've made an assumption:
each record consists a 6 field row followed by an optional 5 field row


Code:
awk -F" " '
NF==6 {
   if(getline nl) {
     if(split(nl, v, " ") == 5) for(i=2;i<7;i++) $i = $i","v[i-1]
     else $0=$0"\n"nl
  }
}
1' infile

This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 04-13-2015
Thank you Chubler for the help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to merge the multiple data files as a single file?

Hi Experts, I have created multiple scripts and send the output to new file, getting this output to my mailbox on daily basis. I would like to send the all outputs to a single file, need to merge all file outputs on a single file. For example, Created script for df -h > df.doc grep... (7 Replies)
Discussion started by: seenuvasan1985
7 Replies

2. Shell Programming and Scripting

Perl script to Merge contents of 2 different excel files in a single excel file

All, I have an excel sheet Excel1.xls that has some entries. I have one more excel sheet Excel2.xls that has entries only in those cells which are blank in Excel1.xls These may be in different workbooks. They are totally independent made by 2 different users. I have placed them in a... (1 Reply)
Discussion started by: Anamika08
1 Replies

3. Shell Programming and Scripting

Merge the data from two servers into a single file

Hi All, Need your inputs for the below. I have 2 different servers 611 & 610, where i would be running two scripts. And would would be running one script from 611 at every 4 hours to merge the data from the 2 servers into 2 files and send a mail. so below is the code snippet for 611: ... (3 Replies)
Discussion started by: ss_ss
3 Replies

4. UNIX for Dummies Questions & Answers

file merge using awk

Hi All, I have 2 csv files. 1st file has 10 columns and the 2nd file has 12 columns. The requirement is, if the 4th column of file1 matches with the 4th column of file2, then append the 12th column of file2 with file1. Both files have equal number of lines and the 4th column values are... (1 Reply)
Discussion started by: ganesh_248
1 Replies

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

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

7. Shell Programming and Scripting

two file merge with awk

Help I read a file that has 2 fields. look for in a second file the first field and update it with the second field of first file. file1 1131518fat11416.txt ../newaod/2001/04/2001-04-00129233-1.txt file2 INSERT INTO tabric VALUES... (2 Replies)
Discussion started by: mcarlo65
2 Replies

8. Shell Programming and Scripting

please help to merge file with awk or sed

hi experts please help me,thanks in advance file1 arch : x86 install : pass make os : pass make build kernel : pass=100 failed=45 usb storage pass : The Linux Kernel Archives file2 arch : ppc install : failed make os : http://kernel.org (6 Replies)
Discussion started by: yanglei_fage
6 Replies

9. Shell Programming and Scripting

merge two files into one file use awk

Hi, guys. I have one question: I have two files: passwd and shadow (the number of records in these files are not equal)the contents of them are below: passwd: ************** ftp:x:24:24: sshd:x:71:65: uucp:x:10:14: brownj:x:5005:1000: sherrys: x :5006:1000: ... ************* ... (2 Replies)
Discussion started by: daikeyang
2 Replies

10. Shell Programming and Scripting

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 awk -F: 'BEGIN... (5 Replies)
Discussion started by: RacerX
5 Replies
Login or Register to Ask a Question