Awk field separator

寵の児 提交于 2021-02-10 06:37:26

问题


I have a set of key value pairs in file on each line delimited by ":"

I am fetching the key value pairs using awk as mentioned below after reading each line

key=$(echo $LINE |  awk -F " *: *" '{print $1}')
value=$(echo $LINE |  awk -F " *: *" '{print $2}')

Problem is after if the value itself contains ":", it is further split and I will end up reading only value before ":" .

How can I read the entire value


回答1:


If you just want to split on the first :, it will be easier to use bash string manipulation:

key=${LINE%%:*}
value=${LINE#*:}

%% lops off the longest string that matches its regex (:*) from the back of the string, and # removes the shortest string that matches at the front of the string. e.g.

$ LINE="a:b:c"
$ key=${LINE%%:*}
$ value=${LINE#*:}
$ echo $key
a
$ echo $value
b:c

OK, since your shell doesn't have this, you can use sed:

key=$(echo "$LINE" | sed 's/:.*$//')
value=$(echo "$LINE" | sed 's/[^:]*://')

The first finds and removes everything between a colon and the end of the line, and since (most, including sed's) regexes are by default greedy, that will be everything from the first colon on. The second removes everything up to and including the first colon.




回答2:


You're always going to run into problems if a field contains your delimiter. If you know that a specific field always contains a delimiter, you can come up with a workaround where you replace that occurrence with sed first:

echo $LINE | sed 's/:/|/3' | awk ...

In the above example, if you knew that the third occurrence of : on a line was always there and within a field, then you could substitute it with | or something similar. But that's just one hack of a solution; the real answer is to change your delimiter, if possible, or write code to parse it that can take into account something like escapes (e.g. \:).




回答3:


Answering an old question, but using cut would make this very simple:

key=$(echo $LINE | cut -d: -f1)
value=$(echo $LINE | cut -d: -f2-)

-f2- means fields 2 until the end




回答4:


If your file contains the characters that are interpreted as delimiter symbols, then you will need to replace those characters with something else or choose a different delimiter.



来源:https://stackoverflow.com/questions/9296633/awk-field-separator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!