0

In various files in lots of sub folders I have the following text in large text files, each unique

Linux.Secret = 'XYZZYXZYXZYXZYXZYXZ'

I would like to make it so all the Linux.Secret = '' are the same as Linux.Secret = 'NEWSECRET'

How can I do this?

4
  • Are you just trying to replace all lines which match the pattern Linux.Secret = '[A-Z,a-z]' with the string Linux.Secret = 'NEWSECRET' ? Commented Jul 17, 2018 at 17:28
  • Yes anything that starts with Linux.Secret = and enclosed in ''
    – masterq
    Commented Jul 17, 2018 at 17:30
  • unix.stackexchange.com/questions/112023/…
    – Jeff Schaller
    Commented Jul 17, 2018 at 17:33
  • Just need to adapt one of those answers for handling the single quotes.
    – Jeff Schaller
    Commented Jul 17, 2018 at 17:34

1 Answer 1

2

First find all files that contains Linux.Secret = at the start of a line:

find . -type f -exec grep -q '^Linux.Secret =' {} ';'

Note that grep -q does not output anything, it just exits with a status depending on whether the expression could be matched or not.

Then, for all files that passes these tests (is a regular file and contains that text), we run a simple (GNU) sed command:

find . -type f -exec grep -q '^Linux.Secret =' {} ';' \
    -exec sed -i "s/^Linux.Secret =.*/Linux.Secret = 'NewSecret'/" {} +

This makes the change in-place in the files by substituting the whole line with the line we'd like to have. We handle the single quotes by using double quotes around the expressions that require it.

If you need to be more careful with the matching and only match Linux.Secrets = followed by something in single quotes:

find . -type f -exec grep -E -q "^Linux.Secret = '[^']+'" {} ';' \
    -exec sed -E -i "s/^Linux.Secret = '[^']+'.*/Linux.Secret = 'NEWSECRET'/" {} +

Related:

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .