SED inserting a blank line below a line - sed

I want to add a black line below a line ending in a period.
I have sed '/ REGEX goes here/G' I'm not sure how to replace the regex to except a period as expression to look for.
I want to add a blank line below each sentence here's an example of some text I would want to use the sed shell script on.
Line one.
The second line.
The third.
This is line four.
five.
This is the sixth sentence.
This is line seven.
Eighth and last.

Try following sed
sed 's/\.$/.\n/' file
EDIT (after comments of #Jotne)
If you have any spaces after . at the end of line, adding \s* would be safe.
sed 's/\.\s*$/.\n/' file

I'm not sure how to replace the regex to except a period as expression
to look for.
You can say:
sed '/\.$/G' filename
Escape the . so as to match a literal .; the anchor $ ensures that the matched . was at the end of line.

Related

sed replace # with empty line

How do you replace a line that only has # with an empty line using sed?
I have tried to find on google but I haven't gotten anything.
File Content:
#test
another test
#
another test2
Expected result:
#test
another test
another test2
So, under expected result, after another test the line should be blank without the #.
Any help is greatly appreciated.
With regular expressions you can match for the beginning of a line with ^ and the end of a line with $. The s/regexp/replacement/ command will replace text that matches regexp with replacement.
This sed command gives the desired output:
sed 's/^#$//' < input.txt
On each line, sed looks for the start of a line, a # character, and then the end of a line, and replaces it with nothing. The newline character remains, however, so you are left with a blank line.
sed '/^#$//'
Anchor to beginning (^) and end ($) of line to match a whole line exactly.
Using sed
$ sed '/[[:alnum:]]/ ! s/#//' file
#test
another test
another test2
This might work for you (GNU sed):
sed '/^#$/g' file
If a line contains only #, replace it by a blank line.
Alternatives:
sed 's/^#$//' file
or
sed '/^#$/c\\' file

How to replace the nth occurrence of a string using sed

Is there any way to replace the nth occurrence of a string in a file using sed?
I'm using sed -i '0,/jack.*/ s//jill/' to replace the first occurrence.
How can i change it so that it replaces the nth occurrence?
My file contents the following lines:
first line
second line
third line
jack=1
fifth line
jack=
seventh line
I don't know the value after jack=, it can be anything or nothing.
I want to replace the 2nd occurrence of jack= and anything that follows it with jill.
First replace all the newlines with a unique character that does not occur anywhere else in your file (e.g. ^) using tr. You need to do this in order to create a single string for sed. Then pass it to sed and tell it to replace the nth occurrence of your string. Finally, pass the output back through tr to recreate the newlines.
For n=2, the command is:
$ tr '\n' '^' < file | sed 's/jack/jill/2' | tr '^' '\n'
first line
second line
third line
jack
fifth line
jill
seventh line
Update:
It can also be done with sed, WITHOUT changing the newlines first, using the following command:
$ sed ':a;N;$!ba;s/jack/jill/2' file
Alternatively, use awk:
$ awk '/jack/{c+=1}{if(c==2){sub("jack","jill",$0)};print}' file
Try this, sed ':a;N;$!ba;s/word1/word2/n' filename
Here, :a;N;$!ba is used to load the entire file into memory, line by line, so that sed can process the whole file in a single pass. The s/word1/word2/N substitution then replaces every Nth occurrence of word1 with word2.

replace last character in a line with sed

I am trying to replace the last character in a line with the same character plus a quotation mark '
This is the sed code
sed "s/\([A-Za-z]\)$/\1'/g" file1.txt > file2.txt
but does not work. Where is the error?
try:
sed "s/\([a-zA-Z]\)\s*$/\1\'/" file
This will replace the last character in the line followed by none or many spaces.
HTH Chris
It seems pointless to replace a character with itself, so try this: for lines ending with a letter, add a quote to the end:
sed "/[a-zA-Z]$/s/$/'/"
This does what you ask for:
sed "s/\(.\)$/\1'/" file1.txt > file2.txt
Your line only matches a line with a single character. Note that the s operation only takes effect if the line matches, not if only a subset of the line matches the regex.

Can I use the sed command to replace multiple empty line with one empty line?

I know there is a similar question in SO How can I replace mutliple empty lines with a single empty line in bash?. But my question is can this be implemented by just using the sed command?
Thanks
Give this a try:
sed '/^$/N;/^\n$/D' inputfile
Explanation:
/^$/N - match an empty line and append it to pattern space.
; - command delimiter, allows multiple commands on one line, can be used instead of separating commands into multiple -e clauses for versions of sed that support it.
/^\n$/D - if the pattern space contains only a newline in addition to the one at the end of the pattern space, in other words a sequence of more than one newline, then delete the first newline (more generally, the beginning of pattern space up to and including the first included newline)
You can do this by removing empty lines first and appending line space with G command:
sed '/^$/d;G' text.txt
Edit2: the above command will add empty lines between each paragraph, if this is not desired, you could do:
sed -n '1{/^$/p};{/./,/^$/p}'
Or, if you don't mind that all leading empty lines will be stripped, it may be written as:
sed -n '/./,/^$/p'
since the first expression just evaluates the first line, and prints it if it is blank.
Here: -n option suppresses pattern space auto-printing, /./,/^$/ defines the range between at least one character and none character (i.e. empty space between newlines) and p tells to print this range.

Remove Leading Whitespace from File

My shell has a call to 'fortune' in my .login file, to provide me with a little message of the day. However, some of the fortunes begin with one leading whitespace line, some begin with two, and some don't have any leading whitespace lines at all. This bugs me.
I sat down to wrapper fortune with my own shell script, which would remove all the leading whitespace from the input, without destroying any formatting of the actual fortune, which may intentionally have lines of whitespace.
It doesn't appear to be an easy one-liner two-minute fix, and as I read(reed) through the man pages for sed and grep, I figured I'd ask our wonderful patrons here.
Using the same source as Dav:
# delete all leading blank lines at top of file
sed '/./,$!d'
Source: http://www.linuxhowtos.org/System/sedoneliner.htm?ref=news.rdf
Additionally, here's why this works:
The comma separates a "range" of operation. sed can accept regular expressions for range definitions, so /./ matches the first line with "anything" (.) on it and $ specifies the end of the file. Therefore,
/./,$ matches "the first not-blank line to the end of the file".
! then inverts that selection, making it effectively "the blank lines at the top of the file".
d deletes those lines.
# delete all leading blank lines at top of file
sed '/./,$!d'
Source: http://www.linuxhowtos.org/System/sedoneliner.htm?ref=news.rdf
Just pipe the output of fortune into it:
fortune | sed '/./,$!d'
How about:
sed "s/^ *//" < fortunefile
i am not sure about how your fortune message actually looks like, but here's an illustration
$ string=" my message of the day"
$ echo $string
my message of the day
$ echo "$string"
my message of the day
or you could use awk
echo "${string}" | awk '{gsub(/^ +/,"")}1'