sed not replacing the second occurance [duplicate] - sed

This question already has answers here:
How to replace only on second matching row using sed
(4 answers)
Closed 4 years ago.
File: test
cat cat
dog cat
dog puppy
dog cat
Command:
sed -i 's/dog/big_dog/2' test
According to the explanation here: http://www.grymoire.com/Unix/Sed.html#uh-8 the output of the code should have been:
cat cat
dog cat
big_dog puppy
dog cat
But the file remains unchanged for me.
What am I doing wrong?

Looks like awk has a solution:
awk '/dog/{c++;if(c==2){sub("dog","big_dog");c=0}}1' test
Source: https://www.linuxquestions.org/questions/programming-9/replace-2nd-occurrence-of-a-string-in-a-file-sed-or-awk-800171/

Related

Sed with variables [duplicate]

This question already has answers here:
sed substitution with Bash variables
(6 answers)
Closed 1 year ago.
i have a config file with this format
http://link:port/username/password/1234
Im using this code to replace the Username1/Password1 with a different Username2/Password2
sed -i -e 's/\/Username1\/Password1/\/Username2\/Password2/g' /etc/config.cfg
Now i want to make this something like
$UsernameOLD = Username1
$PasswordOLD = Password1
$UsernameNEW = Username2
$PasswordNEW = Password2
sed -i -e 's/\/$UsernameOLD\/$PasswordOLD/\/$UsernameNEW\/$PasswordNEW/g' /etc/config.cfg
Could anyone help me getting this ready ?
I found the solution i had to use double quotes instead of '
The working command looks like this:
UsernameOLD=MyOldUsername
PasswordOLD=MyOldPassword
UsernameNEW=MyNewUsername
PasswordNEW=MyOldUsername
sed -i -e "s/\/$UsernameOLD\/$PasswordOLD/\/$UsernameNEW\/$PasswordNEW/" /etc/config.cfg

sed: -e expression #1, char 5: unterminated `s' command [duplicate]

This question already has answers here:
sed edit file in place
(15 answers)
Closed 4 years ago.
I'm learning SED and I saw this sed substitution example. It's supposed to replace the first lowercase t as uppercase in each new line.:
$ sed 's/t/T' text-01.txt
sed: -e expression #1, char 5: unterminated `s' command
Contents of file:
$ cat text-01.txt
10 tiny toes
this is that
5 funny 0
one two three
tree twice
It's not the end of the world though, since I can just output into a new file:
cat text-01.txt | sed 's/t/T/' > text-02.txt
But what am I supposed to do if I want to edit the original file?
The commands aren't the same, the closing / is missing in the first one:
# v
sed 's/t/T' text-01.txt
cat text-01.txt | sed 's/t/T/' > text-02.txt
# ^

sed: Why does a file being edited in-place have modified lines doubled? [duplicate]

This question already has answers here:
sed is printing a substituted line twice
(2 answers)
Closed 6 years ago.
The perils of working tired:
sed -i 's/foo/barbazqux/p' example.txt
This made the output file have two copies of any modified line.
Turns out I hadn't removed the p flag from the end of the command. I'd been doing this when testing it on the command line.
Correct command:
sed -i 's/foo/barbazqux/' example.txt

Substitution option: s in sed command [duplicate]

This question already has answers here:
How to use sed to replace only the first occurrence in a file?
(25 answers)
Closed 8 years ago.
I am using sed command make substitution in the file.
Assume my file1 is:
10
11
10
11
Then I want to substitute the 1st "10" in file1 to "12", and dump to file2. The file2 should be:
12
11
10
11
I tried this command:
sed 's/10/12/' file1 >file2
But it changed the 2nd "10" also. So, how can I write the sed command to do that?
If you can use awk instead of sed, you can have more control like this:
awk '!changed && /10/ { sub(/10/, "12"); changed = 1}1' file1
12
11
10
11
try:
sed '0,/10/s/10/12/' file1 >file2
Like GriffinG said, you can do something like this:
sed '0,/10/ s/10/12/' file1 > file2
The 0,/10/ at the beginning sets the bounds on the following substitution command. It says start from line 0 (the first line), and go until a line it matches the pattern /10/. So After that first match, sed will stop processing that substitution.
If you do not have GNU sed, try:
echo | cat - file | sed '1,/10/s/10/12/; 1d'
or
sed '1,/10/s/10/12/; 1d' << EOF
$(cat file)
EOF
or in bash / ksh93
sed '1,/10/s/10/12/; 1d' <(echo; cat file)
GNU sed knows 0,/10/ so the extra empty line is not required

Sed (POSIX) coming from linux [duplicate]

This question already has answers here:
How to remove every other line with sed?
(5 answers)
Closed 8 years ago.
I am somewhat new to posix, and i can't use: sed '1~2p'
My goal is to skip every one line from line 1:
1
2
3
4
would become
1
3
I was wondering what is the posix equivalent of ~.
Code for sed:
sed -e n -e d file
or:
sed -e 'n;d' file
The simpler, portable solution would be:
awk 'NR%2' file
bash solution:
while read -r line; do
[ $((i++ % 2)) -eq 0 ] && echo "$line";
done < file