Merging two lines into one (awk)


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Merging two lines into one (awk)
# 1  
Old 10-14-2019
Merging two lines into one (awk)

Hi,

I am attempting to merge the following lines which run over two lines using awk.

INITIAL OUTPUT

Code:
2019 Sep 28 10:47:24.695 hkaet9612 last message repeated 1 time
2019 Sep 28 10:47:24.695 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interfa
ce Ethernet1/45 is down (Interface removed)
2019 Sep 28 10:47:24.699 hkaet9612 last message repeated 1 time
2019 Sep 28 10:47:24.699 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interfa
2019 Sep 28 10:47:24.699 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interfa
ce Ethernet1/46 is down (Interface removed)
2019 Sep 28 10:47:24.702 hkaet9612 last message repeated 1 time

EXPECTED OUTPUT

Code:
2019 Sep 28 10:47:24.695 hkaet9612 last message repeated 1 time
2019 Sep 28 10:47:24.695 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interface Ethernet1/45 is down (Interface removed)
2019 Sep 28 10:47:24.699 hkaet9612 last message repeated 1 time
2019 Sep 28 10:47:24.699 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interfa
2019 Sep 28 10:47:24.699 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interface Ethernet1/46 is down (Interface removed)
2019 Sep 28 10:47:24.702 hkaet9612 last message repeated 1 time

Attempts:

Code:
$ awk 'length==80{ORS=$0?"":RS}1' unwrap.txt
2019 Sep 28 10:47:24.695 hkaet9612 last message repeated 1 time
2019 Sep 28 10:47:24.695 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interface Ethernet1/45 is down (Interface removed)2019 Sep 28 10:47:24.699 hkaet9612 last message repeated 1 time2019 Sep 28 10:47:24.699 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interface Ethernet1/46 is down (Interface removed)2019 Sep 28 10:47:24.702 hkaet9612 last message repeated 1 time

$ awk 'length==80{ORS=/$0/?"":RS}1' unwrap.txt
2019 Sep 28 10:47:24.695 hkaet9612 last message repeated 1 time
2019 Sep 28 10:47:24.695 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interfa
ce Ethernet1/45 is down (Interface removed)
2019 Sep 28 10:47:24.699 hkaet9612 last message repeated 1 time
2019 Sep 28 10:47:24.699 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interfa
ce Ethernet1/46 is down (Interface removed)
2019 Sep 28 10:47:24.702 hkaet9612 last message repeated 1 time

Can I please get some assistance around why this is not working?

Thanks.

Last edited by sand1234; 10-15-2019 at 09:34 AM..
# 2  
Old 10-14-2019
Hello sand1234,

Could you please try following.

Code:
awk '{printf("%s%s",$0~/^[0-9]+/ && FNR>1?ORS:FNR==1?"":OFS,$0)}'   Input_file

Output will be as follows.

Code:
2019 Sep 28 10:47:24.695 hkaet9612 last message repeated 1 time
2019 Sep 28 10:47:24.695 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interfa ce Ethernet1/45 is down (Interface removed)
2019 Sep 28 10:47:24.699 hkaet9612 last message repeated 1 time
2019 Sep 28 10:47:24.699 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interfa ce Ethernet1/46 is down (Interface removed)
2019 Sep 28 10:47:24.702 hkaet9612 last message repeated 1 time

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 10-14-2019
Hi RavinderSingh13,

The code works, although the last line does not have '\n'

Code:
root@localhost# awk '{printf("%s%s",$0~/^[0-9]+/ && FNR>1?ORS:FNR==1?"":OFS,$0)}' unwrap.txt
2019 Sep 28 10:47:24.695 hkaet9612 last message repeated 1 time
2019 Sep 28 10:47:24.695 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interface Ethernet1/45 is down (Interface removed)
2019 Sep 28 10:47:24.699 hkaet9612 last message repeated 1 time
2019 Sep 28 10:47:24.699 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interface Ethernet1/46 is down (Interface removed)
2019 Sep 28 10:47:24.702 hkaet9612 last message repeated 1 time root@localhost#

