Join lines using sed or awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Join lines using sed or awk
# 1  
Old 10-17-2016
Join lines using sed or awk

Hi,

I have text file that looks like this:

Code:
blabla
bla
PATTERN
LINE1
LINE2
bla
bla
bla
PATTERN
LINE1
LINE2
bla
PATTERN
LINE1
LINE2
bla
bla
...



There are exactly 2 lines following PATTERN.
I need output to be one line with PATTERN, LINE1 and LINE2 concatenated.
That is, output from input file above should be :

Code:
PATTERNLINE1LINE2
PATTERNLINE1LINE2
PATTERNLINE1LINE2

How can such 3 linies be joined using sed or awk ?

Regards,
Hench

Last edited by Scrutinizer; 10-17-2016 at 03:31 PM.. Reason: code tags
# 2  
Old 10-17-2016
Hi,
Don't believe to use code tag button for code or data samples.
Either, you can try this:
Code:
sed -ne '/PATTERN/{N;N;s/\
//g;p;}' file

Regards.

Last edited by disedorgue; 10-17-2016 at 03:39 PM.. Reason: Add -n flag to sed command
# 3  
Old 10-17-2016
Hello Hench,

Please use code tags for commands/codes/Inputs into your posts as per forum rules, could you please try following and let me know if this helps.
Code:
awk '/PATTERN/ && A{print A;A=""} /PATTERN/ || /LINE1/ || /LINE2/{A=A?A $0:$0}'  Input_file

Output will be as follows.
Code:
PATTERNLINE1LINE2
PATTERNLINE1LINE2

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 10-17-2016
Hi, try:
Code:
sed -n '/PATTERN/{N;N;s/\n//gp;}' file

# 5  
Old 10-17-2016
Maybe this awk, though I am still learning so may not be the best Smilie.

Code:
awk '/PATTERN/{if (x)print x;x="";}{x=(!x)?$0:x$0;}END{print x;}' file

output:
Code:
PATTERNLINE1LINE2
PATTERNLINE1LINE2
PATTERNLINE1LINE2

As @RavinderSingh13 found my script is not desired and also carries over the bla. Sorry for thatSmilie

Last edited by cmccabe; 10-17-2016 at 04:18 PM..
# 6  
Old 10-17-2016
IMO, above sed solutions will give wrong results if input has LINE3 in place of LINE1 or LINE2 for example.

Below one should work for given input patterns:
Code:
sed -ne '/PATTERN/ {N;/LINE1/N;/LINE2/s/\n//gp}' file


Last edited by greet_sed; 10-17-2016 at 03:52 PM.. Reason: Add text
# 7  
Old 10-17-2016
Quote:
Originally Posted by cmccabe
Maybe this awk, though I am still learning so may not be the best Smilie.
Code:
awk '/PATTERN/{if (x)print x;x="";}{x=(!x)?$0:x$0;}END{print x;}' file

output:
Code:
PATTERNLINE1LINE2
PATTERNLINE1LINE2
PATTERNLINE1LINE2

Hello cmccabe,

IMHO above code will not produce output as per OP's request. It will give blaalso in output.

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. UNIX for Beginners Questions & Answers

Sed/awk join lines once pattern found

Hi all OS - RHEL6.4 I have input file -f1.txt I need to search line which starts with \Start and read next line till it gets blank line and join them all. I need to trim any trailing spaces for each line.So output.txt should be.. \Start\now\fine stepwatch this space for toolsends... (7 Replies)
Discussion started by: krsnadasa
7 Replies

2. Shell Programming and Scripting

awk join lines based on keyword

Hello , I will need your help once again. I have the following file: cat file02.txt PATTERN XXX.YYY.ZZZ. 500 ROW01 aaa. 300 XS 14 ROW 45 29 AS XD.FD. PATTERN 500 ZZYN002 ROW gdf gsste ALT 267 fhhfe.ddgdg. PATTERN ERE.MAY. 280 PATTERRNTH 5000 rt.rt. ROW SO a 678 PATTERN... (2 Replies)
Discussion started by: alex2005
2 Replies

3. Shell Programming and Scripting

Join the line on delimiter using sed/awk in UNIX

I've input as , abcd| ef 123456| 78| 90 Desired output as, abcdef 1234567890 Anyone please give the solution. (5 Replies)
Discussion started by: jinixvimal
5 Replies

4. UNIX for Dummies Questions & Answers

sed, join lines that do not match pattern

Hello, Could someone help me with sed. I have searched for solution 5 days allready :wall:, but cant find. Unfortunately my "sed" knowledge not good enough to manage it. I have the text: 123, foo1, bar1, short text1, dat1e, stable_pattern 124, foo2, bar2, long text with few lines, date,... (4 Replies)
Discussion started by: petrasl
4 Replies

5. Shell Programming and Scripting

How to get awk to edit in place and join all lines in text file

Hi, I lack the utter fundamentals on how to craft an awk script. I have hundreds of text files that were mangled by .doc format so all the lines are broken up so I need to join all of the lines of text into a single line. Normally I use vim command "ggVGJ" to join all lines but with so many... (3 Replies)
Discussion started by: n00ti
3 Replies

6. Shell Programming and Scripting

How to use SED to join multiple lines?

Hi guys, anyone know how can i join multiples lines using sed till the end of a file and output to another file in a single line? The end of each line will be replaced with a special char "#". I am using the below SED command, however it seems to remove the last 2 lines. Also not all lines... (12 Replies)
Discussion started by: DrivesMeCrazy
12 Replies

7. Shell Programming and Scripting

Fixed Width Join & Pad Sed/Awk Help

I was wondering someone might be able to push me in the right direction, I am writing a script to modify fixed-width spool files, As you can see below the original spool file broke a single line into two for printability sake. I have had been able do the joins using sed, the thing I am... (10 Replies)
Discussion started by: Cho Nagurai
10 Replies

8. Shell Programming and Scripting

awk, join or sed

$ cat file1 a:23:43 A B C a:24:21 a:23:44 S D A F a:24:44 a:23:45 S D E a:24:45 $ cat file2 a:23:53 (1 Reply)
Discussion started by: jkl_jkl
1 Replies

9. Shell Programming and Scripting

to join the corresponding lines using shell commands or awk

Suppose u have this file gi_1 ABCDEFDHIJ KMNOPQRSTU VWXYZABCDE gi_2 JKLMNOPQRS TUVWXYZABC DEFGHIJKLM gi_3 PQRSTUVWXY ZABCDEFGHI JKLMNOPQRS gi_4 CDEFGHIJKL MNOPQRSTUV WXYZABCDEF gi_5 IJKLMNOPQR STUVWXYZAB CDEFGHIJKLM FGHIJKLMNO PQRSTUVWXY ZABCDEFABC NOPQRSTUVW XYZABCDEFG... (7 Replies)
Discussion started by: cdfd123
7 Replies

10. Shell Programming and Scripting

Awk Join multiple lines

Hi, I have data with broken lines: Sample data: "12"|"25"|"a"|"b"|"c"|"d"|"e"|"f"|"2453748"|"08:10:50" "16"|"25"|"a"|"b"|"c"|"d"|"e"|"f"|" 2453748"|"08:15:50" "16"|"25"|"a"|"b"|" c"|"d"|"e"|"f"|"2453748"|"08:19:50" "16"|"25"|"a"|"b"|"c"|"d"|"e"|"f"|"2453748"|"08:19:50" In the... (5 Replies)
Discussion started by: hitmansilentass
5 Replies
Login or Register to Ask a Question