简体   繁体   中英

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 :

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

But when I try this on 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.

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.

Also, it might work to use two sed commands chained using the shell, like:

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

I've not tested that on an AIX system, mind you. It's just a guess.

From the POSIX specification for sed (Emphasis added):

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 '!' character. It is unspecified whether <blank> characters can follow the '!'character, and conforming applications shall not follow the '!' character with <blank> characters.

I suspect you're running into that. GNU sed allows spaces between, AIX sed probably doesn't.

Use sed "1!s|xxxx|''|" data.txt sed "1!s|xxxx|''|" data.txt instead and I bet it'll work.

I do think the sed "2,$ s/xxxx/''/" data.txt suggested in a comment is clearer, though.

Use awk :

$ awk -v q="''" 'NR>1{sub(/xxxx/, q)} 1' ip.txt
xxxx aaaaaaaaaaaaaaaaaaa
'' bbbbbbbbbbbbbbbbbbb
'' ccccccccccccccccccc
'' ddddddddddddddddddd
'' eeeeeeeeeeeeeeeeeee
  • -vq="''" creates variable q with value ''
  • NR>1 if line number is greater than 1
  • sub(/xxxx/, q) replace xxxx with content of q variable for input line
    • if you always need to replace entire first field, you can use $1=q instead of sub here
  • 1 print contents of input line, which is present in $0 special variable

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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