replace last character in a line with sed - 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.

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 use Sed to change letter to uppercase in first and second column in text file to upper case

I have text file input.txt which has
april,december,month.gmail.com
lion,tiger,animal.gmail.com
Using sed change first and second columns to uppercase? Is there a way to do it?
With GNU sed:
sed 's/^[a-z]*,[a-z]*,/\U&/' file
s: substitute command
[a-z]*,: search for zero ore more lowercase letter followed by a ,. The pattern is repeated for second field
the \U sequence turns the replacement to uppercase
\U is applied to & which reference the matched string
or if there is only three comma separated fields:
sed 's/^[a-z].*,/\U&/' file
output:
APRIL,DECEMBER,month.gmail.com
LION,TIGER,animal.gmail.com
As #Sundeep suggests, the second sed can be shortened to:
s/^.*,/\U&/
which converts all characters until last , is found
For more on GNU sed substitution command, see this article

SED inserting a blank line below a line

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.

Insert newline after pattern with changing number in sed

I want to insert a newline after the following pattern
lcl|NC_005966.1_gene_750
While the last number(in this case the 750) changes. The numbers are in a range of 1-3407.
How can I tell sed to keep this pattern together and not split them after the first number?
So far i found
sed 's/lcl|NC_005966.1_gene_[[:digit:]]/&\n/g' file
But this breaks off, after the first digit.
Try:
sed 's/lcl|NC_005966.1_gene_[[:digit:]]*/&\n/g' file
(note the *)
Alternatively, you could say:
sed '/lcl|NC_005966.1_gene_[[:digit:]]/G' file
which would add a newline after the specified pattern is encountered.
sed 's/lcl|NC_005966\.1_gene_[[:digit:]][[:digit:]]*/&\
/g' file
You need to escape . as it's an RE metacharacter, and you need [[:digit:]][[:digit:]]* to represent 1-or-more digits and you need to use \ followed by a literal newline for portability across seds.

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.