示例文件
aaa [bbb bb] ccc "ddd dd" eee
bbb [ccc cc] ddd "eee ee" fff
预期:
line1
s1="aaa", s2="bbb bb", s3="ccc", s4="ddd dd", s5="eee"
line2
s1="bbb", s2="ccc cc", s3="ddd", s5="eee ee", s5="fff"
谢谢!
答案 0 :(得分:1)
使用gnu awk
可以使用:
awk -v OFS=", " -v FPAT='\\[[^]]*\\]|"[^"]*"|[^[:space:]]+' '{
for (i=1; i<=NF; i++) {
gsub(/^[["]|[]"]$/, "", $i)
$i = "s" i "=\"" $i "\""
}
$0 = "line" NR ORS $0
} 1' file
输出:
line1
s1="aaa", s2="bbb bb", s3="ccc", s4="ddd dd", s5="eee"
line2
s1="bbb", s2="ccc cc", s3="ddd", s4="eee ee", s5="fff"
答案 1 :(得分:0)
仅限bash-
$: IFS=']"[' read -a line < infile # read the "groups"
$: line=( "${line[@]% }" ) # strip training spaces
$: line=( "${line[@]# }" ) # strip leading spaces
line
数组现在具有清理的数据。
分步显示-
$: IFS=']"[' read -a line < infile
$: printf "[%s]\n" "${line[@]}"
[aaa ]
[bbb bb]
[ ccc ]
[ddd dd]
[ eee]
$: line=( "${line[@]% }" )
$: printf "[%s]\n" "${line[@]}"
[aaa]
[bbb bb]
[ ccc]
[ddd dd]
[ eee]
$: line=( "${line[@]# }" )
$: printf "[%s]\n" "${line[@]}"
[aaa]
[bbb bb]
[ccc]
[ddd dd]
[eee]