7

I have two files one holds a list of patterns, another need to be changed based on these patterns. So far I have been experimenting with

cat patterns.txt| xargs -n1| sed 's/patternfromfile/subtitution/'

but I am not sure how to enter xargs into 's/here//'

Thank you

3 Answers 3

5

You may want the -i option to xargs (check the man page for details). That essentially replaces {} with the pattern, and runs the command once per pattern. So, it'll run sed on the file once per pattern; you'll need to bear that in mind when creating your patterns.

user@host [/tmp]$ cat patterns
a
[pd]ie
user@host [/tmp]$ cat file
hat
cherry pie
die alone
eat cake
user@host [/tmp]$ cat patterns | xargs -i sed 's/{}/moo/' file
hmoot
cherry pie
die moolone
emoot cake
hat
cherry moo
moo alone
eat cake

Alternatively, you could use something else to dynamically build the sed script from your patterns file. For example, you could use sed on your pattern file to apply them all in one pass over the file. :)

user@host [/tmp]$ sed 's|^|s/|;s|$|/moo/;|' patterns | tee patterns.sed
s/a/moo/;
s/[pd]ie/moo/;
user@host [/tmp]$ sed -f patterns.sed file
hmoot
cherry moo
moo moolone
emoot cake
1
  • Thats it! It worked perfectly thank you so much.
    – sgp667
    Feb 24, 2014 at 16:57
3

You can build a regex like this:

regex=$(tr '\n' '|' < patterns.txt)
sed -ri "s/$regex/replacement/" filename

You may choose to add the "g" flag to the s/// command.

-1

Without knowing what patterns you have:

Use -i option if you want to edit yourfile directly:

   while read pattern
    do
        sed -i 's/"${pattern}"/substitution/' yourfile
    done < patterns.txt

Alternatively redirect output to a new file:

   while read pattern
    do
        sed 's/"${pattern}"/substitution/' yourfile >> outfile
    done < patterns.txt
3
  • 1
    close: the variable substitution will not occur due to the single quotes. Feb 19, 2014 at 20:56
  • For safely editing the file-in-place, you can also use tee , like this, for example: cat file |sed "s/pat/subst/" |tee file
    – Rolf
    Nov 19, 2020 at 9:53
  • 1
    @Rolf That is not safe at all! It creates a race condition, where cat might or might not finish reading the file before tee blanks it and starts rewriting it. The shell usually starts the processes in a pipe in order, so for small files cat will usually win the race. But for big files, tee will blank the file before it's all read, and you'll get a truncated file. Usually. Unless the substitution expands the file enough, in which case you might get an infinite expansion. Feb 3, 2021 at 0:21

You must log in to answer this question.

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