我想阅读一个文件,awk但我被困在第四个字段,它会在逗号后自动断开.
数据: - test.txt
"A","B","ls","This,is,the,test"
"k","O","mv","This,is,the,2nd test"
"C","J","cd","This,is,the,3rd test"
cat test.txt | awk -F , '{ OFS="|" ;print $2 $3 $4 }'
Run Code Online (Sandbox Code Playgroud)
产量
"B"|"ls"|"This
"O"|"mv"|"This
"J"|"cd"|"This
Run Code Online (Sandbox Code Playgroud)
但输出应该是这样的
"B"|"ls"|"This,is,the,test"
"O"|"mv"|"This,is,the,2nd test"
"J"|"cd"|"This,is,the,3rd test"
Run Code Online (Sandbox Code Playgroud)
任何的想法
使用awk,您还可以使用:
awk -F'\",\"' 'BEGIN{OFS="\"|\""}{print "\""$2,$3,$4}' filename
Run Code Online (Sandbox Code Playgroud)
注意:这只能在假设","在字符串之间找不到时才起作用.那就是它被用作字段分隔符.
输出:
"B"|"ls"|"This,is,the,test"
"O"|"mv"|"This,is,the,2nd test"
"J"|"cd"|"This,is,the,3rd test"
Run Code Online (Sandbox Code Playgroud)
要么
好一点:
awk -F'^\"|\",\"|\"$' 'BEGIN{OFS="\"|\""}{print "\""$3,$4,$5"\""}' filename
Run Code Online (Sandbox Code Playgroud)