AWK – Add and Number Blank Line Above Each Line

awktext processing

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

Best Answer

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
Related Question