HK2432
HK2432

Reputation: 133

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 ?

Upvotes: 0

Views: 1061

Answers (4)

Shawn
Shawn

Reputation: 52439

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 instead and I bet it'll work.

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

Upvotes: 1

KamilCuk
KamilCuk

Reputation: 141165

You could save first line using shell, and invoke sed on the rest:

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

Upvotes: 0

Sundeep
Sundeep

Reputation: 23667

Use awk:

$ awk -v q="''" 'NR>1{sub(/xxxx/, q)} 1' ip.txt
xxxx aaaaaaaaaaaaaaaaaaa
'' bbbbbbbbbbbbbbbbbbb
'' ccccccccccccccccccc
'' ddddddddddddddddddd
'' eeeeeeeeeeeeeeeeeee
  • -v q="''" 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

Upvotes: 0

dstromberg
dstromberg

Reputation: 7177

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.

Upvotes: 1

Related Questions