Remove line breaks after a match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove line breaks after a match
# 8  
Old 06-11-2012
An ex approach:

Code:
printf '%s\n' a '' . 'g/TABLE/+1,/^$/-1j' '$d' x | ex -s file

Regards,
Alister
# 9  
Old 06-11-2012
It is working for me:
Code:
[root@linux]~# cat file
THE GREEN TABLE
Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Vestibulum consectetur, purus vitae sagittis
vehicula, justo ipsum accumsan mi, in ornare risus
turpis vel dolor. Aliquam in auctor nibh. Proin sit
amet diam purus. Quisque suscipit, dui eu vehicula
malesuada, tortor neque sagittis dolor, vel placerat
lacus odio sed justo.

THE GREEN TABLE
Cum sociis natoque penatibus et magnis dis parturient
montes, nascetur ridiculus mus. Donec convallis hendrerit
odio non sollicitudin. Vivamus condimentum consequat
velit, ut volutpat mi tristique eget. Donec magna mi,
tempor at posuere non, pulvinar quis tortor. Integer
ornare vehicula enim, et gravida neque laoreet non.
[root@linux]~# perl -pe 's/THE GREEN TABLE/\n$&/||s/\n/ /;END{print "\n"}' file

THE GREEN TABLE
Lorem ipsum dolor sit amet, consectetur adipiscing  elit. Vestibulum consectetur, purus vitae sagittis  vehicula, justo ipsum accumsan mi, in ornare risus  turpis vel dolor. Aliquam in auctor nibh. Proin sit  amet diam purus. Quisque suscipit, dui eu vehicula  malesuada, tortor neque sagittis dolor, vel placerat  lacus odio sed justo.
THE GREEN TABLE
Cum sociis natoque penatibus et magnis dis parturient  montes, nascetur ridiculus mus. Donec convallis hendrerit  odio non sollicitudin. Vivamus condimentum consequat  velit, ut volutpat mi tristique eget. Donec magna mi,  tempor at posuere non, pulvinar quis tortor. Integer  ornare vehicula enim, et gravida neque laoreet non.

# 10  
Old 06-11-2012
Try calling the first paragraph THE BLUE TABLE. When I run it - it is doing the entire document.
# 11  
Old 06-11-2012
You wanted "specific text" to be matched. This code is matching only "THE GREEN TABLE". Do you have some rule on how does the specific text look like? Does it always have "TABLE" in there?
# 12  
Old 06-11-2012
It varies - but it is always a 2 or 3 word phrase. In these examples I used THE BLUE TABLE and THE GREEN TABLE.

When I run the code it is removing all line breaks in the document, not just those after the matched words.
# 13  
Old 06-12-2012
Thank you for the input, and my apologies if I'm slow on getting it.

bartus11, I can see now how your code works:

Code:
perl -pe 's/THE GREEN TABLE/\n$&/||s/\n/ /;END{print "\n"}' file

It removes line breaks from everything except the matched text. In my usage that unfortunately won't work - the files are long and dynamic.

What will work is code that removes line breaks from the lines AFTER the match.

It will remove all line breaks in a document after a match, until there is a blank line. Then it will stop.

It won't start removing line breaks again until there is a match, then it will remove line breaks starting on the next line, until there is a blank line, then it will stop.

It will not remove line breaks from blank lines.

Example - the text before:
Code:
HEADLINE ONE
Lorem ipsum dolor sit amet, consectetur adipiscing 
elit. Vestibulum consectetur, purus vitae sagittis 
vehicula, justo ipsum accumsan mi, in ornare risus 
turpis vel dolor. 

HEADLINE TWO
Cum sociis natoque penatibus et magnis dis parturient 
montes, nascetur ridiculus mus. Donec convallis hendrerit 
odio non sollicitudin. Vivamus condimentum consequat 
velit, ut volutpat mi tristique eget. 

HEADLINE THREE
Lorem ipsum dolor sit amet, consectetur adipiscing 
elit. Vestibulum consectetur, purus vitae sagittis 
vehicula, justo ipsum accumsan mi, in ornare risus 
turpis vel dolor. 

HEADLINE FOUR
Cum sociis natoque penatibus et magnis dis parturient 
montes, nascetur ridiculus mus. Donec convallis hendrerit 
odio non sollicitudin. Vivamus condimentum consequat 
velit, ut volutpat mi tristique eget.


The text after, presuming a match for HEADLINE ONE and HEADLINE THREE:

Code:
HEADLINE ONE
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum consectetur, purus vitae sagittis vehicula, justo ipsum accumsan mi, in ornare risus turpis vel dolor. 

