简体   繁体   English

AIX 的等效 sed linux 命令

[英]Equivalent sed linux command for AIX

I've this type of data :我有这种类型的数据:

xxxx aaaaaaaaaaaaaaaaaaa
xxxx bbbbbbbbbbbbbbbbbbb
xxxx ccccccccccccccccccc
xxxx ddddddddddddddddddd
xxxx eeeeeeeeeeeeeeeeeee

I want this output :我想要这个输出:

xxxx aaaaaaaaaaaaaaaaaaa
'' bbbbbbbbbbbbbbbbbbb
'' ccccccccccccccccccc
'' ddddddddddddddddddd
'' eeeeeeeeeeeeeeeeeee

On linux I can do :在 linux 上我可以这样做:

sed "1 ! s|xxxx|''|" data.txt

But when I try this on AIX :但是当我在 AIX 上尝试这个时:

sed: 0602-403 1 ! s|xxx|''| is not a recognized function.

Could you help me ?你可以帮帮我吗 ?

It's been a long time since I've done any AIX.我已经很久没有做过 AIX 了。

It might be easier to install GNU sed for your AIX system(s), especially if you think you'll be porting Linux shell code a lot.为您的 AIX 系统安装 GNU sed 可能更容易,特别是如果您认为您将大量移植 Linux shell 代码。

Also, it might work to use two sed commands chained using the shell, like:此外,使用 shell 链接的两个 sed 命令可能会起作用,例如:

sed -n "1p" < input
sed -n "2,$ s|xxxx|''|p" < input

I've not tested that on an AIX system, mind you.请注意,我没有在 AIX 系统上测试过。 It's just a guess.这只是一个猜测。

From the POSIX specification for sed (Emphasis added):来自sedPOSIX 规范(添加了重点):

A function can be preceded by a '!'函数前面可以有一个 '!' character, in which case the function shall be applied if the addresses do not select the pattern space.字符,在这种情况下,如果地址未选择模式空间,则应应用该功能。 Zero or more <blank> characters shall be accepted before the '!' '!' 前应接受零个或多个<blank>字符character.特点。 It is unspecified whether <blank> characters can follow the '!'未指定<blank>字符是否可以跟在 '!' 之后character, and conforming applications shall not follow the '!'字符,并且符合要求的应用程序不应跟随“!” character with <blank> characters.带有<blank>字符的字符。

I suspect you're running into that.我怀疑你遇到了这个问题。 GNU sed allows spaces between, AIX sed probably doesn't. GNU sed之间允许有空格,而 AIX sed可能不允许。

Use sed "1!s|xxxx|''|" data.txt使用sed "1!s|xxxx|''|" data.txt sed "1!s|xxxx|''|" data.txt instead and I bet it'll work. sed "1!s|xxxx|''|" data.txt代替,我敢打赌它会起作用。

I do think the sed "2,$ s/xxxx/''/" data.txt suggested in a comment is clearer, though.不过,我确实认为评论中建议的sed "2,$ s/xxxx/''/" data.txt更清楚。

Use awk :使用awk

$ awk -v q="''" 'NR>1{sub(/xxxx/, q)} 1' ip.txt
xxxx aaaaaaaaaaaaaaaaaaa
'' bbbbbbbbbbbbbbbbbbb
'' ccccccccccccccccccc
'' ddddddddddddddddddd
'' eeeeeeeeeeeeeeeeeee
  • -vq="''" creates variable q with value '' -vq="''"使用值''创建变量q
  • NR>1 if line number is greater than 1如果行号大于NR>1NR>1 1
  • sub(/xxxx/, q) replace xxxx with content of q variable for input line sub(/xxxx/, q)用输入行的q变量的内容替换xxxx
    • if you always need to replace entire first field, you can use $1=q instead of sub here如果你总是需要替换整个第一个字段,你可以在这里使用$1=q而不是sub
  • 1 print contents of input line, which is present in $0 special variable 1打印输入行的内容,存在于$0特殊变量中

您可以使用 shell 保存第一行,并在其余部分调用sed

{ IFS= read -r firstline; printf "%s\n" "$firstline"; sed "s|xxxx|''|"; } <input

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM