Join lines using sed or awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Join lines using sed or awk
# 8  
Old 10-17-2016
Thank you, I have updated my answer to reflect this. Learning takes practice Smilie.
# 9  
Old 10-17-2016
I'm not sure that LINE1 and LINE2 are also checked.
The goal is just search PATTERN and concatenate the following 2 lines.

Either, another way in awk:
Code:
awk 'A==3{print X$0};/PATTERN/ {A=1;X=""};A>=1{++A;A%=4;X=X$0}' file

Regards.
This User Gave Thanks to disedorgue For This Post:
# 10  
Old 10-18-2016
Quote:
Originally Posted by greet_sed
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

If the OP meant as his requirement to test for those LINES2 too, then this would not work in case PATTERN is followed by LINE2.

to test for the right order, and to ensure the pattern and lines are there, one could do something like this:
Code:
sed -n '/PATTERN/{N;N;/\n.*LINE1.*\n.*LINE2/s/\n//gp;}' file

Still a little bit better would be:
Code:
sed -n '/PATTERN/{$!N;$!N;/LINE1.*\n.*LINE2/s/\n//gp;}' file

--
Quote:
Originally Posted by RavinderSingh13
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
Hi Ravinder, this one will not print the last pattern. You need an END section for that and you could replace /PATTERN/ || /LINE1/ || /LINE2/ with /PATTERN|LINE1|LINE2/ .

Also note that is not testing for all three lines to be present as it will still print lines if one or two lines are missing, or in the wrong order, or not adjacent.
This User Gave Thanks to Scrutinizer 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