LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 02-22-2012, 09:07 AM   #1
spart1985
LQ Newbie
 
Registered: Feb 2012
Posts: 6

Rep: Reputation: Disabled
Smile Bash script to replace line break with condition


So now I have a file like this :

Quote:
2012 hello hi
excep
no excep

2012 no one
excep
So I will have to remove line breaks from those lines
which starts with character line excep and not number like 2012.

So the result should be like :
Quote:
2012 hello hi excep no excep

2012 no one excep
Thanks in advance
 
Old 02-22-2012, 09:12 AM   #2
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,211

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
you need an if condition and the tr command like so:
Code:
cat file.txt | tr '\n' ' ' > out-file.txt
what this does is pipe the output of cat into tr and substitutes all new-lines with spaces (you mite need to play around with sed awk to get it just rite)).

Last edited by schneidz; 02-22-2012 at 09:16 AM.
 
Old 02-22-2012, 10:48 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
sed is not all that easy to use when doing multi-line edits like this, but it can handle them.

Code:
sed -r ':a ; N; s/\n([^0-9])/ \1/ ; t a'  file
The N command grabs the next line and adds it to the current one in the pattern buffer, separated by a newline. Then you can target that newline in the s/// command. In this case, if the newline is followed by a non-digit character, then it will replace it with a space (carrying over the matched number).

:a ... t a is a (test) loop. You need to embed the above command in one in order to have it keep processing newlines until the condition fails.

Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

I've found the sed faq to be especially helpful in situations like this.
 
Old 02-22-2012, 12:07 PM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Code:
awk 'ORS=/^$/?RT"\n":" "' file
 
Old 02-23-2012, 02:44 AM   #5
spart1985
LQ Newbie
 
Registered: Feb 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks for the help grail, appreciate you effort but in your script
Quote:
awk 'ORS=/^$/?RT"\n":" "' file
removes all the line breaks until it finds an empty line. So if there are no empty line it brings all the line in a single line. In my case I only want those line that are starting with some characters.
 
Old 02-23-2012, 03:30 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Well it is the usual story ... if you present the incorrect data you do not always get the desired result, however, place your regex between the slashes and see how you go.
Let us know if you get stuck?
 
Old 02-23-2012, 05:28 AM   #7
spart1985
LQ Newbie
 
Registered: Feb 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hi David,
Your solution works perfectly. But somehow it is failing to execute sometimes. Like if my input is :

Quote:
2102testing
tsting
testin

2012 testing
2102 testing
no one
all in one

2012 dddd
the result is

Quote:
2102testing tsting testin

2012 testing
2102 testing
no one all in one

2012 dddd
Where as it should be like :
Quote:
2102testing tsting testin

2012 testing
2102 testing no one all in one

2012 dddd
Thanks in advance. Also your references are really helpful.
 
Old 02-23-2012, 09:52 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
So in other words, it doesn't work perfectly.

Yeah, I seem to have left out some necessary parts. That's what I get for trying to work from memory. Multi-line sed commands can be such a dog to wrap your head around.

So, going back to the sed faq, here's the full working (hopefully) command:

Code:
sed -r ':a ;$! N; s/\n([^0-9])/ \1/; ta ; P ; D'
The added parts do this:

$! N means it doesn't try to continue on when the last line is reached. The last line doesn't print without it.
P ; D prints out, then deletes, the part of the pattern space before the first newline.

Frankly, I'm still not completely sure what everything is doing. If I'm reading it right, it tries to loop through as many lines as it can, but the loop breaks when it hits a line that contains a digit, or nothing. So whenever the loop terminates, it prints out the first, completed part of the pattern space, and continues to hold on to the last part (the line that caused it to break) for further processing in the next pass.
 
1 members found this post helpful.
Old 02-23-2012, 10:43 AM   #9
spart1985
LQ Newbie
 
Registered: Feb 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks David. The issue is solved now and everything is perfect.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 07:56 AM
Perl script to replace a line after finding a previous line Mark1986 Programming 1 02-28-2011 06:09 PM
line break on bash script ZAMO Linux - General 5 04-24-2010 01:27 PM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 10:43 AM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 09:49 PM


All times are GMT -5. The time now is 10:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration