4

The following variable include for example this values

echo $SERVERS


server1,server2,server3,server4,server5

and when I want to pipe them on different lines then I do the following

echo $SERVERS | tr ',' '\n'

server1
server2
server3
server4
server5

now I want to add another pipe ( echo $SERVERS | tr ',' '\n' | ..... ) , in order to print the following expected results

1  ………………  server1
2  ………………… server2
3  ………………… server3
4  ………………… server4
5  ………………… server5
6  ………………  server6
7  ………………… server7
8  ………………… server8
9  ………………… server9
10 ………………… server10
11 ………………… server11
12 ………………… server12

Not sure how to do it but maybe with nc command os similar

Any suggestion?

yael
  • 12,598
  • 51
  • 169
  • 303

3 Answers3

8

With awk:

$ servers='server1,server2,server3,server4,server5'

$ awk -v RS=, '{print NR "........" $0}' <<<"$servers"
1........server1
2........server2
3........server3
4........server4
5........server5

or, to output the line numbers with left-padding

awk -v RS=, '{printf "%3d........%s\n",NR,$0}' <<<"$servers"

(choose the field width 3 as appropriate for the size of your server list).

steeldriver
  • 78,509
  • 12
  • 109
  • 152
  • is it possible to add printf , so it will be alignment when number are bigger then 10 ( I update my question ) – yael Feb 09 '20 at 13:12
  • @yael like `printf "%3d........%s\n",NR,$0`you mean? sure – steeldriver Feb 09 '20 at 13:17
  • yes , Iit will be great if you add this to your answer – yael Feb 09 '20 at 13:22
  • That will print an extra blank line at the end of the output since `<<<` ensures the generated string has a terminating newline. – Ed Morton Feb 09 '20 at 13:42
  • @EdMorton yes I realize that, I considered suggesting `<(printf '%s' "$servers")` but it seemed like overkill for what is likely a non-problem – steeldriver Feb 09 '20 at 13:56
  • 1
    You could be right but I feel like trailing blanks at the end of lines or trailing newlines at the end of output are something that don't get noticed at first and then they often cause some head-scratching down the road when they DO cause a problem and then people "fix" it by tacking on a pipe to some other command to remove them... so best just never to output them in the first place. The `printf` workaround you suggest has the problkem that it's output isn't a POSIX text file due to no terminating newline so YMMV with what any given tool will do with it (though it'll probably be OK). – Ed Morton Feb 09 '20 at 13:59
  • OK fair enough - upvoting yours, will delete mine if @yael would be kind enough to un-accept it – steeldriver Feb 09 '20 at 14:04
  • No reason to delete it, it might be just fine for this particular OP, but it's always worth just stating any caveats especially when they're completely invisible in the output as in this case. – Ed Morton Feb 09 '20 at 14:04
3
cat -n <<< ${SERVERS//,/$'\n'} 
     1  server1
     2  server2
     3  server3
     4  server4
     5  server5
RudiC
  • 8,889
  • 2
  • 10
  • 22
3
$ awk -F',' '{for (i=1; i<=NF; i++) printf "%-2d ........ %s\n", i, $i}' <<<"$servers"
1  ........ server1
2  ........ server2
3  ........ server3
4  ........ server4
5  ........ server5
Ed Morton
  • 28,789
  • 5
  • 20
  • 47