Shell脚本,将字符串拆分为最后一个空白字符前后的所有内容

时间:2014-07-22 15:15:24

标签: regex string awk whitespace solaris

我在shell脚本中分离字符串时遇到了一些问题。我一直在尝试使用RegEx,perl,awk,grep等在线发现的类似代码...但我似乎无法获得所需的结果。

基本上我有很多字符串。大多数都采用以下格式: 长串,空格,数字,例如。

Something!Something_Something_@Something_Something 10

然而,少数不是一个字符串(它们应该是!)但是它们有空格而不是下划线,例如。

Something!Something_Something_@Something Something 10

Something!Something - Something_@Something Something 10

然后将每个字符串格式化如下:

... |awk '{printf "%-100s %10d\n", $1, $2}' > file.out

打印不包含空格的字符串的正确结果

Something!Something_Something_@Something_Something                  10

然而,在第一个例子的情况下,由于空格分隔符,它只打印以下内容:

Something!Something_Something_@Something                        10

所以基本上我需要一种方法来拉出最后一个“”空间之前的所有内容,并在awk printf语句中将其分配给$ 1。任何帮助将不胜感激!!!

顺便说一句,它是Solaris 5.10服务器。

1 个答案:

答案 0 :(得分:0)

Hackjob但这会起作用

awk '{x=$NF;NF--;printf "%-100s %10d\n", $0, x}'
相关问题