Grep和管道命令提取数字

时间:2018-09-08 19:02:22

标签: linux shell

存储库包含文件trial.txt,其文本如下:

    Determining Coefficients
    Average Tractions
      trial 1 xyzt 1.987213
      trial 2 xyzt 1.131322
      trial 3 xyzt 1.123132
    trial for abc 1
      1.473248 2.027327
    trial for abc 2
      2.043960 2.043960
    trial for abc 3
      2.027327 1.473248

我正在尝试找到带有grep和管道以及正则表达式的单行表达式,该表达式将从文件中trial for abc #之后的第一行和第三行的数值中仅提取 。运行命令后的输出应为:

    1.473248 2.027327
    2.027327 1.473248

感谢任何人都可以提供帮助! 谢谢。

4 个答案:

答案 0 :(得分:1)

这对我有效,仅按您的要求使用grep和管道:

grep -A1 "trial for" "trial.txt" | grep -v "trial for"

它的作用是搜索单词“ trial for”,然后打印一行,然后将其传送到另一个grep,该行将删除包含“ trial for”的行,仅打印正确的数量/数字< / p>

答案 1 :(得分:0)

您不需要grep或任何管道处理...

sed -ne '/trial for abc [0-9]\+/{n;p}' trial.txt

-n选项停止sed自动打印每一行,并且对于与正则表达式匹配的每一行,将运行命令npn将下一行读入sed的模式空间,然后p将其打印出来。

答案 2 :(得分:0)

这对我有用

#
  

“ ^”标记行的开头。

     

“ [.0-9] *”仅允许使用空格,句点和数字。

     

“ $”标记该行的结尾。

该表达式并不完美,一般会接受许多其他字符串,但是最简单的表达式将拒绝文本文件中所有不需要的行。

另一个版本,拒绝包含任何字母的所有行

➜  /tmp cat trial.txt | grep "^[ .0-9]*$"
      1.473248 2.027327
      2.043960 2.043960
      2.027327 1.473248

答案 3 :(得分:0)

Field_A

为我工作,其中class SQLAFilterConverter(BaseFilterConverter): """ Class for converting columns into a supported list of filters specific for SQLAlchemy. """ conversion_table = (('is_relation_many_to_one', [FilterRelationOneToManyEqual, FilterRelationOneToManyNotEqual]), ('is_relation_one_to_one', [FilterRelationOneToManyEqual, FilterRelationOneToManyNotEqual]), ('is_relation_many_to_many', [FilterRelationManyToManyEqual]), ('is_relation_one_to_many', [FilterRelationManyToManyEqual]), ('is_enum', [FilterEqual, FilterNotEqual]), ('is_text', [FilterStartsWith, FilterEndsWith, FilterContains, FilterEqual, FilterNotStartsWith, FilterNotEndsWith, FilterNotContains, FilterNotEqual]), ('is_binary', [FilterStartsWith, FilterEndsWith, FilterContains, FilterEqual, FilterNotStartsWith, FilterNotEndsWith, FilterNotContains, FilterNotEqual]), ('is_string', [FilterStartsWith, FilterEndsWith, FilterContains, FilterEqual, FilterNotStartsWith, FilterNotEndsWith, FilterNotContains, FilterNotEqual]), ('is_integer', [FilterEqual, FilterGreater, FilterSmaller, FilterNotEqual]), ('is_float', [FilterEqual, FilterGreater, FilterSmaller, FilterNotEqual]), ('is_numeric', [FilterEqual, FilterGreater, FilterSmaller, FilterNotEqual]), ('is_date', [FilterEqual, FilterGreater, FilterSmaller, FilterNotEqual]), ('is_boolean', [FilterEqual, FilterNotEqual]), ('is_datetime', [FilterEqual, FilterGreater, FilterSmaller, FilterNotEqual]), ) 将在锚行之后打印1行,而下一个 grep -A 1 'trial for abc' /tmp/1 | grep '^[0-9. ]+$' 将从结果中提取一条带有行号的行