LinuxQuestions.org
Visit Jeremy's Blog.
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-12-2011, 08:11 PM   #1
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 425
Blog Entries: 28

Rep: Reputation: 2
sed append at new line if the old line is not empty


Hi, I want to append at a new line if the old line is non empty.
so it would be something like
Code:
1
2
3//dont add here
Code:
1
2
3
//add here
but if sometimes there could be extra empty line, then add at the empty line. So the script needs to detect whether or not the last line is empty.
i figured out the symbol for a new line pattern is ^
so it would probably be
sed "a\^stuff here"
is this right? but i dont seem to get the right result
Thanks
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-12-2011, 08:30 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Quote:
Originally Posted by ted_chou12 View Post
i figured out the symbol for a new line pattern is ^
so it would probably be
sed "a\^stuff here"
?? How did you figure that ??
Anyway, here is what I think you're looking for:
Code:
sed '$ {/^$/ {c newline
;q};a newline
}' file
The line-breaks are important! Issue them exactly as they are.
 
Old 02-12-2011, 09:06 PM   #3
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 425
Blog Entries: 28

Original Poster
Rep: Reputation: 2
hi, thank you, but i seem to have trouble substituting the variables in:
Code:
sed -i '$ {/^$/ {c newline
;q};a "${FORM_min} ${FORM_hr} ${FORM_day} ${FORM_mn} ${FORM_wd} ${FORM_path}${log}"
}' "file"
this adds line at every second line, so it added multiple lines, and at the end line, it does not add at the new empty line, but adds at the line that already existed, so the text became
1
5
2
5
3
5
45
sorry for my inexperience
thanks
 
Old 02-13-2011, 01:55 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,493

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Double quotes are required for parameter expansion. So you need them around the sed command instead of single ones.
 
Old 05-26-2011, 05:53 AM   #5
tg3793
Member
 
Registered: Jul 2009
Location: Philippines
Distribution: Linux Mint 13 (Mate Edition)
Posts: 50

Rep: Reputation: 12
Any chance someone can help me with a one liner? I won't go over the last sixteen examples that I've tried from the studying I've been doing over the last two hours but I'll just give you the last one.

I have several dozen .txt files in a directory and I wanted to add about four empty lines to the end of each text file before I combine them.

I already know how to combine several files with the CAT command. I just need help adding those four lines to the end of each .txt file.

Here is 'one' example of what I had tried to do (again I'm sparing you the other sixteen or so examples):

Code:
cat * | sed -i '$:/r:g'
Isn't the "/r" supposed to be what you do to add a blank line?
 
Old 05-26-2011, 06:00 AM   #6
tg3793
Member
 
Registered: Jul 2009
Location: Philippines
Distribution: Linux Mint 13 (Mate Edition)
Posts: 50

Rep: Reputation: 12
<sigh> ... So I finally ask the question and THEN I seem to have figured it out.
I think this is working for me guys. Let me know if you can think of any improvements or anything that I'm overlooking.

Code:
sed -i '$ s/$/ \r\r/' *
 
Old 05-26-2011, 06:08 AM   #7
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209
@tg3793

Please start your own thread. Hijacking other people's threads can negatively affect your love life (and is considered rude.)
 
Old 05-26-2011, 06:11 AM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Quote:
Originally Posted by tg3793 View Post
<sigh> ... So I finally ask the question and THEN I seem to have figured it out.
I think this is working for me guys. Let me know if you can think of any improvements or anything that I'm overlooking.

Code:
sed -i '$ s/$/ \r\r/' *
Ok,

first of all, please do not hijack other threads. Start your own thread and link to other threads if you think they might help in clarifying the problem.

You said you wanted to concatenate the files. I also do not know how your construct will add empty lines. Are you trying to manipulate windows files?
Here is one that works with Unix files:
Code:
sed -s '$ s/$/\n\n\n\n/' file file2 > combined
 
Old 05-26-2011, 07:57 AM   #9
tg3793
Member
 
Registered: Jul 2009
Location: Philippines
Distribution: Linux Mint 13 (Mate Edition)
Posts: 50

Rep: Reputation: 12
Quote:
Originally Posted by sycamorex View Post
@tg3793

Please start your own thread. Hijacking other people's threads can negatively affect your love life (and is considered rude.)
I would disagree. I think that my comment was completely related to the post.
http://www.squidoo.com/threadjacking

But then again I'm not a Linux veteran and I might have incorrectly assessed that "sed append at new line if the old line" and my issue of "using sed to append a line to the end of a text file" have nothing to do with each other :-)

I mean this with complete respect. I don't want to say things on a thread that are unrelated, but on the opposite end of the spectrum I don't want to start a new thread about something that is already being discussed.

Last edited by tg3793; 05-29-2011 at 11:37 PM.
 
Old 05-26-2011, 08:04 AM   #10
Hevithan
Member
 
Registered: Apr 2011
Location: Washington State
Distribution: Zorin5-(Ubuntu 11.04) // Backtrack 5-(Ubuntu 10.04) // Dreamlinux 3.5-(Debian)
Posts: 275
Blog Entries: 5

Rep: Reputation: 40
The keywords I think where '' start your own". It has nothing to do with the fact you are off topic, It has to do with the fact that everyone who responded is focusing on 1 specific problem (the first posters problem) and introducing a new one will throw people off, Because they'll have to sift through posts to find the one they want to answer, Then you get 5 conversations going at once and it is a mess. So you can start a new thread and keep things more organized and maybe start off with something like:

"Hello, I have a problem much like this: (insert link to this thread) ...blah blah blah"

Last edited by Hevithan; 05-26-2011 at 08:09 AM.
 
Old 05-26-2011, 08:04 AM   #11
tg3793
Member
 
Registered: Jul 2009
Location: Philippines
Distribution: Linux Mint 13 (Mate Edition)
Posts: 50

Rep: Reputation: 12
Quote:
Originally Posted by crts View Post
You said you wanted to concatenate the files. I also do not know how your construct will add empty lines. Are you trying to manipulate windows files?
Here is one that works with Unix files:
Code:
sed -s '$ s/$/\n\n\n\n/' file file2 > combined
From the reading that I did it appears that both the "\r" and the "\n" work in different situations. I didn't have to try this one that you are suggesting because the combination I finally stumbled on (my doing so certainly wasn't skill :-) ) worked.

