本文介绍了AWK字段分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件中的每一行都有一组键值对,以:"分隔

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

我在读取每一行后使用awk提取键值对,如下所述

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 ":" .

如何读取整个值

推荐答案

如果只想拆分第一个:,则使用 bash 字符串会更容易操作:

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


好的,因为您的外壳没有这个,您可以使用 sed :

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

第一个查找并删除冒号和行尾之间的所有内容,并且由于(大多数,包括 sed 的)正则表达式默认是贪婪的,因此正则表达式将是第一个冒号的所有内容上.第二个将删除直到第一个冒号为止的所有内容.

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.

这篇关于AWK字段分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 18:42