Use the printf
function in Awk
, with the characters /
around the integer part.
awk '{printf("/%d/ %s\n", NR, $0)}' file
/1/ UK
/2/ Australia
/3/ Newzealand
/4/ Brazil
/5/ America
NR
is a special variable in awk
which tracks the record number of each line as it is processed, and the line as such being $0
. The above command is POSIX
compatible since awk
in all major platforms support printf
with format specifiers.
A proper way to do it in bash
alone, without any third-party tools.
#!/bin/bash
count=1
while IFS= read -r line
do
printf "/%d/ %s\n" "$count" "$line"
((count++))
done <input-file
AFAIK, from the man nl
page, there are options you can use, but NONE to wrap the line numbers around the characters you want, you can of-course use it one side, with -s
flag
The closest I could do with it is,
nl -nrz -s/ -w1 file
1/UK
2/Australia
3/Newzealand
4/Brazil
5/America
here, -nrz
for right justification and -s
for the special character to use after the numbering done. In this case, I have a added a /
and -w
controls the width of the numbering part.