如何添加和排序特定的文本行?

时间:2018-06-18 06:26:24

标签: linux perl awk

我有2个文件:

  • first.txt
  • second.txt

first.txt包含:

A
B
C
D
A
B
C
D

second.txt包含:

1 header
123
456
2 header
123
1 header
123
2 header
123
456
  

如何添加和排序second.txt的每1个标题123到2标题123到每个   第一个.txt的ABCD如下:

A
B
C
D
1 header
123
456
2 header
123
A
B
C
D
1 header
123
2 header
123
456

我尝试使用cat first.txt second.txt,但它只输出如下:

A
B
C
D
A
B
C
D
1 header
123
456
2 header
123
1 header
123
2 header
123
456

你们有什么想法吗? 这些是样本问题,真正的问题有数百万行的文本行,由于敏感的数据集我只能共享样本问题。

谢谢,
上午

2 个答案:

答案 0 :(得分:1)

然后它会很简单:

BUFF=`sed -n '1,4p' first.txt`; awk -v buff="$BUFF" '!/^1 header$/{print}/^1 header$/{print buff;print}' second.txt
A
B
C
D
1 header
123
456
2 header
123
A
B
C
D
1 header
123
2 header
123
456

使用sed -n '1,4p'将第4行存储在变量中。然后,使用语法awk将该变量内容传递给-v buff="$BUFF"sed程序的核心将读取第二个文件,并且当您到达内容为1 header的行时,对于每行不包含1 header打印行的行,然后打印在打印特定行之前,使用sed命令提取的4行。

答案 1 :(得分:1)

请您试着跟随并告诉我这是否对您有帮助。

awk '
FNR==NR{
  if(FNR%4==0 && FNR>1){
     a[++i]=val ORS $0;
     val="";
     next};
  val=val?val ORS $0:$0;
  next
}
count==3{
  print a[++j] ORS val;
  count="";
  val=""}
/header/{
  count++}
{
  val=val?val ORS $0:$0
}
END{
  if(count){
    print a[++j] ORS val}
}' first.txt second.txt

输出如下。

A
B
C
D
1 header
123
456
2 header
123
A
B
C
D
1 header
123
2 header
123
456

说明: 现在也添加上述代码的说明。

awk '
FNR==NR{                 ##Checking condition if FNR value is eqaul to NR value which will be TRUE when first Input_file is being read.
  if(FNR%4==0 && FNR>1){ ##Checking condition if line is completly divided by 4 and NOT the first line then do following.
     a[++i]=val ORS $0;  ##Creating an array named a whose index is variable i increasing value and value is variable val value along with new line and current line.
     val="";             ##Nullifying the variable val here.
     next};              ##Using next keyword to skip al next statements here.
  val=val?val ORS $0:$0; ##Creating variable named val whose value is concatenating its own value in it.
  next                   ##Using next keyword to skip all further statements from here now.
}
count==3{                ##Checking condition if variable named count is 3 then do following.
  print a[++j] ORS val;  ##Printing value of array a whose index is variable j with increasing value of 1 in it then ORS and value of variable val here.
  count="";              ##Nullifying the variable count here.
  val=""}                ##Nullifying the variable val here now.
/header/{                ##Checking condition if a line is having string header in it then do following.
  count++}               ##Increasing the value of variable count with 1 here.
{
  val=val?val ORS $0:$0  ##Creating variable named val whose value is concatenating its own values.
}
END{                     ##Starting END section here of awk.
  if(count){             ##Checking condition if variable count value is NOT NULL then do following.
    print a[++j] ORS val}##Printing value of array a whose index is variable j and ORS and then value of variable val here.
}' first.txt second.txt  ##Mentioning Input_file(s) named first.txt and second.txt here.