开发者

deleting only pattern from the lines of a file

开发者 https://www.devze.com 2023-03-22 09:22 出处:网络
I just want to delete a pattern from a file using shell script. For example, given: blah blah (100%) blah blah

I just want to delete a pattern from a file using shell script. For example, given:

blah blah (100%) blah blah 

I want to delete all occurrences of the pattern (100%), even if it appears more than once o开发者_StackOverflow社区n a line, and on all lines in the file where it does appear.


You can use sed

echo "blah (100%) blah (100%)" | sed 's/(100%)//g'
blah  blah 


sed -i 's/(100%)//g' <yourfile>

Example:

[you@home]$ cat file.txt

blah blah (100%) blah blah
ah (100%) blah blah (100%)
blah blah (100%) blah

[you@home]$ sed -i 's/(100%)//g' file.txt

[you@home]$ cat file.txt

blah blah  blah blah
ah  blah blah
blah blah  blah

The -i option means modifies the file in place. If that is omitted, the result is printed to stdout and the file remains unchanged.

0

精彩评论

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

关注公众号