开发者

How to make sed read its script from a file?

Recently I came across following grep command:

/usr/xpg4/bin/grep -Ff grep.txt input.txt > output.txt

which as per my understanding means that from input.txt, grep the matter contained in grep.txt and output it to output.txt.

I want to do something similar for sed i.e. I want to keep the sed commands in a separate file (say sed.txt) and want to apply them on input file (say input.txt) and creat开发者_开发问答e a output file (say output.txt).

I tried following:

/usr/xpg4/bin/sed -f sed.txt input.txt > output.txt

It does not work and I get the following error:

sed: command garbled

The contents of files mentioned above are as below:

sed.txt

sed s/234/acn/ input.txt
sed s/78gt/hit/ input.txt

input.txt

234GH
5234BTW
89er
678tfg
234
234YT
tfg456
wert
78gt
gh23444


Your sed.txt should only contain sed commands: No prefixing with sed or suffixing with an input file. In your case it should probably be:

# sed.txt
s/234/acn/
s/78gt/hit/

When ran on your input:

$ /usr/xpg4/bin/sed -f sed.txt input.txt
acnGH
5acnBTW
89er
678tfg
acn
acnYT
tfg456
wert
hit
ghacn44


Rather than keeping the sed commands in a separate text file, you may want to try creating a sed script. The file below can run directly on your data files:

./myscript.sed inputfile.txt > outputfile.txt

#!/bin/sed -f
s/234/acn/
s/78gt/hit/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