[英]Splitting data field into two columns & Inserting it to an existing table (MYSQL)
[英]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.