从 Linux 终端使用 grep/awk/sed 命令从文件中读取值

时间:2021-05-04 18:02:40

标签: awk sed grep

我必须从它包含的文件 test.txt 中读取值

test.txt

parameters()->getParm (&randomNumberSeed, "-seed");
parameters()->getParm( &withInit, "-withInit");
parameters()->getParm( &useTotalSpace, "-useTotalSpace");
parameters ()->getParm (unitSize, "-unitSize");
parameters ()->getParm(&randomRaid,"-randomRaid");
parameters()->getParm(&spares, "-spares");
parameters()->getParm( &transferType, "-transferType");
parameters()->getParm(&withBmicEvents, "-withBmicEvents");
parameters()->getParm(&bmicSleepSeconds, "-bmicWaitTime");

int main (int argc, char *argv[])
{
   static beyond test (argc, argv);
   return test.start ();
}

我期望从文本文件中得到的结果是

expected output:-
 
randomNumberSeed  -seed
withInit          -withInit
useTotalSpace     -useTotalSpace
unitSize          -unitSize
randomRaid        -randomRaid
spares            -spares
transferType      -transferType
withBmicEvents    -withBmicEvents
bmicSleepSeconds  -bmicWaitTime

我正在使用此命令来获取我的结果......我不需要得到结果。请帮我得到我的结果。

cat test.txt | grep getParm | sed 's/getParm(/  /;s/&/  /;s/,/  /;s/\"/  /g' | awk  '{print  $2 "\t"  $3}'

(       randomNumberSeed
withInit        -withInit
useTotalSpace   -useTotalSpace
()->getParm     (unitSize
()->    randomRaid
spares  -spares
transferType    -transferType
withBmicEvents  -withBmicEvents
bmicSleepSeconds        -bmicWaitTime

4 个答案:

答案 0 :(得分:2)

根据您展示的样品,您可以尝试以下操作吗?

awk '
match($0,/getParm[[:space:]]+?\([[:space:]]+?&?[^,]*/){
  val=substr($0,RSTART,RLENGTH)
  sub(/.*[&(]/,"",val)
  match($0,/-[^"]*"\);$/)
  print val,substr($0,RSTART,RLENGTH-3)
}
' Input_file | column -t


或者,如果您的 Input_file 与所示示例完全相同,那么您可以简单地使用 sub(substitute) 函数。

awk '
{
  sub(/.*->getParm[[:space:]]+?\([[:space:]]+?&?/,"")
  gsub(/[[:space:]]+?"|"\);$/," ")
  print
}
' Input_file | column -t

第一个解决方案的说明: 为第一个解决方案添加详细说明。

awk '
##Starting awk program from here.
match($0,/getParm[[:space:]]+?\([[:space:]]+?&?[^,]*/){
##using match function to match regex getParm[[:space:]]+?\([[:space:]]+?&?[^,]*
  val=substr($0,RSTART,RLENGTH)
##Creating val which has sub string value of above matched regex.
  sub(/.*[&(]/,"",val)
##Substituting everything till & OR ( in val here with NULL.
  match($0,/-[^"]*"\);$/)
##Using match to match from - till " including " ); at end of line.
  print val,substr($0,RSTART,RLENGTH-3)
##Printing val and sub string as per requirement.
}
' Input_file | column -t
##Mentioning Input_file and sending it to column to get good form data in output.

答案 1 :(得分:2)

您可以使用此 sed 解决方案:

sed -nE '/>getParm/s/.*\([ &]*([_[:alnum:]]+), *"([^"]+)"\).*/\1 \2/p' file |
column -t

randomNumberSeed  -seed
withInit          -withInit
useTotalSpace     -useTotalSpace
unitSize          -unitSize
randomRaid        -randomRaid
spares            -spares
transferType      -transferType
withBmicEvents    -withBmicEvents
bmicSleepSeconds  -bmicWaitTime

正则表达式解释:

  • />getParm/:匹配包含 >getParm 字符串的行
  • .*:匹配 0 个或多个任意字符
  • \(:比赛开场(
  • [ &]*:匹配 0 个或多个空格或 &
  • ([_[:alnum:]]+):匹配并捕获第 1 组中 1 个以上的单词字符
  • , *":匹配逗号后跟 0 个或多个空格后跟一个 "
  • ([^"]+):匹配并捕获第 2 组中的 1+ 个非" 字符串
  • "\):匹配 ")
  • .*:匹配剩余的字符串直到结束

答案 2 :(得分:2)

使用 GNU awk 和 &,正则表达式 ( *&*" 作为字段分隔符。

awk -F '(&|,|\\( *&*|")' '{print $3,$5}' test.txt | column -t

输出:

randomNumberSeed  -seed
withInit          -withInit
useTotalSpace     -useTotalSpace
unitSize          -unitSize
randomRaid        -randomRaid
spares            -spares
transferType      -transferType
withBmicEvents    -withBmicEvents
bmicSleepSeconds  -bmicWaitTime

答案 3 :(得分:2)

$ awk -F'[()",& ]+' '/getParm/{print $3, $4}' file | column -t
randomNumberSeed  -seed
withInit          -withInit
useTotalSpace     -useTotalSpace
unitSize          -unitSize
randomRaid        -randomRaid
spares            -spares
transferType      -transferType
withBmicEvents    -withBmicEvents
bmicSleepSeconds  -bmicWaitTime
相关问题