首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用AWK从文件中连续输出行

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

Stack Overflow用户
提问于 2020-09-15 12:58:17
回答 2查看 130关注 0票数 2

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

代码语言:javascript
复制
#!/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的输入文件

代码语言:javascript
复制
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

然后我希望输出文件如下所示

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2020-09-15 13:15:53

您可以尝试使用GNU awk编写和测试下面的示例吗?其中需要在lines_from变量中提到需要打印的所有行,然后有一个名为till_lines的变量,它告诉我们需要从特定行打印多少行(例如--> from from line print next 4行也是如此)。另外,我测试了OP的代码,它对我来说工作得很好,它用new_file生成输出文件,因为在bash循环中调用awk不是一种好的做法,因此在这里添加这也是一个改进。

代码语言:javascript
复制
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

当我看到output_file的内容时,我可以看到以下内容:

代码语言:javascript
复制
cat output_file
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

解释:对以上内容增加了详细解释。

代码语言:javascript
复制
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.
票数 2
EN

Stack Overflow用户

发布于 2020-09-15 19:40:23

另一个awk变体

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63895366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档