HEADLINE TWO
Cum sociis natoque penatibus et magnis dis parturient 
montes, nascetur ridiculus mus. Donec convallis hendrerit 
odio non sollicitudin. Vivamus condimentum consequat 
velit, ut volutpat mi tristique eget. 

HEADLINE THREE
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum consectetur, purus vitae sagittis vehicula, justo ipsum accumsan mi, in ornare risus turpis vel dolor. 

HEADLINE FOUR
Cum sociis natoque penatibus et magnis dis parturient 
montes, nascetur ridiculus mus. Donec convallis hendrerit 
odio non sollicitudin. Vivamus condimentum consequat 
velit, ut volutpat mi tristique eget.

Thank you all who can offer a solution.
# 14  
Old 06-12-2012
I'm curious. Did you test my suggestion?

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove single-line breaks only in document

Regarding copy/pasted text of copyright-free book from archive.org (link below), in attempt to expand single-line-break paragraph text (not section headings or paragraph breaks) to wider right margin, Justify or Wrap in LIbreOffice is not working, and Find/Replace the paragraph mark ($) wraps all... (2 Replies)
Discussion started by: p1ne
2 Replies

2. UNIX for Beginners Questions & Answers

Remove line breaks and extra spaces

Hi, I want to remove all extra spaces, line breaks . Need a new line entry only for term starting"array" For eg: my input is array(), array(), array(), and my expected output is array(), array(), array(), Is it possible using awk? (5 Replies)
Discussion started by: rsi.245
5 Replies

3. Shell Programming and Scripting

[BASH] read 'line' issue with leading tabs and virtual line breaks

Heyas I'm trying to read/display a file its content and put borders around it (tui-cat / tui-cat -t(ypwriter). The typewriter-part is a 'bonus' but still has its own flaws, but thats for later. So in some way, i'm trying to rewrite cat using bash and other commands. But sadly it fails on... (2 Replies)
Discussion started by: sea
2 Replies

4. UNIX for Dummies Questions & Answers

Page breaks and line breaks

Hi All, Need an urgent solution to an issue . We have created a ksh file or shell script which generates 1 DAT file. the DAT file contains extract of a select statement . Now the issue is , when we are executing the ksh file , the output is coimng with page breaks and line breaks . We have... (4 Replies)
Discussion started by: Ayaskant
4 Replies

5. Shell Programming and Scripting

Awk-sed help : to remove first and last line with pattern match:

awk , sed Experts, I want to remove first and last line after pattern match "vg" : I am trying : # sed '1d;$d' works fine , but where the last line is not having vg entry it is deleting one line of data. - So it should check for the pattern vg if present , then it should delete the line ,... (5 Replies)
Discussion started by: rveri
5 Replies

6. Shell Programming and Scripting

Remove line breaks in csv file using shell script

Hi All, I've a csv file in which the record is getting break into 1 line or more than one line. I want to combine those splits into one line and remove the unwanted character existing in the record i.e. double quote symbol ("). The line gets break only when the record contains double... (4 Replies)
Discussion started by: rajak.net
4 Replies

7. Shell Programming and Scripting

remove non-alphabetic from a match line

Hey all, I want to remove non-alphabetic charecters from the line that have "Products from " for example a line like this: Products from LG (295) should just become like Products fromLG in the file, and then I would delete the remaining and it would be just "LG" tnx (9 Replies)
Discussion started by: Johanni
9 Replies

8. Shell Programming and Scripting

Piped input to sed 's/\n/ /' doesn't remove the line breaks..

Using ls input as example.. ls | sed 's/\n/ /'outputs with line breaks, where I was expecting the \n to disappear. I've tried \r as well wondering if terminal output used different breaks. Is there a way to remove the line breaks without saving to file and then working from there? ----------... (2 Replies)
Discussion started by: davidpbrown
2 Replies

9. Shell Programming and Scripting

any better way to remove line breaks

Hi, I got some log files which print the whole xml message in separate lines: e.g. 2008-10-01 14:21:44,561 INFO do something 2008-10-01 14:21:44,561 INFO print xml : <?xml version="1.0" encoding="UTF-8"?> <a> <b>my data</b> </a> 2008-10-01 14:21:44,563 INFO do something again I want... (3 Replies)
Discussion started by: csmklee
3 Replies

10. Shell Programming and Scripting

How to remove page breaks from a flat file???

Hi All, I get a flat file with its last field data splitting onto a new line.I got this program from Vgersh which when run would cancatenate the split data back to the end of the previous records.But this program fails when it encounters a page break between the split data and the previous... (5 Replies)
Discussion started by: kumarsaravana_s
5 Replies
Login or Register to Ask a Question