0

I have multiple files to rename

    leng-1494-001
    leng-1464-002
    leng-2414-004
    leng-7894-005
    leng-1323-006

I want to rename it

    ferr-1494-001
    ferr-1464-002
    ferr-2414-004
    ferr-7894-005
    ferr-1323-006

I know how to do for ferr and leng,but how to replace characters from 6 to 10( 1464,7894) with blank line for example,or string like aaaa bbbb cccc using sed? Also awk or perl solution is welcome. Thanks

  • Replace them with what? – jasonwryan May 22 '15 at 0:44
  • With blank line for example,or string like aaaa bbbb cccc now edit question – elbarna May 22 '15 at 0:45
1

POSIXly:

$ sed -e 's/-[^-]*-/-/' file
    leng-001
    leng-002
    leng-004
    leng-005
    leng-006
  • Probably my english is not good,i want to remove only the part from 6 to 10 on example 1464 2414 etc – elbarna May 22 '15 at 1:20
  • I try the edited,is working,but perl solution is more adaptable for other files maybe called leng-prov-help-293494-udd :) – elbarna May 22 '15 at 1:51
  • But i like this for files filename-badstring-continue,so i accept as solution – elbarna May 22 '15 at 1:59
2

Using sed:

sed -r 's/(.*[a-zA-Z]{4}\-)([0-9]{4})/\1aaaa/g'
  • echo ferr-1234-006 |sed -r 's/(.+leng\-)([0-9]{4})/\1aaaa/g' return ferr-1234-006 – elbarna May 22 '15 at 1:10
  • In your question you said the original file names contain "leng". Anyways, modified accordingly. Now it should work fine for "leng", "ferr" or whatever. – shivams May 22 '15 at 1:12
  • Why the negative vote?? – shivams May 22 '15 at 1:17
  • Error,sorry i have replaced my vote with positive but your answer doesn't work on my problem – elbarna May 22 '15 at 1:17
1

I found a solution using perl substring function

echo     leng-1323-006|perl -lpe 'substr($_, 5, 5) = "";'

return correctly

leng-006

I hve removed from position 5 the 5 bad characters include the -

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.