user3299633
user3299633

Reputation: 3380

sed isn't working when it's piped from another sed command

I'm trying to prepare my output for a grep expression, but when I try to modify the data to get it in the format I want I'm having issues getting it the way I want.

I'm using the following command to get a list of IP addresses that I need.

PRIV_IP=$(aws ec2 describe-instances  \
     --region "${REGION}" \
     --output text \
     --query 'Reservations[].Instances[].[PrivateIpAddress]' \
     --filters Name=tag:TagA,Values="${TagAData}" \
               Name=tag:TagB,Values="HOME" \
               Name=tag:TagC,Values="MAIN" | sed 's/\./-/g' | sed 's/ /\\|/g')

This is the output of the command; it ignores the last sed statement.

echo $PRIV_IP
1-2-3-4 5-6-7-8 9-10-11-12

If I perform the sed manually it works as intended.

echo $PRIV_IP | sed 's/ /\\|/g'
1-2-3-4\|5-6-7-8\|9-10-11-12

Can someone provide some input on what I'm doing incorrectly?

Upvotes: 0

Views: 111

Answers (1)

Stefan Becker
Stefan Becker

Reputation: 5962

It could be that your real command prints TABs but in your test they got converted to space already, e.g.

$ echo -e "A\tB"
A       B
$ echo -e "A\tB" | sed -e 's/ /X/g'
A       B
$ a=$(echo -e "A\tB"); echo $a
A B
$ echo $a | sed -e 's/ /X/g')
AXB

Solution: replace all white space as suggested by the comments, i.e.

$ echo -e "A\tB" | sed -e 's/[[:space:]]/X/g'
AXB

Upvotes: 1

Related Questions