#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
любая помощь была бы оценена
Ответ №1:
Не могли бы вы попробовать следующий еще один способ сделать это в 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.
Ответ №2:
$ 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