stfsngue January 2017

UNIX Bash: Number each line of a file with a specific format

I'm trying to number each line of a file with this format: /1/ , /2/ , ...

For example

From

UK
Australia
Newzealand
Brazil
America

To

/1/ UK
/2/ Australia
/3/ Newzealand
/4/ Brazil
/5/ America

I think about

grep -n file.txt // but the format is 1: , 2: , ...
nl file.txt  // but the format is 1 , 2 , 3

Can i give a specific format to nl command?

Answers


Inian January 2017

Use the printf function in Awk

awk '{printf("/%d/ %s\n", NR, $0)}' file

Post Status

Asked in January 2017
Viewed 3,995 times
Voted 5
Answered 1 times

Search




Leave an answer