使用awk命令向后显示行

使用awk命令向后显示行,awk,Awk,我对awk命令还比较陌生,还在使用它,我正在尝试显示一个文件的多行,比如说第3-5行,然后向后显示它。因此,对于给定的文件: Hello World How are you I love computer science, I am using awk, And it is hard. 它应该输出: science, computer love I awk, using am I hard. is it And 朝着正确方向迈出的任何一步都是有益的 您可以使用以下awk命令来实现您的目标:

我对awk命令还比较陌生,还在使用它,我正在尝试显示一个文件的多行,比如说第3-5行,然后向后显示它。因此,对于给定的文件:

Hello World
How are you
I love computer science,
I am using awk,
And it is hard.
它应该输出:

science, computer love I
awk, using am I
hard.  is it And

朝着正确方向迈出的任何一步都是有益的

您可以使用以下
awk
命令来实现您的目标:

输入:

$ cat text 
Hello World
How are you
I love computer science,
I am using awk,
And it is hard.
$ awk 'NR<3{print}NR>=3{for(i=0; i<NF; i++){printf "%s ",$(NF-i);} printf "\n";}' text                                                            
Hello World
How are you
science, computer love I 
awk, using am I 
hard. is it And
输出:

$ cat text 
Hello World
How are you
I love computer science,
I am using awk,
And it is hard.
$ awk 'NR<3{print}NR>=3{for(i=0; i<NF; i++){printf "%s ",$(NF-i);} printf "\n";}' text                                                            
Hello World
How are you
science, computer love I 
awk, using am I 
hard. is it And
对于要仅打印范围(3-5)的多行文件,请使用:


$awk'NR>=3&&NR您可以使用以下
awk
命令来实现您的目标:

输入:

$ cat text 
Hello World
How are you
I love computer science,
I am using awk,
And it is hard.
$ awk 'NR<3{print}NR>=3{for(i=0; i<NF; i++){printf "%s ",$(NF-i);} printf "\n";}' text                                                            
Hello World
How are you
science, computer love I 
awk, using am I 
hard. is it And
输出:

$ cat text 
Hello World
How are you
I love computer science,
I am using awk,
And it is hard.
$ awk 'NR<3{print}NR>=3{for(i=0; i<NF; i++){printf "%s ",$(NF-i);} printf "\n";}' text                                                            
Hello World
How are you
science, computer love I 
awk, using am I 
hard. is it And
对于要仅打印范围(3-5)的多行文件,请使用:


$awk'NR>=3&&NR下面的
awk
可能会对您有所帮助,我使用
start
end
变量仅获取OP需要打印的行

awk -v start=3 -v end=5 'FNR>=start && FNR<=end{for(;NF>0;NF--){printf("%s%s",$NF,NF==1?RS:FS)}}'   Input_file
解释:现在也将解释添加到解决方案中

awk -v start=3 -v end=5 '            ##Mentioning variables named start and end where start is denoting the starting line and end is denoting end line which we want to print.
FNR>=start && FNR<=end{              ##Checking condition here if variable FNR(awk out of the box variable) value is greater than or equal to variable start AND FNR value is less than or equal to end variable. If condition is TRUE then do following:
  for(;NF>0;NF--){                   ##Initiating a for loop which starts from value of NF(Number of fields, which is out of the box variable of awk) and it runs till NF is 0.
    printf("%s%s",$NF,NF==1?RS:FS)}  ##Printing value of NF(value of field) and other string will be either space of new line(by checking when field is first then print new line as print space).
}
' Input_file                         ##Mentioning Input_file name here.
awk-v start=3-v end=5'##提到名为start和end的变量,其中start表示起始行,end表示要打印的结束行。
FNR>=开始和&FNR0;NF--){##启动一个for循环,该循环从NF的值(字段数,它是awk的开箱即用变量)开始,一直运行到NF为0。
printf(“%s%s”,$NF,NF==1?RS:FS)}##打印NF的值(字段值)和其他字符串将是新行的空格(通过检查字段何时为第一个,然后将新行打印为打印空格)。
}
'输入文件###在此处提及输入文件名。

遵循
awk
可能会对您有所帮助,我使用
start
end
变量仅获取需要由OP打印的行

awk -v start=3 -v end=5 'FNR>=start && FNR<=end{for(;NF>0;NF--){printf("%s%s",$NF,NF==1?RS:FS)}}'   Input_file
解释:现在也将解释添加到解决方案中

awk -v start=3 -v end=5 '            ##Mentioning variables named start and end where start is denoting the starting line and end is denoting end line which we want to print.
FNR>=start && FNR<=end{              ##Checking condition here if variable FNR(awk out of the box variable) value is greater than or equal to variable start AND FNR value is less than or equal to end variable. If condition is TRUE then do following:
  for(;NF>0;NF--){                   ##Initiating a for loop which starts from value of NF(Number of fields, which is out of the box variable of awk) and it runs till NF is 0.
    printf("%s%s",$NF,NF==1?RS:FS)}  ##Printing value of NF(value of field) and other string will be either space of new line(by checking when field is first then print new line as print space).
}
' Input_file                         ##Mentioning Input_file name here.
awk-v start=3-v end=5'##提到名为start和end的变量,其中start表示起始行,end表示要打印的结束行。
FNR>=开始和&FNR0;NF--){##启动一个for循环,该循环从NF的值(字段数,它是awk的开箱即用变量)开始,一直运行到NF为0。
printf(“%s%s”,$NF,NF==1?RS:FS)}##打印NF的值(字段值)和其他字符串将是新行的空格(通过检查字段何时为第一个,然后将新行打印为打印空格)。
}
'输入文件###在此处提及输入文件名。
$cat tst.awk
NR>2和&NR0;我——){
printf“%s%s”,$i,(i>1?OFS:ORS)
}
}
$awk-f tst.awk文件
科学,计算机,我爱你
啊,我在用什么
坚固的是吗
$cat tst.awk
NR>2和&NR0;我——){
printf“%s%s”,$i,(i>1?OFS:ORS)
}
}
$awk-f tst.awk文件
科学,计算机,我爱你
啊,我在用什么
坚固的是吗


您实际尝试了什么<代码>正确方向上的任何步骤都将是有益的
不是一个有效的问题。我将“我正在尝试显示一个文件的多行,比如说第3-5行,并向后显示”粘贴到google中。第一个是这篇文章,第二个是提示,您需要awk的
NR
变量过滤掉行,并使用
NF
变量在字段上循环。您实际尝试了什么<代码>正确方向上的任何步骤都将是有益的
不是一个有效的问题。我将“我正在尝试显示一个文件的多行,比如说第3-5行,并向后显示”粘贴到google中。第一个是这篇文章,第二个是提示,你需要awk的
NR
变量过滤掉这些行,并使用
NF
变量在字段上循环。我相信OP不需要打印前2行,你也可以在你的文章中添加该解决方案(以及当前的nice解决方案)。编辑;-)谢谢你,祝你今天愉快!非常感谢您的反馈,我已经编辑了这篇文章,并用正确的语法替换了
printf
printf“%s”,$(NF-I)
我相信OP不需要打印前2行,您也可以在您的文章中添加该解决方案(以及当前的nice解决方案)。已编辑;-)谢谢你,祝你今天愉快!非常感谢您的反馈,我已经编辑了这篇文章,并用正确的语法替换了
printf
printf“%s”,$(NF-I)
哇,回答不错!我喜欢可以动态传递给
awk
和您的打印循环的范围!根据POSIX,非常干净的递减NF是未定义的行为,因此在某些AWK中,它将删除最后一个字段,在其他AWK中,它将保留整个记录,而其他AWK可以执行任何其他操作。@EdMorton,让我现在尝试修复它并返回给您/。另外,FS和RS是输入分隔符,在输出时,您应该使用OFS和OR。如果你的答案和我发布的一样,我会删除我的答案。你必须用-v选项定义一个结尾吗?仅尝试使用awk-vstart=2“{print$(NF-3)}”来获取标头后面的所有行,但它仍然捕获标头的计数。不是很大,只是不知道是否需要。我有一个情况,行数可能会超过当前的数字。哇好答案!我喜欢可以动态传递给
awk
和您的打印循环的范围!根据POSIX,非常干净的递减NF是未定义的行为,因此在某些AWK中,它将删除最后一个字段,在其他AWK中,它将保留整个记录,而其他AWK可以执行任何其他操作。@EdMorton,让我现在尝试修复它并返回给您/。另外,FS和RS是输入分隔符,在输出时,您应该使用OFS和OR。如果你的答案和我发布的一样,我会删除我的答案。你必须用-v选项定义一个结尾吗?仅尝试使用awk-vstart=2“{print$(NF-3)}”来获取标头后面的所有行,但它仍然捕获标头的计数。不是很大,只是不知道它