Merge multiple lines to one line when line starts with and ends with


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge multiple lines to one line when line starts with and ends with
# 1  
Old 11-29-2012
Merge multiple lines to one line when line starts with and ends with

example:

Code:
comment Now_TB.table column errac is for error messages
1 - first 
2 - second
3 -third ;

in this example I need to be able to grab the comment as first word and ; as the last word and it might span a few lines. I need it to be put all in one line without line breaks so I can grep for comment and pull the whole line out.

comment Now_TB.table column errac is for error messages 1 - first 2 - second 3 -third ;

also there will be a lot of these in the file and other items between these... but from comment to ; all of that goes in one line.

thank you

Last edited by Scrutinizer; 11-29-2012 at 04:49 PM.. Reason: code tags
# 2  
Old 11-29-2012
What have you tried so far? Where are you stuck?
# 3  
Old 11-29-2012
Code:
awk '/comment/ {F=1} F {P=P$0} !F; F&&/;/ {print P;F=0}'

sed '/^comment/,/;$/{/^[^0-9]/N;s/\n//;}'

these dont grab more than two lines and they dont work well with other items in the file. but they are close...

Last edited by Scott; 11-29-2012 at 03:59 PM.. Reason: Please use code tags
This User Gave Thanks to wambli For This Post:
# 4  
Old 11-29-2012
Code:
awk ' { if($0 ~ /^comment/ && NR!=1) printf "\n%s", $0; else printf "%s", $0; } END { printf "\n"; } ' infile

# 5  
Old 11-29-2012
Try:
Code:
awk '/^comment/{ORS=FS} /;/{ORS=RS}1' file

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

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Merge multiple lines into a single line

Hi all, I'm relatively new to scripting, I can do pretty basic things. I have a daily log file that looks like: timestamp=2017-06-28-01.01.35.080576; event status=0; userid=user1; authid=user1; application id=10.10.10.10.11111.12345678901; application name=GUI; ... (29 Replies)
Discussion started by: dwdnet
29 Replies

3. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

4. UNIX for Dummies Questions & Answers

How to merge every n lines into one line?

I want to merge every 16 lines into one line, how can I do that? My file looks like below: 0 . 2 2 . 0 0 . 0 0 . 0 0 . 0 0 0 0 0 (2 Replies)
Discussion started by: ml4me
2 Replies

5. Shell Programming and Scripting

Merge multiple lines in one line

Hi guys, So i have a input file with several sequences aligned (fasta) >NC_005930 241 bp MNMINIFIINNIFDQFIPVKLSIFSLTSVGSIIA LSWVWINTKTHWAISRSNTP-SLLLNSL WTLLITNL-NEKTNPWAPWLFSLFLLCFSFNIMSLI-PYTF-SQ TSHLSFTFGLSLPIWIMVNIAGFKNNWKKKISHLLPQGTPIYLVPVMII IETISLFIQPLTLGFRLGANLLAGHLLIFLCSCTIWE... (6 Replies)
Discussion started by: andreia
6 Replies

6. UNIX for Dummies Questions & Answers

To merge a few lines to 1 line

Hi Experts, This is my input file. input.txt 0 /dev/fd 25 /var 1 /tmp 1 /var/run 1. If this file has single line, then leave it, print the single line else merge the 4 lines above into 1 line as below e.g (6 Replies)
Discussion started by: streddy
6 Replies

7. UNIX for Advanced & Expert Users

Merge a group of lines into single line

Hi Everybody, Below are the contents of the a text file .., SN = 8 MSI = 405027002277133 IKVALUE = DE6AA6A11D42B69DF6398D44B17BC6F2 K4SNO = 2 CARDTYPE = SIM ALG = COMP128_3 SN = 8 MSI = 405027002546734 IKVALUE = 1D9F8BAA73973D8FBF8CBFB01436D822 K4SNO = 2 CARDTYPE = SIM ALG =... (8 Replies)
Discussion started by: prasanth_babu
8 Replies

8. Shell Programming and Scripting

script to merge multiple line

Dear all, I am new to this community. I need a script that will merge two lines of a text file based on some character. for example- Input asfdas fas sdfhksd sdf ss 45 gf# gjsdh fhjsd yiu oiuio ioioiuii sdgjdshsdg sdhfjk sdfhsd sdf sdf sdf wer wer we# qqwewq qwe qwe wer# fsdf sdf... (8 Replies)
Discussion started by: wildhorse
8 Replies

9. Shell Programming and Scripting

merge multi-lines into one line

Hi, Can anyone help me for merge the following multi-line logs( the black lines) which beginning with time: into one line. For the line with "-", it needs to be deleted. Please see the red color line. ######################################### time: 20080817073334 dn: uid=ok,ou=nbt... (3 Replies)
Discussion started by: missyou
3 Replies

10. Shell Programming and Scripting

Removing end of line to merge multiple lines

I'm sure this will be an easy question for you experts out there, but I have been searching the forum and working on this for a couple hours now and can't get it right. I have a very messy data file that I am trying to tidy up - one of the issues is some records are split into multiple lines: ... (4 Replies)
Discussion started by: tink
4 Replies
Login or Register to Ask a Question