sprunknwn
sprunknwn

Reputation: 37

How can I replace a number for an increasing number for each line using sed?

I got a file with the following text -

observ1
observ1
observ1
observ1
observ1

What I need is a sed command that replace it to -

observ1
observ2
observ3
observ4
observ5

Or a sed command to add observ# in the beginning of each line for example - file 1.txt - hi
bye
see
you
soon

After said sed command -

observ1,hi
observ2,bye
observ3,see
observ4,you
observ5,soon

Thanks!

Upvotes: 0

Views: 68

Answers (1)

Emerald Cottet
Emerald Cottet

Reputation: 186

In 4 steps :

sed = a.txt | sed 'N;s/\n/,/' | sed 's/^/observ/' > b.txt
  1. numbering lines of the file
  2. add number + ',' at each line
  3. add prefix 'observ' at each line
  4. print result in new file

Upvotes: 1

Related Questions