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

awk命令打印,但不按顺序打印?,awk,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": "Jo

我使用这个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", 然后输出这个

"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", 我怎样才能解决这个问题

请帮我试试:

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

解释:我正在做字段分隔符:使用AWK的-F选项空间,然后我检查字段6th.如果是默认的AWK字段分隔符,空间等于0或1或2或3或4,如果该条件为真,那么它将打印当前的LangWAK作品,然后按动作模式打印,字段为第二。这里提到了条件,但我没有提到任何操作,因此默认情况下,当前行将打印,然后我将根据第二个字段对其输出进行排序。

在给定输入的情况下,下面应该可以工作,无需调用匹配函数和设置

输出


嗯,;我不能在没有外部命令的情况下使用awk简单命令来解决这个问题 命令这可以使用关联数组,但它有点复杂,但使用简单的while外部命令对我来说效果很好

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

无论如何,任何一种语言都包括句子,但你的真正目标仍然不清楚。您是否试图找到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、排序和切割

要完全在一个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 awk、mawk、gawk、tawk和nawk。唯一的缺点是它将所有匹配行存储在内存中。

我需要知道,因为当我按1到3的顺序打印时,awk没有按顺序打印,而是按1到4的顺序打印order@victorhernandezzero:显然,它不会按排序顺序打印,因为在最后第二行显示的输入文件中第二个字段是4,最后一行是3。因此,当您给定从0到4匹配的条件时,最后两行都将满足该条件,并且它将打印它们,如果在循环中只放入0到3,则最后一行的第二行将不满足条件,最后一行将位于3匹配行之后,表示距离最后一行的第三行,看起来它在对数据进行排序,而实际情况并非如此,它只是使用条件打印。如果这有帮助,请告诉我。是的,请帮助我,但我可以通过某种方式使用for语句按顺序打印此数据awk-v OFS=''-v FS='Name''{对于i=0;如果你不是在测试它是否等于0或1或2或3或4,你是在测试它是否包含这些数字中的任何一个,以便它与9627匹配,例如。@victorhernandezzero不确定你为什么仍然反对使用之前两次给出的健壮解决方案:和。这不是同一个问题请不要关闭有人o仅使用awk为不同的原因组织数据SAWK没有外部命令对于不正确打印的awk不一样您需要添加-s选项进行排序以保证保留输入顺序,但我认为它会变得特定于GNU。非常感谢您的回答我已经给了您一个投票结果是我想se nawk在windows、linux和mac上使用它,但不使用sort,因为这不像是将其本机传输到这三个平台。这就是为什么我选择第一个答案,因为我最初问我的命令出了什么问题,没有更好的答案是他唯一能做的事情。这是一个非常古老的、POSIX之前的awk。你确定吗你必须解决的问题是,你不能使用modern/POSIX awk?问题是,我无法让它仅使用cygwins在Windows上本机运行,这是一个问题。我大约99%确定存在gawk的本机Windows版本。mac也是如此。
$ 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",
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
$ 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",
$ 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",