简体   繁体   中英

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 -v OFS="|" '{$1=$1}1' but it splits on every space

how can i achieve this (awk, sed or anything else to compile a automated script...)

This might work for you (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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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