Awk 如何在unix中将列转换为行?

Awk 如何在unix中将列转换为行?,awk,Awk,我的输入文件是: zoo1 ---- cat dog mouse zoo2 ---- lion tiger zebra 我希望我的输出文件为: cat,dog,mouse lion,tiger,zebra 你知道怎么做吗?在你问题中的例子中,这一行代码有效: awk -v RS= '/----/{next}{gsub(/\n/,",")}7' file 或者通过设置OFS和FS的: awk -v RS= -v OFS="," -F'\n' '/----/{next}$1=$1' f

我的输入文件是:

zoo1
----

cat
dog
mouse

zoo2
----

lion
tiger
zebra
我希望我的输出文件为:

cat,dog,mouse
lion,tiger,zebra

你知道怎么做吗?

在你问题中的例子中,这一行代码有效:

 awk -v RS= '/----/{next}{gsub(/\n/,",")}7' file
或者通过设置OFS和FS的

awk -v RS= -v OFS="," -F'\n' '/----/{next}$1=$1' file
小测试:

kent$  awk -v RS= '/----/{next}{gsub(/\n/,",")}7' f
cat,dog,mouse
lion,tiger,zebra



kent$  awk -v RS= -v OFS="," -F'\n' '/----/{next}$1=$1' f
cat,dog,mouse
lion,tiger,zebra

对于您问题中的示例,此一行代码有效:

 awk -v RS= '/----/{next}{gsub(/\n/,",")}7' file
或者通过设置OFS和FS的

awk -v RS= -v OFS="," -F'\n' '/----/{next}$1=$1' file
小测试:

kent$  awk -v RS= '/----/{next}{gsub(/\n/,",")}7' f
cat,dog,mouse
lion,tiger,zebra



kent$  awk -v RS= -v OFS="," -F'\n' '/----/{next}$1=$1' f
cat,dog,mouse
lion,tiger,zebra

带awk的单向开关

$ awk '!(NR%2){$1=$1;print}' FS='\n' OFS=',' RS= file
cat,dog,mouse
lion,tiger,zebra

带awk的单向开关

$ awk '!(NR%2){$1=$1;print}' FS='\n' OFS=',' RS= file
cat,dog,mouse
lion,tiger,zebra

您可以使用perl的段落模式来实现这一点:

$ perl -000 -ne 'next if /---/;print join(",",split(/\n/)),"\n"' file
cat,dog,mouse
lion,tiger,zebra
人力资源

-0[octal/hexadecimal]
     specifies the input record separator ($/) as an octal or hexadecimal number.
     If there are no digits, the null character is the separator.  Other switches
     may precede or follow the digits.  For example, if you have a version of 
     find which can print filenames terminated by the null character, you can say
     this:

            find . -name '*.orig' -print0 | perl -n0e unlink

     The special value 00 will cause Perl to slurp files in paragraph mode.  
     Any value 0400 or above will cause Perl to slurp files whole, but by 
     convention the value 0777 is the one normally used for this purpose.

您可以使用perl的段落模式来实现这一点:

$ perl -000 -ne 'next if /---/;print join(",",split(/\n/)),"\n"' file
cat,dog,mouse
lion,tiger,zebra
人力资源

-0[octal/hexadecimal]
     specifies the input record separator ($/) as an octal or hexadecimal number.
     If there are no digits, the null character is the separator.  Other switches
     may precede or follow the digits.  For example, if you have a version of 
     find which can print filenames terminated by the null character, you can say
     this:

            find . -name '*.orig' -print0 | perl -n0e unlink

     The special value 00 will cause Perl to slurp files in paragraph mode.  
     Any value 0400 or above will cause Perl to slurp files whole, but by 
     convention the value 0777 is the one normally used for this purpose.

tr
提供了一种非常简单易懂的方法

tr-s\\n“,”


tr
提供了一种非常简单易懂的方法

tr-s\\n“,”


代码7的意义是什么?它只是一个真实的值,以便打印记录吗?@JonathanLeffler,是的。一个非零的数字。我知道它是有效的;这是伟大的代码高尔夫;就我个人而言,我不认为随机常数在生产代码中有一席之地。我更喜欢看显式的
{print}
。虽然
1
也可以完成这项工作,而且是半常规的,但我不喜欢(半)常规。“我想我今天早上起床的时候是错的。@JonathanLeffler我同意你的看法。
7
的意义是什么?”?它只是一个真实的值,以便打印记录吗?@JonathanLeffler,是的。一个非零的数字。我知道它是有效的;这是伟大的代码高尔夫;就我个人而言,我不认为随机常数在生产代码中有一席之地。我更喜欢看显式的
{print}
。虽然
1
也可以完成这项工作,而且是半常规的,但我不喜欢(半)常规。“我想我今天早上起床的时候有点不对劲。”乔纳坦利弗勒:我同意你的看法。