Can you please explain why my code didn't work, and the logic from your code?

Thanks.
# 4  
Old 10-14-2019
Hello sand1234,

When I ran code for your samples it worked fine for me, if still it is giving no new line at last edit it like:
Code:
awk '{printf("%s%s",$0~/^[0-9]+/ && FNR>1?ORS:FNR==1?"":OFS,$0)} END{print ""}'   Input_file

Explanation of code:
$0~/^[0-9]+/ && FNR>1 If line starts with digit and NOT 1st line then print ORS means a new line.
:FNR==1?"":OFS,$0 Else line is 1st line print nothing or else print space OFS.

Thanks,
R. Singh
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 10-15-2019
Try also (your code simplified):
Code:
awk 'length==80 {ORS=""} 1; {ORS=RS}' file

or
Code:
awk '{ORS=length==80?"":RS} 1' file

Your first attempt doesn't work as expected as ORS is never reset to RS, as you'll never enter the statement with an empty $0.
Your second will never set ORS to "" as /.../ denotes an RE string, and the string "$0" is not matched within the line $0.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 10-15-2019
Hi RavinderSingh13,

Thanks for the explanation and solution.

However in this case we need to match on lines which have 80 character length.

I changed your solution to the one below.

However, I just realized that although the code works, the logic is incorrect. If len(line)==80, add newline, else add "". This in fact is the opposite of what we are trying to achieve!

Code:
$ awk '{printf("%s%s",$0~length==80?ORS:"",$0)} END {print ""}' unwrap2.txt

2019 Sep 28 10:47:24.695 hkaet9612 last message repeated 1 time
2019 Sep 28 10:47:24.695 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interface Ethernet1/45 is down (Interface removed)
2019 Sep 28 10:47:24.699 hkaet9612 last message repeated 1 time
2019 Sep 28 10:47:24.699 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interfa
2019 Sep 28 10:47:24.699 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interface Ethernet1/46 is down (Interface removed)
2019 Sep 28 10:47:24.702 hkaet9612 last message repeated 1 time

Further to this point, the matching criteria does not match the 80 character lines we want, and instead matches everything.

Code:
$ awk '$0~length==80' unwrap2.txt
2019 Sep 28 10:47:24.695 hkaet9612 last message repeated 1 time
2019 Sep 28 10:47:24.695 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interfa
2019 Sep 28 10:47:24.699 hkaet9612 last message repeated 1 time
2019 Sep 28 10:47:24.699 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interfa
2019 Sep 28 10:47:24.699 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interfa
2019 Sep 28 10:47:24.702 hkaet9612 last message repeated 1 time

How can I modify the logic of my code to correctly match on 80 char lines and achieve the desired result?

Thanks.

--- Post updated at 12:42 PM ---

Hi RudiC,

Thanks for the explanation. Both of your solutions work well.

I've just updated the sample file, I would like the solution to cater for lines which are meant to be 80 char in length, and do not need to be condensed.

Interestingly enough, the following seems to work, although the logic doesn't make much sense (as explained in my previous post). Perhaps you can shed some light on this?

Code:
awk '{printf("%s%s",$0~length==80?ORS:"",$0)} END {print ""}' unwrap2.txt

2019 Sep 28 10:47:24.695 hkaet9612 last message repeated 1 time
2019 Sep 28 10:47:24.695 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interface Ethernet1/45 is down (Interface removed)
2019 Sep 28 10:47:24.699 hkaet9612 last message repeated 1 time
2019 Sep 28 10:47:24.699 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interfa
2019 Sep 28 10:47:24.699 hkaet9612 %ETHPORT-5-IF_DOWN_INTERFACE_REMOVED: Interface Ethernet1/46 is down (Interface removed)
2019 Sep 28 10:47:24.702 hkaet9612 last message repeated 1 time

