Merge lines in a file with Awk - incorrect output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge lines in a file with Awk - incorrect output
# 1  
Old 07-14-2009
Merge lines in a file with Awk - incorrect output

Hi,

I would like:

FastEthernet0/0 is up, line protocol is up
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
0 output errors, 0 collisions, 0 interface resets
Serial1/0:0 is up, line protocol is up
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort
0 output errors, 0 collisions, 0 interface resets

in a file to be output as:

FastEthernet0/0 is up, line protocol is up, 0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 output errors, 0 collisions, 0 interface resets
Serial10:0 is up, line protocol is up, 0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 output errors, 0 collisions, 0 interface resets


I've used the awk syntax: awk 'ORS=NR%3?",":"\n"' <INPUT-FILE> but my output is:

, 0 output errors, 0 collisions, 0 interface resetsred
, 0 output errors, 0 collisions, 0 interface resetsred, 0 abort


I read numerous posts regarding merging lines, but I haven't been able to understand why the awk syntax above doesn't work.


Any ideas?


Thanks,
Mario
# 2  
Old 07-14-2009
Code:
nawk 'ORS=(FNR%3)?", ":RS' myFile

# 3  
Old 07-14-2009
Hi, thanks but the system I am using does not have nawk installed.

The awk syntax I presented above worked fine on a Fedora server, but moving the script to CentOS, it no longer works.


I'd prefer to avoid system specific binaries and syntax so it is easily portable (perhaps this may be why I have run into this problem now...)


FYI, I get the same output...

awk 'ORS=(FNR%3)?", ":RS' test2
, 0 output errors, 0 collisions, 0 interface resetsred
, 0 output errors, 0 collisions, 0 interface resetsred, 0 abort


I am using the default bash shell. Could the shell I am using be a problem?
# 4  
Old 07-14-2009
no, the shell should not matter.
Could you post the exact content of your sample data file test2 - using the code tags.
# 5  
Old 07-14-2009
Hope I post this right..


cat <inout file> gives me:

FastEthernet0/0 is up, line protocol is up
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
0 output errors, 0 collisions, 0 interface resets
Serial1/0:0 is up, line protocol is up
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
0 output errors, 0 collisions, 0 interface resets

Needed output:

FastEthernet0/0 is up, line protocol is up, 0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 output errors, 0 collisions, 0 interface resets
Serial1/0:0 is up, line protocol is up, 0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 output errors, 0 collisions, 0 interface resets


Output I get running my and your syntax:

$awk 'ORS=NR%3?",":"\n"' test2

, 0 output errors, 0 collisions, 0 interface resetsred
, 0 output errors, 0 collisions, 0 interface resetsred, 0 abort


Entering the input file with vi seems to show that each original line is ended with an end of line correctly (one line doesn't follow onto the next.
# 6  
Old 07-14-2009
Code:
nawk 'NF{cnt++}ORS=(cnt%3)?", ":RS'

# 7  
Old 07-14-2009
Hmm.. I get exactly the same output as with the other commands.

Testing that I have the right syntax, I use: awk 'NF{cnt++}ORS=(cnt%1)?", ":RS' test2

This gives me the whole file input correctly (as if I would cat it).


Should it make a difference I am using awk and not nawk?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to add lines with symbol to output file

In the awk below which does execute I get output that is close, except for all the lines that start with a # are removed. Some lines have one others two or three and after the script adds the ID= to the fields below the pattern in the awk, I can not seem to add the # lines back to the output. ... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

E-mail Output Merge Lines for Some Text..

Hi all. This is the content of the text file used for the e-mail: TM ICP-EDW BILLING REGISTER USAGE BREAKDOWN_01062014.csv TM_ICP_EDWH_FICL_13062014.TXT TM_ICP_EDWH_FICL_16062014.TXT TM_ICP_EDW_Detailed Payment Journal Report_13062014.txt TM_ICP_EDW_Detailed Payment Journal... (9 Replies)
Discussion started by: aimy
9 Replies

3. Shell Programming and Scripting

Df -h | awk - output incorrect matching

Running solaris 9, on issuing the follwing command df -h | awk '$5 > 45 {print}' Filesystems with utilisation > 45% are being displayed as well as those between 5 and-9%!!! (3 Replies)
Discussion started by: squrcles
3 Replies

4. UNIX for Dummies Questions & Answers

ID incorrect field values in dat file and output to new file

Hi All I have a .dat file, the values are seperated by ". I wish to identify all field values in field 14 that are not '01-APR-2013' band then copy those records to a new file. Can anyone suggest the UNIX command required. Thanks in advance Andy (2 Replies)
Discussion started by: aurum1313
2 Replies

5. Shell Programming and Scripting

Find duplicates in column 1 and merge their lines (awk?)

Hi, I have a file (sorted by sort) with 8 tab delimited columns. The first column contains duplicated fields and I need to merge all these identical lines. My input file: comp100002 aaa bbb ccc ddd eee fff ggg comp100003 aba aba aba aba aba aba aba comp100003 fff fff fff fff fff fff fff... (5 Replies)
Discussion started by: falcox
5 Replies

6. Shell Programming and Scripting

Merge multiple lines in same file with common key using awk

I've been a Unix admin for nearly 30 years and never learned AWK. I've seen several similar posts here, but haven't been able to adapt the answers to my situation. AWK is so damn cryptic! ;) I have a single file with ~900 lines (CSV list). Each line starts with an ID, but with different stuff... (6 Replies)
Discussion started by: protosd
6 Replies

7. Shell Programming and Scripting

awk file comparison, x lines after matching as output

Hello, I couldn't find anything on the Forum that would help me to solve this problem. Could any body help me process below data using awk? I have got two files: file1: Worker1: Thomas Position: Manager Department: Sales Salary: $5,000 Worker2: Jason Position: ... (5 Replies)
Discussion started by: killerbee
5 Replies

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

9. Shell Programming and Scripting

awk : deleting specific incorrect lines

Hello friends, I searched in forums for similar threads but what I want is to have a single awk code to perform followings; I have a big log file going like this; ... 7450494 1724465 -47 003A98B710C0 7450492 1724461 -69 003A98B710C0 7450488 1724459 001DA1915B70 trafo_14:3 7450482... (5 Replies)
Discussion started by: enes71
5 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