简体   繁体   English

awk和字段分割参数

[英]awk and field splitting parameters

i have a file like this 我有一个像这样的文件

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"

what i need is to input this info into a database, so i need to separate the fields. 我需要的是将此信息输入数据库,所以我需要分隔字段。 the logic is that the field starts with some text(field name) and ends after the 2nd " desired output of 1st line using | as field separator (anything would do) 逻辑是该字段以某些文本(字段名称)开始,并在第一行的第二个所需输出之后使用|作为字段分隔符结束(任何操作)

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

try to use awk awk -v OFS="|" '{$1=$1}1' 尝试使用awk awk -v OFS="|" '{$1=$1}1' awk -v OFS="|" '{$1=$1}1' but it splits on every space awk -v OFS="|" '{$1=$1}1'但会在每个空格上分割

how can i achieve this (awk, sed or anything else to compile a automated script...) 我如何才能做到这一点(awk,sed或其他任何方式来编译自动化脚本...)

This might work for you (GNU sed): 这可能对您有用(GNU sed):

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

This replaces space(s) following a field by a | 这用|代替字段后的空格| globally throughout the file. 在整个文件中全局。

通过以下方式使用GNU awk:

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

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

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