AWK在不删除下一行的情况下加入下一行

AWK在不删除下一行的情况下加入下一行,awk,Awk,我正在使用以下命令连接csv中的下一行,但希望不删除这些行 awk 'NR%2{printf "%s, ",$0;next;}1' test.csv Test.csv rabbit cat dog turtle sheep cow 结果: rabbit, cat dog, turtle sheep, cow 预期结果: rabbit, cat cat, dog dog, turtle turtle, sheep sheep, cow cow, rabbit 如果您能在

我正在使用以下命令连接csv中的下一行,但希望不删除这些行

awk 'NR%2{printf "%s, ",$0;next;}1' test.csv
Test.csv

rabbit
cat
dog
turtle
sheep
cow
结果:

rabbit, cat
dog, turtle
sheep, cow
预期结果:

rabbit, cat
cat, dog
dog, turtle
turtle, sheep
sheep, cow
cow, rabbit

如果您能在GNU awk中提供更多帮助,我们将不胜感激,请您尝试以下方法。使用显示的样本进行书写和测试

$ awk -v OFS=', ' 'NR==1{first=$0} NR>1{print prev, $0} {prev=$0} END{print prev, first}' file
rabbit, cat
cat, dog
dog, turtle
turtle, sheep
sheep, cow
cow, rabbit
awk -v RS= -v OFS=', ' '
{
  for(i=1;i<=(NF-1);i++){
    print $i,$(i+1)
  }
}
END{
  print $NF,$1
}
' Input_file
说明:在此处添加详细说明

awk -v RS= -v OFS=', ' '       ##Starting awk program and setting RS as NULL here and setting OFS as comma with space here.
{
  for(i=1;i<=(NF-1);i++){      ##Starting for loop from here till 2nd last field.
    print $i,$(i+1)            ##Printing current field and next field here.
  }
}
END{                           ##Starting END block of this code here.
  print $NF,$1                 ##Printing last field and first field as per OP question here.
}
' Input_file                   ##Mentioning Input_file name here.

在GNU awk中,还有一种方法可以做到这一点,请尝试下面的方法。使用显示的样本进行书写和测试

awk -v RS= -v OFS=', ' '
{
  for(i=1;i<=(NF-1);i++){
    print $i,$(i+1)
  }
}
END{
  print $NF,$1
}
' Input_file
说明:在此处添加详细说明

awk -v RS= -v OFS=', ' '       ##Starting awk program and setting RS as NULL here and setting OFS as comma with space here.
{
  for(i=1;i<=(NF-1);i++){      ##Starting for loop from here till 2nd last field.
    print $i,$(i+1)            ##Printing current field and next field here.
  }
}
END{                           ##Starting END block of this code here.
  print $NF,$1                 ##Printing last field and first field as per OP question here.
}
' Input_file                   ##Mentioning Input_file name here.