繁体   English   中英

awk和字段分割参数

[英]awk and field splitting parameters

我有一个像这样的文件

fld1="the farm 10" fld3="the farm 1.0" img="https://urlshortener/45R6wmN.png" titlefld4="draw4"
fld1="testing explosives" fld3="testing explosives v15" img="https://urlshortener/45R6wmN.png" titlefld4="draw4"
fld1="law cases" fld3="law cases v5" img="https://urlshortener/45R6wmN.png" titlefld4="draw4"
fld1="history trails" fld3="history trails v4 " img="https://urlshortener/vrjnrethrt.png" titlefld4="draw4"
fld1="climbing dumber" fld3="climbing dumber v1.2" img="https://urlshortener/ervwyntuny.png" titlefld4="draw4"
fld1="pluming 4 dumbs" fld3="pluming 4 dumbs v2.0" img="https://urlshortener/rthvbyh.png" titlefld4="draw4"

我需要的是将此信息输入数据库,所以我需要分隔字段。 逻辑是该字段以某些文本(字段名称)开始,并在第一行的第二个所需输出之后使用|作为字段分隔符结束(任何操作)

fld1="the farm 10"|fld3="the farm 1.0"|img="https://urlshortener/45R6wmN.png" titlefld4="draw4"

尝试使用awk awk -v OFS="|" '{$1=$1}1' awk -v OFS="|" '{$1=$1}1'但会在每个空格上分割

我如何才能做到这一点(awk,sed或其他任何方式来编译自动化脚本...)

这可能对您有用(GNU sed):

sed -r 's/(\S+="[^"]*")\s+/\1|/g' file

这用|代替字段后的空格| 在整个文件中全局。

通过以下方式使用GNU awk:

awk 'BEGIN { FPAT="[^= ]+=\"[^\"]+\""; OFS="|" } { $1=$1 } 1'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM