3

I am using the following command to output a list of servers with associated IPs. For another part of my script, I need this output to be formatted in a particular manner. With an incrementing line number above each line. Example below

 paste <(aws ec2 describe-instances --query 'Reservations[*].Instances[*].Tags[*].{Name:Value}' --output text) \
         <(aws ec2 describe-instances --query 'Reservations[*].Instances[*].{PrivateIP:PrivateIpAddress}' --output text) | awk 'ORS="\n\n"' >> $TMP1  

Which outputs ( in a tmp file ) :

Dev Server   111.11.11.11

Test Server     222.22.22.22

However how can I append numbers to each blank line like so?

Example

1
Dev Server   111.11.11.11
2
Test Server  222.22.22.22

2
  • 1
    Is it possible to get your output from a single query? I don't know what aws is, but some online docs have examples like Reservations[*].Instances[*].{Name:Tags[?Key==`Name`]|[0].Value, PrivateIP:PrivateIpAddress}
    – rowboat
    Dec 30, 2020 at 16:04
  • aws == amazon web services command-line interface. I have been trying to use a single. I will try your suggestion. Dec 30, 2020 at 16:10

2 Answers 2

9

If you change the way you create the file so that you don't insert blank lines (by removing your last awk step), then you can use the short sed script = to insert line numbers:

$ cat file
Dev Server   111.11.11.11
Test Server     222.22.22.22
$ sed '=' file
1
Dev Server   111.11.11.11
2
Test Server     222.22.22.22

The editing command = in sed outputs the current line number on a line of its own. By default, sed outputs the contents of the editing buffer at the end of the editing script, before continuing with the next line. Since the editing buffer remains unchanged by the editing script, the data is reproduced as is.

Ideally, though, one would take the JSON document that I presume that your aws command could produce and pass that through jq to get the correct output, without the need to do two queries.


Working with the given data, using awk:

$ cat file
Dev Server   111.11.11.11

Test Server     222.22.22.22

$ awk '/./ { print ++c; print }' file
1
Dev Server   111.11.11.11
2
Test Server     222.22.22.22

If you need to have an empty line at the end, as in the question:

$ awk '/./ { print ++c; print } END { print }' file
1
Dev Server   111.11.11.11
2
Test Server     222.22.22.22

Both of these awk commands precedes each non-empty line with an incrementing counter, c, running from 1 onward.

0
5

Use awk:

$ cat FILE

Dev Server   111.11.11.11

Test Server     222.22.22.22   
$ awk '{ if ($0 ~ /^$/) { print ++counter } else { print $0 }}' FILE
1
Dev Server   111.11.11.11
2
Test Server     222.22.22.22
2
  • You seem to assume that there is an initial empty line in the data, and no empty line at the end. This is different from what's shown in the question.
    – Kusalananda
    Dec 30, 2020 at 15:58
  • @Kusalananda: yes, I guess this is what OP means Dec 30, 2020 at 16:03

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .