Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果列签出为特定值,则提取行-Unix_Python_Awk_Sed - Fatal编程技术网

Python 如果列签出为特定值,则提取行-Unix

Python 如果列签出为特定值,则提取行-Unix,python,awk,sed,Python,Awk,Sed,假设我有一个选项卡分隔的文件,第一列作为某种索引: $ echo -e "0\tabc\txyz\n1\twhatever ever\tfoobar\n0\t12f2\t1" > test.txt $ cat test.txt 0 abc xyz 1 whatever ever foobar 0 12f2 1 我想提取第一列中索引为0或1的行 我可以用Python实现这一点: $ python -c "print '\n'.join([line.strip() f

假设我有一个选项卡分隔的文件,第一列作为某种索引:

$ echo -e "0\tabc\txyz\n1\twhatever ever\tfoobar\n0\t12f2\t1" > test.txt
$ cat test.txt 
0   abc xyz
1   whatever ever   foobar
0   12f2    1
我想提取第一列中索引为0或1的行

我可以用Python实现这一点:

$ python -c "print '\n'.join([line.strip() for line in open('test.txt') if line.split('\t')[0] == '0'])"

0   abc xyz
0   12f2    1
但是sed/awk(或任何unix工具)与短python脚本的等价物是什么呢?

使用sed:

sed '/^0\t/!d' test.txt
对于sed:

sed '/^0\t/!d' test.txt

获取以
0
开头的所有行:

grep '^0' file

获取以
0
1
开头的所有行:

grep '^\(0\|1\)' file

获取以
0
开头的所有行:

grep '^0' file

获取以
0
1
开头的所有行:

grep '^\(0\|1\)' file

Awk
版本:

  • 对于前导0行:

    awk'/^0/'

  • 对于前1行:

    awk'/^1/'

Sed
版本:

  • 对于前导0行:

    sed-n'/^0/p'

  • 对于前1行:

    sed-n'/^1/p'


    • Awk
      版本:

      • 对于前导0行:

        awk'/^0/'

      • 对于前1行:

        awk'/^1/'

      Sed
      版本:

      • 对于前导0行:

        sed-n'/^0/p'

      • 对于前1行:

        sed-n'/^1/p'


      这里的所有其他答案都使用正则表达式,并且存在匹配“01”、“11”、“12”等的问题。使用
      awk
      ,您可以测试字符串是否相等:

      awk '$1 == 0' test.txt
      awk '$1 == 1' test.txt
      awk '$1 <= 1' test.txt
      
      awk'$1==0'test.txt
      awk'$1==1'test.txt
      
      awk'$1这里的所有其他答案都使用正则表达式,并且存在匹配“01”、“11”、“12”等的问题。使用
      awk
      ,您可以测试字符串是否相等:

      awk '$1 == 0' test.txt
      awk '$1 == 1' test.txt
      awk '$1 <= 1' test.txt
      
      awk'$1==0'test.txt
      awk'$1==1'test.txt
      
      awk'$1如果我们要查找第一列特别是0或1的记录:

      首先是一些测试材料:

      $ cat file
      0 yes sir
      1 yes sir
      10 nope
      01 nope
      00 nope
      
      在awk中:

      $ awk '$1 == "1" || $1 == "0"' file
      0 yes sir
      1 yes sir
      
      这些将失败:

      $ awk '$1 == 0' file
      0 yes sir
      00 nope
      $ awk '$1 == 1' file
      1 yes sir
      01 nope
      

      如果我们要查找第一列具体为0或1的记录:

      首先是一些测试材料:

      $ cat file
      0 yes sir
      1 yes sir
      10 nope
      01 nope
      00 nope
      
      在awk中:

      $ awk '$1 == "1" || $1 == "0"' file
      0 yes sir
      1 yes sir
      
      这些将失败:

      $ awk '$1 == 0' file
      0 yes sir
      00 nope
      $ awk '$1 == 1' file
      1 yes sir
      01 nope
      

      awk'$1==0'test.txt
      @jordanm你应该发布这是正确的awk答案。如果第一个字段是
      00
      01
      (然后是一些),它就会失败。
      awk'$1==0'test.txt
      @jordanm你应该发布这是正确的awk答案。如果第一个字段是
      00
      01
      (然后是一些),它就会失败。此匹配可能不需要的
      11
      。此匹配可能不需要的
      11
      。如果第一个字段为
      00
      01
      ,这些也将失败。请参阅我的答案。如果第一个字段是
      00
      01
      ,这些也将失败。看看我的答案。