English 中文(简体)
除了含有某些字句的内容外,如何删除含有某些字句的内容?
原标题:How to delete all lines containing certain words, except those containing certain words?
  • 时间:2012-04-22 03:25:54
  •  标签:
  • bash
  • sed

我有一份名为<代码>的文件。 除含有“1”或“城市”的字句外,我谨删除每一行,包括“中心”、“农场”或“中间”等。

  • The list of deletions and exceptions is quite long.
  • The files are in UTF-8.

我怎么能删除其中至少一个字的每一个行文,而不要删除那些有某些例外的行文?

最佳回答

这或许有助于你:

sed -i  /center of|farm|middle of/{/①|city/!d}  file1.txt

sed -i  /center of/ba
        /farm/ba
        /middle of/ba
        b 
        :a
        /①/b
        /city/b
        d  file1.txt

如果有<代码>序号和,则使用:

sed  /*exceptions*/{h;s/.*/:a/p;d}
    x
    /./{x;s|.*|/&/b|p;$!d;s/.*/d/;q}
    x
    s|.*|/&/ba|  words.txt - <<<"*exceptions*" exceptions.txt > file.sed
sed -i -f file.sed file1.txt
问题回答
sed -r  /①|city/{p;d};/center of|farm|middle of/d  file1.txt
sed  /blacklist/{/whitelist/p;d}  file

删除黑名单,但列入白色名单者除外:

echo -e "a b
b c
c d
d e
e f" | sed  /c|d/{/a|b/p;d} 

印刷

a b
b c
e f

每一行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...