我必须格式化文件的内容。因此,如果我在.csv文件中传递第一列-[成功] / [失败],第二列-* co.com,第三列-内容 只是想将内容添加到一行中,如下所示
[SUCCESS] abc.co.com
This is 1st content,,,,/
asdfmmmm
[SUCCESS] abcdd.co.com
This is 2nd content
cabjdhds
[SUCCESS] abcasd.co.com
This is 3rd content...?/
cajbhjwd b
[FAILURE] ab.co.com
This is 3rd content...?/
cajbhjwd b
预期产量
[SUCCESS], abc.co.com, This is 1st content,,,,/ asdfmmmm............
[SUCCESS], abcdd.co.com, This is 2nd content cabjdhds..........
[SUCCESS], abcasd.co.com, This is 3rd content...?/ cajbhjwd b.........
[FAILURE], ab.co.com, This is 3rd content...?/ cajbhjwd b
在下面尝试过,但是全部打印在同一行
cat file |awk 'BEGIN {accum_line = "";} /^[[a-z]+]/{if(length(accum_line)){print accum_line; accum_line = "";}} {accum_line = accum_line " ," $0;} END {if(length(accum_line)){print accum_line; }}'
答案 0 :(得分:1)
能否请您尝试以下步骤(已通过提供的示例进行了测试)。
awk '
BEGIN{
OFS=", "
}
/^\[/{
$1=$1
$0=$0","
if(val){
sub(/, $/,"",val)
print val}
val=""
}
{
val=(val?!/^\[/?val " ":val:"")$0
}
END{
if(val){
sub(/, $/,"",val)
print val
}
}
' Input_file
说明:现在为上面添加说明。
awk ' ##Starting awk program here.
BEGIN{ ##Starting BEGIN section of this code here.
OFS=", " ##Setting OFS as ", " for all lines here.
} ##Closing BEGIN block here.
/^\[/{ ##Checking condition ig a line starts from [ then do following.
$1=$1 ##Resetting $1 to value $1 to make OFS value in affect here.
$0=$0"," ##Concatenating , in current line.
if(val){ ##Checking if variable val is NOT NULL then do following.
sub(/, $/,"",val) ##Substituting , and space at last of line with NULL in variable val.
print val} ##Printing variable val here.
val="" ##Nullifying variable val here.
}
{
val=(val?!/^\[/?val " ":val:"")$0 ##Creating variable val and concatenating its value with its own previous value along with space if line DO NOT start from [.
}
END{ ##Starting END block of this awk code now.
if(val){ ##Checking if val is present then do following.
sub(/, $/,"",val) ##Substituting , and space at last of line with NULL in variable val.
print val ##Printing variable val here.
}
}
' Input_file ##Mentioning Input_file name here.
答案 1 :(得分:0)
根据是否要在每条输出行之间使用换行符,可以用几种不同的方法来完成此操作,例如
awk -F] -v n=0 '
NF==0 {n=0; print "\n"; next}
{ printf n ? ", %s" : "%s]", $1
for (i=2; i<=NF; i++)
printf n ? ", %s" : ",%s", $i
n++
}
END {print ""}
' file
在将']'
用作单独字段的情况下,用']'
分割唯一的行,然后保留一个计数器,并使用printf
附加每行,直到为空达到线。
使用/输出示例
保留每行输出之间的空行,您只需在命令行上复制/鼠标中键进行粘贴即可进行测试,例如
$ awk -F] -v n=0 '
> NF==0 {n=0; print "\n"; next}
> { printf n ? ", %s" : "%s]", $1
> for (i=2; i<=NF; i++)
> printf n ? ", %s" : ",%s", $i
> n++
> }
> END {print ""}
> ' file
[SUCCESS], abc.co.com, This is 1st content,,,,/, asdfmmmm
[SUCCESS], abcdd.co.com, This is 2nd content, cabjdhds
[SUCCESS], abcasd.co.com, This is 3rd content...?/, cajbhjwd b
[FAILURE], ab.co.com, This is 3rd content...?/, cajbhjwd b
消除其他逗号
如果您真的想累积组中的第一条记录以外的所有记录,则可以执行以下操作:
$ awk -F] -v n=0 -v acum="" -v OFS="" '
> NF==0 {n=0; print ",", acum, "\n"; acum=""; next}
> NF>1 {
> printf n ? ", %s" : "%s]", $1
> for (i=2; i<=NF; i++)
> printf n ? ", %s" : ",%s", $i
> n++
> }
> NF==1 { acum = acum " " $1; n++ }
> END {print ",", acum}
> ' file
[SUCCESS], abc.co.com, This is 1st content,,,,/ asdfmmmm
[SUCCESS], abcdd.co.com, This is 2nd content cabjdhds
[SUCCESS], abcasd.co.com, This is 3rd content...?/ cajbhjwd b
[FAILURE], ab.co.com, This is 3rd content...?/ cajbhjwd b
答案 2 :(得分:0)
通过将“记录选择器”设置为无RS=""
,然后重新创建记录{$1=$1}
,您可以像这样使用gnu awk
:
awk '{$1=$1","}1' RS="" OFS=" "
[SUCCESS], abc.co.com This is 1st content,,,,/ asdfmmmm
[SUCCESS], abcdd.co.com This is 2nd content cabjdhds
[SUCCESS], abcasd.co.com This is 3rd content...?/ cajbhjwd b
[FAILURE], ab.co.com This is 3rd content...?/ cajbhjwd b
如果记录之间需要空白行:
awk '{$1=$1","}1' RS="" OFS=" " ORS="\n\n" file
[SUCCESS], abc.co.com This is 1st content,,,,/ asdfmmmm
[SUCCESS], abcdd.co.com This is 2nd content cabjdhds
[SUCCESS], abcasd.co.com This is 3rd content...?/ cajbhjwd b
[FAILURE], ab.co.com This is 3rd content...?/ cajbhjwd b