2

I have a series of files as below :

000_0123
000_0234
000_0345
000_0456 
000_0678 
000_0890
000_01123
000_01234

I want to change the names to :

000_123
000_234
000_345
000_456 
000_678 
000_890
000_1123
000_1234

and I want to add first line in each file as "#include<conio.h>". Can anyone help me ?

2

To add your line to the start of each file you could do

for i in 000*; do sed '1i#include<conio.h>' "$i"; done

1i means insert this at the first line (before existing first line). The existing first line becomes line 2. A warning: this command will fail (do nothing) for empty files.

The contents of all the files with the added line will appear in the terminal one after the other. If it looks right, then do again with -i to change the files in place

for i in 000*; do sed -i '1i#include<conio.h>' "$i"; done    

If you just want to remove the leading 0 from after _ you could use rename to rename the files...

rename 's/0_0/0_/' 000*
2
  • Once again you help me , thank you , work for me , accepted ... – Avani badheka Sep 29 '16 at 10:50
  • my pleasure as always :D – Zanna Sep 29 '16 at 10:54

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.