1

I have an input file with below data, how can I convert it to below mentioned output file.

Input file

00001abc
00002def
00003ghi
00001jkl
00002mno
00003pqr
00001stu
00002vwx
00003yza
00004bcd

Output file

00001abc
00002def
00003ghi
00004jkl
00005mno
00006pqr
00007stu
00008vwx
00009yza
00010bcd
  • is this is the only line you want to change or have you just posted it for example? And, is this a file or data in a file? Nothing is clear. – erTugRul Apr 6 '18 at 11:10
3

Another awk approach.

awk -F'[0-9]+' '{printf("%05d%s\n",NR,$2)}' infile >newfile
1

try

awk '{ printf "%05d%s\n",NR,substr($1,6)}' input_file > output_file

where

  • printf "%05d" .. NR will output line number (NR (Number of record)) padded with 0
  • substr($1,6) will extract substring of $1 (first argument) starting from 6
1

Using sed to delete the existing digits from the start of each line, and nl to number the lines in the specific manner:

$ sed 's/^[0-9]*//' file | nl -n rz -s '' -w 5
00001abc
00002def
00003ghi
00004jkl
00005mno
00006pqr
00007stu
00008vwx
00009yza
00010bcd

The nl options asks for zero-filled numbers (-n rz, which also right-justifies the numbers, but this is unimportant here) with no delimiter between the line numbers and the data (-s ''). The numbers are five digits wide (-w 5).

nl is a POSIX utility.

0

With sh

while read a;do i=$((i+1));printf "%05d%s\n" $i "${a##*[0-9]}";done < infile > outfile

inplace replacement

printf '%s\n' $(while read a;do i=$((i+1));printf "%05d%s\n" $i "${a##*[0-9]}";done < infile) > infile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.