0

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?

user3299633
  • 2,971
  • 3
  • 24
  • 38
  • What is the output of `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",` before `sed` – Jan Gassen Jan 31 '19 at 07:57
  • Just the IPs with .'s instead of -'s and spaces between them – user3299633 Jan 31 '19 at 07:59
  • What happens when you swap the two sed commands? They don't seem to depend on each other. Or if you chain them into ONE command? `sed -e 's/\./-/g' -e 's/ /\\|/g'`? – tink Jan 31 '19 at 08:02
  • It still only replaces the .'s with -'s, regardless of the order in which I put them in the script. – user3299633 Jan 31 '19 at 08:03
  • 3
    Have you tried `s/[[:space:]]/\\|/g` to replace the spaces? Are you sure it is just spaces? I.e. hexdump the original value – Jan Gassen Jan 31 '19 at 08:03
  • 1
    FYI: you do not need multiple sed commands for multiple expressions. Simply separate them with `;`, e.g. `sed -e 's/\./-/g;s/[[:space:]]/\\|/g'` – Stefan Becker Jan 31 '19 at 08:28
  • See also https://stackoverflow.com/q/29378566/6770384 – Socowi Jan 31 '19 at 08:38
  • If I run the command without assigning it to a variable it puts the entries on their own line, so I think that is partially what is going on with this problem. I think I have to just break it down into two statements. – user3299633 Feb 01 '19 at 00:42

1 Answers1

1

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
Stefan Becker
  • 5,695
  • 9
  • 20
  • 30