Thanks.
# 7  
Old 10-15-2019
Quote:
Originally Posted by sand1234
Hi RavinderSingh13,
Thanks for the explanation and solution.
However in this case we need to match on lines which have 80 character length.
I changed your solution to the one below.
However, I just realized that although the code works, the logic is incorrect. If len(line)==80, add newline, else add "". This in fact is the opposite of what we are trying to achieve!
[CODE]$ awk '{printf("%s%s",$0~length==80?ORS:"",$0)} END {print ""}' unwrap2.txt
.................................................................................
Hello sand1234,

Could you please try following(not tested though).
Code:
awk '{printf("%s%s",length($0)==80?ORS:"",$0)} END {print ""}' unwrap2.txt

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

2. Shell Programming and Scripting

Merging 2 lines together

I have a small problem, which due to my lack of knowledge, has left me unable to decipher some of the solutions that I looked at on these forums. So below is a piece of text, which I ran via cat -vet, which comes from within a program file. I have many such programs to process and repeatable,... (4 Replies)
Discussion started by: skarnm
4 Replies

3. Shell Programming and Scripting

Merging lines

Thanks it worked for me. I have one more question on top of that. We had few records which were splitted in 2 lines instead of one. Now i identified those lines. The file is too big to open via vi and edit it. How can i do it without opening the file. Suppose, I want line number 1001 & 1002 to... (2 Replies)
Discussion started by: Gangadhar Reddy
2 Replies

4. Shell Programming and Scripting

merging two .txt files by alternating x lines from file 1 and y lines from file2

Hi everyone, I have two files (A and B) and want to combine them to one by always taking 10 rows from file A and subsequently 6 lines from file B. This process shall be repeated 40 times (file A = 400 lines; file B = 240 lines). Does anybody have an idea how to do that using perl, awk or sed?... (6 Replies)
Discussion started by: ink_LE
6 Replies

5. Shell Programming and Scripting

Merging lines

Hi folks. Could somebody help me write a script or command that will look through a file and for every line that doesn't contain a certain value, merge it with the one above? For example, the file contains: SCOTLAND|123|ABC|yes SCOTLAND|456|DEF|yes SCOTLAND|78 9|GHI|yes ... (3 Replies)
Discussion started by: MDM
3 Replies

6. UNIX for Dummies Questions & Answers

merging 2 lines with awk and stripping first two words

Hey all i am pretty new to awk... here my problem. My input is something like this: type: NSR client; name: pegasus; save set: /, /var, /part, /part/part2, /testpartition, /foo/bar,... (9 Replies)
Discussion started by: bazzed
9 Replies

7. Shell Programming and Scripting

Merging lines in a file

Hi, I want to merge the lines starting with a comma symbol with the previous line of the file. Input : cat file.txt name1,name2 ,name3,name4 emp1,emp2,emp3 ,emp4 ,emp5 user1,user2 ,user3 Output name1,name2,name3,name4 emp1,emp2,emp3,emp4,emp5 (9 Replies)
Discussion started by: mohan_tuty
9 Replies

8. Shell Programming and Scripting

Merging lines using AWK

Hi, Anybody help on this. :( I want to merge the line with previous line, if the line starts with 7. Otherwise No change in the line. Example file aa.txt is like below 122122 222222 333333 734834 702923 389898 790909 712345 999999 My output should be written in another file... (6 Replies)
Discussion started by: senthil_is
6 Replies

9. Shell Programming and Scripting

Merging files with AWK filtering and counting lines

Hi there, I have a couple of files I need to merge. I can do a simple merge by concatenating them into one larger file. But then I need to filter the file to get a desired result. The output looks like this: TRNH 0000000010941 ORDH OADR OADR ORDL ENDT 1116399 000000003... (2 Replies)
Discussion started by: Meert
2 Replies

10. UNIX for Dummies Questions & Answers

Merging lines into one

Hello. I would be very pleased if sb. help me to solve my problem. I've got a file with many non blank lines and I want to merge all lines into one not destroy the informations on them. I've tryed it with split and paste, tr, sed , but everything I've done has been wrong. I know about crazy... (8 Replies)
Discussion started by: Foxgard
8 Replies
Login or Register to Ask a Question