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

2
  • Replace them with what? Commented May 22, 2015 at 0:44
  • With blank line for example,or string like aaaa bbbb cccc now edit question Commented May 22, 2015 at 0:45

3 Answers 3

2

Using sed:

sed -r 's/(.*[a-zA-Z]{4}\-)([0-9]{4})/\1aaaa/g'
4
  • echo ferr-1234-006 |sed -r 's/(.+leng\-)([0-9]{4})/\1aaaa/g' return ferr-1234-006 Commented May 22, 2015 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. Commented May 22, 2015 at 1:12
  • Why the negative vote?? Commented May 22, 2015 at 1:17
  • Error,sorry i have replaced my vote with positive but your answer doesn't work on my problem Commented May 22, 2015 at 1:17
1

POSIXly:

$ sed -e 's/-[^-]*-/-/' file
    leng-001
    leng-002
    leng-004
    leng-005
    leng-006
3
  • Probably my english is not good,i want to remove only the part from 6 to 10 on example 1464 2414 etc Commented May 22, 2015 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 :) Commented May 22, 2015 at 1:51
  • But i like this for files filename-badstring-continue,so i accept as solution Commented May 22, 2015 at 1:59
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 -

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.