我从文本文件中得到以下输出,我需要格式化以便更具可读性。
julian text:case2345
maria text:case4567
clover text,text,text,text,text,text:case3456
neil text,text:case09876
我需要按如下方式重新格式化输出:
julian text:case2345
maria text:case4567
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
neil text:case09876
neil text:case09876
使用awk
我试图匹配模式案例[0-9],将其存储在变量中,然后使用分隔符“,”拆分行,最后打印。
我之前尝试过,但无法获得所需的输出
awk '/match($0,/case[0-9]/){val=substr($0,RSTART,RLENGTH);next}{split($2,k,","); for (i in k) {printf ("%s %s %s\n\n",$1,k[i],val)}}'
答案 0 :(得分:2)
只需调整the answer to your previous question:
$ awk -F'[ ,:]+' '{for (i=2;i<NF;i++) print $1, $i ":" $NF}' file
julian text:case2345
maria text:case4567
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
neil text:case09876
neil text:case09876
答案 1 :(得分:1)
# set field separator
awk -F '[: ]+' '/,/{ # if line/row/record contains comma
split($2,arr,/,/); # split 2nd field by comma,
# store elements in array arr
for(i=1; i in arr;i++) # iterate through array arr
print $1, arr[i] ":" $NF; # print 1st field, array element and last field from record
next # stop processing go to next line
# 1 at the end does default operation that is print $0
}1' infile
测试结果:
$ cat infile
julian text:case2345
maria text:case 4567
clover text,text,text,text,text,text:case3456
neil text,text:case09876
$ awk -F '[: ]+' '/,/{split($2,arr,/,/);for(i=1; i in arr;i++)print $1,arr[i]":"$NF;next}1' infile
julian text:case2345
maria text:case 4567
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
neil text:case09876
neil text:case09876
答案 2 :(得分:1)
关注awk
可能会有所帮助。 (考虑到你的实际Input_file是显示的样本)。
awk -F' +|,|:' '$NF~/[cC][aA][sS][eE]/ && NF>2{for(i=2;i<=(NF-1);i++){print $1 OFS $i":"$NF};next} 1' Input_file
现在也添加非单线形式的解决方案。
awk -F' +|,|:' '
$NF~/[cC][aA][sS][eE]/ && NF>2{
for(i=2;i<=(NF-1);i++){
print $1 OFS $i":"$NF};
next
}
1
' Input_file
说明: 现在也为代码添加说明。
awk -F' +|,|:' ' ##Setting field separator as space(s) OR comma OR colon here for each line.
$NF~/[cCaAsSeE]/ && NF>2{ ##Checking condition here if last field is having case OR CASE string in it and number of fields are more than 2.
for(i=2;i<=(NF-1);i++){ ##Starting a for loop which starts from 2nd value to second last value of total fields value here.
print $1 OFS $i":"$NF};##first field OFS(whose default value is space) value of current field and colon with last field of line.
next ##next is awk default keyword which will skip all further lines now.
}
1 ##Only those lines will come here which was NOT true for above conditions, simple printing of line will happen here.
' Input_file ##Mentioning Input_file name here.