0

Quick question about sed not replacing a second string.

I have two blocks of data; sed will replace the first block of data but it won't or replace some of the second string of data.

first block of data
abcd_0000001=/var/tmp /0000001
abcd_0000001=Y
abcd_0000001=
abcd_0000001=[A-za-z]*.*
abcd_0000001=/tmp/
abcd_0000001=1
abcd_0000001=true
abcd_0000001=pwd
abcd_0000001=scp
abcd_0000001=

second block of data
74747_cnn=/opt/0000001
74747_cnn=
74747_cnn=pwd
74747_cnn=1
74747_cnn=/
74747_cnn=/usr
74747_cnn=/usr/74747_cnn7
74747_cnn=
74747_cnn=scp
74747_cnn=true

If I want to replace abcd_0000001 with defg_0000077 and 74747_cnn with 19197_abc and 0000001 with 0000001AJC, then …

sed will replace the first block of data and will replace some of text from the second block of data. It will replace cnn with _abc but it will not replace 74747. What am I missing or doing wrong?

I am using the following:

sed -i 's/abcd_0000001/defg_0000077/g; s/74747_cnn/19197_abc/g; s/0000001/0000001AJC/g' file.txt
Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
caneus
  • 1
  • 1
  • You must be working with GNU `sed`; that `-i` notation would not work meaningfully with BSD `sed`. – Jonathan Leffler Apr 29 '18 at 04:53
  • @JonathanLeffler Does BSD `sed -i` require an argument, or is `-i` just not supported? – melpomene Apr 29 '18 at 05:00
  • 1
    BSD `sed` requires a non-empty argument optionally attached to `-i` (as in `-i.bak` or `-i .bak`) or an empty argument not attached to `-i` (as in `-i ''`). BSD does support `-i`; you can be neutral between BSD and GNU by using an attached non-empty argument (`-i.bak`). You can't do in situ overwrite in a platform neutral way. And other variants of Unix don't support `-i` at all — it is not standardized by POSIX, possibly because there are these divergent implementations,. – Jonathan Leffler Apr 29 '18 at 05:04
  • @melpomene see also https://stackoverflow.com/questions/24275070/sed-not-giving-me-correct-substitute-operation-for-newline-with-mac-difference/ and https://stackoverflow.com/questions/5694228/sed-in-place-flag-that-works-both-on-mac-bsd-and-linux – Sundeep Apr 29 '18 at 05:16

1 Answers1

0

Using either BSD or GNU sed, replacing your -i with -e (so the data file isn't corrupted modified and can be reused — don't use destructive options like -i until you know everything is working correctly, for your own sanity's sake!), I can execute the command you show and get the same output from the two, like this:

first block of data
defg_0000077=/var/tmp /0000001AJC
defg_0000077=Y
defg_0000077=
defg_0000077=[A-za-z]*.*
defg_0000077=/tmp/
defg_0000077=1
defg_0000077=true
defg_0000077=pwd
defg_0000077=scp
defg_0000077=

second block of data
19197_abc=/opt/0000001AJC
19197_abc=
19197_abc=pwd
19197_abc=1
19197_abc=/
19197_abc=/usr
19197_abc=/usr/19197_abc7
19197_abc=
19197_abc=scp
19197_abc=true

AFAICS, this has done all the substitutions you requested in both blocks of data. Is this not the output you were expecting? How is the output you are getting different from this?

$ /opt/gnu/bin/sed -e 's/abcd_0000001/defg_0000077/g; s/74747_cnn/19197_abc/g; s/0000001/0000001AJC/g' data > gnu.out
$ /usr/bin/sed -e 's/abcd_0000001/defg_0000077/g; s/74747_cnn/19197_abc/g; s/0000001/0000001AJC/g' data > bsd.out
$ diff bsd.out gnu.out
$ cat
first block of data
abcd_0000001=/var/tmp /0000001
abcd_0000001=Y
abcd_0000001=
abcd_0000001=[A-za-z]*.*
abcd_0000001=/tmp/
abcd_0000001=1
abcd_0000001=true
abcd_0000001=pwd
abcd_0000001=scp
abcd_0000001=

second block of data
74747_cnn=/opt/0000001
74747_cnn=
74747_cnn=pwd
74747_cnn=1
74747_cnn=/
74747_cnn=/usr
74747_cnn=/usr/74747_cnn7
74747_cnn=
74747_cnn=scp
74747_cnn=true
$

If you are getting something different, it means you don't have the same data that is shown in the question in the file you're editing. If you are expecting something different, you need to explain what you're expecting and why, because I believe there's a misunderstanding.

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185