3

I have a Solaris machine (SunOSsu1a 5.10 Generic_142900-15 sun 4vsparcSUNW,Netra-T2000).

The following sed syntax removes all leading and trailing whitespace from each line (I need to remove whitespace because it causes application problems).

        sed 's/^[ \t]*//;s/[ \t]*$//' orig_file > new_file

But I noticed that sed also removes the "t" character from the end of each line.

Please advise how to fix the sed syntax/command in order to remove only the leading and trailing whitespace from each line (the solution can be also with Perl or AWK).

Examples (take a look at the last string - set_host)

1)

Original line before running sed command

   pack/configuration/param[14]/action:set_host

another example (before I run sed)

   +/etc/cp/config/Network-Configuration/Network-Configuration.xml:/cp-pack/configuration/param[8]/action:set_host

2)

the line after I run the sed command

   pack/configuration/param[14]/action:set_hos

another example (after I run sed)

   +/etc/cp/config/Network-Configuration/Network-Configuration.xml:/cp-pack/configuration/param[8]/action:set_hos
4

2 回答 2

5

我刚刚想到你可以使用一个字符类:

sed 's/^[[:space:]]*//;s/[[:space:]]*$//'

这发生在您的sed和 gnu sed 中,--posix因为(显然)posix 将 the 解释[ \t]为空格、 a\或 a t。您可以通过放置文字选项卡而不是 来解决此问题\t,最简单的方法可能是Ctrl+ V Tab。如果这不起作用,请将模式放入文件中(使用文字标签)并使用sed -f patterns.sed oldfile > newfile.

于 2012-05-16T15:48:26.780 回答
0

正如@aix 指出的那样,问题无疑是您sed不了解\t. 虽然 GNUsed有,但许多专有的 Unix 风格却没有。HP-UX 是其中之一,我相信 Solaris 也是。如果您无法安装 GNU sed,我会使用 Perl:

perl -pi.old -e 's{^\s+}{};s{\s+$}{}' file

...将修剪一个或多个前导空格 (^\s+) [空格和/或制表符] 以及尾随空格 (\s+$) 就地更新文件,将备份副本保留为“file.old”。

于 2012-05-16T16:37:58.080 回答