-2

I have 3 files:

File1: &VAR1='1111';
File2: &VAR2 = '2222';
File3: &VAR1 = 1111

I want to change that for example with &NEWVAR = 'NEW';

So I have the following script:

A="\&VAR1\s*\=\s*'*[1]{4}'*"
for f in /u/123456/*; do
    if grep -qE $A $f; then
        sed 's/$LAREXP/$LACADE/g' $f
    fi
done

Grep tells me there are coincidence en File1 and File3, wich is correct, but the sed command is no replacing the old string with the new one in those files. It shows on screen the content of the files but there's no replace.

Can anyone help me with this?

Regards,

Ruben
  • 114
  • 12
  • Try `sed -i`, this edits the file in place instead of printing the result onto the terminal – lubgr Jun 28 '18 at 10:47
  • What are the dollar signs in the sed command for? If they're for the end of string then the pattern will never match, and if they're meant to be shell variables then they won't be expanded since they're within single quotes. – Biffen Jun 28 '18 at 10:49
  • Moreover, see also https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable – tripleee Jun 28 '18 at 10:54
  • Finally, neither `sed` nor `grep` commonly support PCRE regex like `\s`; see https://stackoverflow.com/questions/11568859/how-to-extract-text-from-a-string-using-sed/11568930#11568930 – tripleee Jun 28 '18 at 10:55

2 Answers2

3

You need to use sed -i to change the files in-place.

sed is a Stream EDitor and thus, by default, it edits data it reads and then writes it to stdout. Passing -i tells sed to write the changes back to the file given on the command line.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Thanks @Jonathon I used -i and the text stoped showing on screen, but the text hasnt changed. In fact, the 2 files have been edited, but sed did nothing. – Ruben Jun 28 '18 at 10:59
0

It's not clear what you want to accomplish. The obvious errors are fixed in the following annotated code.

# The regex is probably also incorrect; see blowe
A="\&VAR1\s*\=\s*'*[1]{4}'*"
for f in /u/123456/*; do
    # Add quotes around regex and filename
    if grep -qE "$A" "$f"; then
        # single quotes will not work; use doubles
        # add -i option for in-place editing
        # again, add duoble quotes around filename
        sed -i "s/$LAREXP/$LACADE/g" "$f"
    fi
done

The regex will certainly not work in sed and probably not in grep either. There are many different regex dialects and you have used constructs from Perl/Python regex which are not usually supported by older tools. Perhaps you mean

A="\\\\&VAR[[:space:]]*=[[:space:]]*'*1111'*"

which should work in both POSIX grep and POSIX sed. Some platforms support sed -E or sed -r to use something called POSIX Extended Regular Expressions ... but really this is too speculative without any indications of what the regex is supposed to match.

A common workaround is to use Perl.

perl -pi -e "s/$LAREXP/$LACADE/g" /u/123456/*

depending also on what's in LAREXP and LACADE.

tripleee
  • 175,061
  • 34
  • 275
  • 318