However I can see the additional commands that you have above are going to truly make the whole operation a one liner. And for clarification I am on Ubuntu Lucid. I don't know what program the .txt files were made in but it is a book that was broken into separate chapters, which I then want to in turn combine back into a single file (book).

Thanks again. I'm sure that this little exchange will be helpful for others in the future.
 
Old 05-26-2011, 08:09 AM   #12
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Quote:
Originally Posted by Hevithan View Post
The keywords I think where '' start your own". It has nothing to do with the fact you are off topic, It has to do with the fact that everyone who responded is focusing on 1 specific problem (the first posters problem) and introducing a new one will throw people off, Because they'll have to sift through post to find the one they want to answer, Then you get 5 conversations going at once and it is a mess. So you can start a new thread and keep things more organized and maybe start off with something like:

"Hello, I have a problem much like this: (insert link to this thread) ...blah blah blah"
In addition to what hevithan said, starting your own thread has also the advantage that it will be on the zero reply list, i.e. it will be bumped automatically a couple of times if noone responds. So it is not just the matter of breaching 'protocol'

Hope this helps.
 
2 members found this post helpful.
Old 05-26-2011, 08:33 AM   #13
tg3793
Member
 
Registered: Jul 2009
Location: Philippines
Distribution: Linux Mint 13 (Mate Edition)
Posts: 50

Rep: Reputation: 12
Quote:
Originally Posted by crts View Post
In addition to what hevithan said, starting your own thread has also the advantage that it will be on the zero reply list, i.e. it will be bumped automatically a couple of times if noone responds. So it is not just the matter of breaching 'protocol'

Hope this helps.
Ok ok point taken :-) ... But perhaps it would still be useful to have a policy on this site somewhere. I think 'now' we are really getting off topic so I will link to another post that I made to carry us into the thread hijacking topic if we are interested.

I did a little searching on this forum and I don't seem to be the only one that thinks commenting on an existing thread is beneficial. But I can also see your point as well and I'll be happy to play along with the LQ way of doing things if that's what you guys think is the best for everyone here.
 
  


Reply

Tags
[sed]


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
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 01:48 PM
[SOLVED] Trying to number every other line and append those numbers to end of line kmkocot Programming 7 04-23-2010 11:17 AM
Recursive line append with sed usrb Programming 3 04-01-2009 05:10 AM
Attempting to append a line of text to the end of the previous line market_garden Linux - General 4 12-11-2008 11:37 AM
using sed to append a variable after a certain line jadeddog Programming 6 11-05-2008 12:49 AM


All times are GMT -5. The time now is 05:29 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration