Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何使用AWK从文件中连续输出行_Linux_Bash_Shell_Awk - Fatal编程技术网

Linux 如何使用AWK从文件中连续输出行

Linux 如何使用AWK从文件中连续输出行,linux,bash,shell,awk,Linux,Bash,Shell,Awk,我有一个有多行的文件,我想连续输出文件的一些行,例如第一次,从第1行打印到第5行,下一次,从第2行打印到第6行,等等。 我发现AWK是一个非常有用的函数,我试着自己编写代码,但它什么也不输出。 下面是我的代码 #!/bin/bash for n in `seq 1 3` do N1=$n N2=$((n+4)) awk -v n1="$N1" -v n2="$N2" 'NR == n1, NR == n2 {print $0}' my_file

我有一个有多行的文件,我想连续输出文件的一些行,例如第一次,从第1行打印到第5行,下一次,从第2行打印到第6行,等等。 我发现AWK是一个非常有用的函数,我试着自己编写代码,但它什么也不输出。 下面是我的代码

#!/bin/bash
for n in `seq 1 3`
do
  N1=$n
  N2=$((n+4))
  awk -v n1="$N1" -v n2="$N2" 'NR == n1, NR == n2 {print $0}' my_file >> new_file
done
例如,我有一个名为my_file的输入文件

1 99 tut
2 24 bcc
3 32 los
4 33 rts
5 642 pac
6 23 caas
7 231 cdos
8 1 caee
9 78 cdsa
然后我希望输出文件如下所示

1 99 tut
2 24 bcc
3 32 los
4 33 rts
5 642 pac
2 24 bcc
3 32 los
4 33 rts
5 642 pac
6 23 caas
3 32 los
4 33 rts
5 642 pac
6 23 caas
7 231 cdos

请您尝试使用GNU
awk
中显示的样本编写并测试以下内容。如果需要提到所有需要从变量中打印的行,那么有一个名为直到行的变量,它告诉我们需要从特定行打印多少行(例如-->从第1行打印接下来的4行)。另一方面,我已经测试了OP的代码,它对我来说工作得很好,因为在bash循环中调用
awk
,所以它使用新的_文件生成输出文件不是一个好的做法,因此在这里添加它也是一种改进

awk -v lines_from="1,2,3" -v till_lines="4" '
BEGIN{
  num=split(lines_from,arr,",")
  for(i=1;i<=num;i++){ line[arr[i]] }
}
FNR==NR{
  value[FNR]=$0
  next
}
(FNR in line){
  print value[FNR] > "output_file"
  j=""
  while(++j<=till_lines){ print value[FNR+j] > "output_file" }
}
'  Input_file  Input_file
说明:添加上述内容的详细说明

awk -v lines_from="1,2,3" -v till_lines="4" '    ##Starting awk program from here and creating 2 variables lines_from and till_lines here, where lines_from will have all line numbers which one wants to print from. till_lines is the value till lines one has to print.
BEGIN{                                           ##Starting BEGIN section of this program from here.
  num=split(lines_from,arr,",")                  ##Splitting lines_from into arr with delimiter of , here.
  for(i=1;i<=num;i++){                           ##Running a for loop from i=1 to till value of num here.
    line[arr[i]]                                 ##Creating array line with index of value of array arr with index of i here.
  }
}
FNR==NR{                                         ##Checking condition FNR==NR which will be TRUE when 1st time Input_file is being read.
  value[FNR]=$0                                  ##Creating value with index as FNR and its value is current line.
  next                                           ##next will skip all further statements from here.
}
(FNR in line){                                   ##Checking condition if current line number is coming in array then do following.
  print value[FNR] > "output_file"               ##Printing value with index of FNR into output_file
  j=""                                           ##Nullifying value of j here.
  while(++j<=till_lines){                        ##Running while loop from j=1 to till value of till_lines here.
    print value[FNR+j] > "output_file"           ##Printing value of array value with index of FNR+j and print output into output_file
  }
}
'  Input_file Input_file                         ##Mentioning Input_file names here.
awk-v lines_from=“1,2,3”-v till_lines=“4”###从此处启动awk程序,并在此处创建两个变量line_from和till_line,其中line_from将具有要打印的所有行号。till_lines是必须打印的till lines值。
开始{##从这里开始本程序的开始部分。
num=split(行_-from,arr,“,”)35;#将行_-from拆分为arr,分隔符为。
对于(i=1;i“输出_文件”##将索引为FNR的值打印到输出_文件中
j=”“##在这里使j的值无效。
而(++j“output_file”##打印索引为FNR+j的数组值的值,并将输出打印到output_file中
}
}
“输入文件输入文件”在此处提及输入文件名。

另一种
awk
变体

awk '
BEGIN {N1=1; N2=5}
arr[NR]=$0 {}
END {
    while (arr[N2]) {
        for (i=N1; i<=N2; i++)
            print arr[i] 
        N1++
        N2++
    }
}
' file
awk'
开始{N1=1;N2=5}
arr[NR]=$0{}
结束{
while(arr[N2]){

对于(i=N1;我希望以代码的形式展示您的努力。我想您可以在一个
awk
中完成这项工作。您可以发布您的输入和预期输出的示例吗?为了更好地理解这里的问题,我们可以在一个
awk
中完成这项工作(如果可能的话)太好了。嗨@RavinderSingh13我刚刚编辑了它,希望现在更清楚。谢谢。我尝试了你的代码,新文件中的输出看起来是正确的。@Barmar我换了另一台计算机,现在我的代码也可以工作了。谢谢你的评论,它提醒我这个问题可能与系统有关。@RavinderSingh13谢谢你的努力,我解决了我的问题换到另一台计算机时出现的问题,似乎应该归咎于系统。您的脚本和注释非常有教育意义,谢谢。@witt.S,不客气,我想在这里指出一点。老实说,您应该避免使用
for
循环,然后在循环中调用
awk
。在每次迭代中调用
awk
for-loop是一个过度的IMHO,如果可能的话,您可以使用这个单一的解决方案。
awk '
BEGIN {N1=1; N2=5}
arr[NR]=$0 {}
END {
    while (arr[N2]) {
        for (i=N1; i<=N2; i++)
            print arr[i] 
        N1++
        N2++
    }
}
' file