awk命令打印但不按顺序打印?

时间:2017-03-13 17:15:03

标签: awk

我使用这个awk命令

"fechaName": "1","firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"fechaName": "2","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "3","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "4","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "555", "xxxxx": "John","xxxxx": "John",
"fechaName": "7","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
"fechaName": "6","firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"fechaName": "3","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",


 awk  -v OFS='"' -v   FS='Name": "'     '{ for( i=0; i<=4; i++ ) if( match($2,i) ) print $0 }'   sumacomando

并输出此


"fechaName": "1","firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"fechaName": "2","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "3","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "4","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "555", "xxxxx": "John","xxxxx": "John",
"fechaName": "3","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",

我期待这个


"fechaName": "1","firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"fechaName": "2","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "3","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
"fechaName": "3","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "4","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "555", "xxxxx": "John","xxxxx": "John",


我能解决这个问题

请帮帮我

4 个答案:

答案 0 :(得分:2)

根据给定的输入,下方应该有效,无需调用match函数并设置OFS

awk -F'"' '$4 <= 4' file  | sort -t'"' -nk4

<强>解释

  • 将字段sep设置为"
  • 如果第4个字段小于或等于4则打印文件的当前记录
  • 按第4个字段排序,其中字段分隔符为"

<强>输入

$ cat file
"fechaName": "1","firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"fechaName": "2","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "3","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "4","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "555", "xxxxx": "John","xxxxx": "John",
"fechaName": "7","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
"fechaName": "6","firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"fechaName": "3","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",

<强>输出

$ awk -F'"' '$4 <= 4' file  | sort -t'"' -nk4 
"fechaName": "1","firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"fechaName": "2","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "3","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "3","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
"fechaName": "4","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "555", "xxxxx": "John","xxxxx": "John",

答案 1 :(得分:2)

目前还不清楚你的真正目标是什么。您是否试图找到fechaName小于5的所有行并对它们进行排序?如果是这样的话,那就是:

$ awk -F'"' -v OFS='\t' '$4<5{print $4, NR, $0}' file | sort -k1,2n | cut -f3-
"fechaName": "1","firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"fechaName": "2","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "3","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "3","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
"fechaName": "4","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "555", "xxxxx": "John","xxxxx": "John",

以上内容适用于任何POSIX awk,sort和cut。

在一个awk脚本中完全按照你想要的那样做:

$ cat tst.awk
BEGIN { FS = "\"" }
$4 < 5 {
    if ( !seen[$4]++ ) {
        keys[++numKeys] = $4
    }
    keyLines[$4,++numLines[$4]] = $0
}
END {
    for (keyNr=1; keyNr<=numKeys; keyNr++) {
        key = keys[keyNr]
        for (lineNr=1; lineNr<=numLines[key]; lineNr++) {
            print keyLines[key,lineNr]
        }
    }
}

$ awk -f tst.awk file
"fechaName": "1","firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"fechaName": "2","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "3","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "3","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
"fechaName": "4","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "555", "xxxxx": "John","xxxxx": "John",

以上内容适用于任何现代awk,包括所有POSIX awks,mawk,gawk,tawk和nawk。唯一不利的是它将所有匹配行存储在内存中。

答案 2 :(得分:1)

尝试:

 awk -F'[": ,]' '$6 <= 4' Input_file | sort -k2

说明:我使用awk的-F选项将字段分隔符设置为(“:,space),然后我检查字段6th(如果你默认考虑awk的字段,它将是第2个字段)分隔符作为空格)等于0或1或2或3或4,如果此条件为TRUE则它将打印当前行(awk工作条件然后动作模式,所以这里提到条件但是我没有提到任何动作,所以默认打印将发生当前行),然后我根据第二个字段对它的输出进行排序。

答案 3 :(得分:-1)

好吧;我不能使用awk简单命令而不用外部来做这个问题 命令这可以使用关联数组,但它有点复杂但是简单而外部命令适合我

x=1 


while [ $x -le 8 ]; do   awk -v     OFS='"' -v   FS='Name": "'   -v x=$x     'match($2,x)  {  print $0}'    sumacomando >> sumacomando4   ;x=$(( $x + 1 )); done

任何语言都包含句子