LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed append at new line if the old line is not empty (https://www.linuxquestions.org/questions/linux-newbie-8/sed-append-at-new-line-if-the-old-line-is-not-empty-862339/)

ted_chou12 02-12-2011 08:11 PM

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:banghead:
Thanks

crts 02-12-2011 08:30 PM

Quote:

Originally Posted by ted_chou12 (Post 4256602)
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.

ted_chou12 02-12-2011 09:06 PM

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

grail 02-13-2011 01:55 AM

Double quotes are required for parameter expansion. So you need them around the sed command instead of single ones.

tg0000 05-26-2011 05:53 AM

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?

tg0000 05-26-2011 06:00 AM

<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/' *

sycamorex 05-26-2011 06:08 AM

@tg3793

Please start your own thread. Hijacking other people's threads can negatively affect your love life (and is considered rude.)

crts 05-26-2011 06:11 AM

Quote:

Originally Posted by tg3793 (Post 4367506)
<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

tg0000 05-26-2011 07:57 AM

Quote:

Originally Posted by sycamorex (Post 4367511)
@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.

tg0000 05-26-2011 08:04 AM

Quote:

Originally Posted by crts (Post 4367512)
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.

Hevithan 05-26-2011 08:04 AM

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"

crts 05-26-2011 08:09 AM

Quote:

Originally Posted by Hevithan (Post 4367605)
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.

tg0000 05-26-2011 08:33 AM

Quote:

Originally Posted by crts (Post 4367609)
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.


All times are GMT -5. The time now is 07:18 PM.