0

I'm new to Linux and I have to remove a specific string of digits in a file name.

Here are my file names:

AB.TEXT.OMN.BUFFER.INSERT.123.20130315.CSV 
AB.TEXT.OMN.BUFFER.APPEND.5345667.20130315.CSV

I need the output as:

AB.TEXT.OMN.BUFFER.INSERT.20130315.CSV   
AB.TEXT.OMN.BUFFER.APPEND.20130315.CSV

I want to remove the 123 and which may 5345667 or other numbers what ever come in that position, so I want to remove string between . (5th occurrence) and . (6th Occurrence) in the file name.

2 Answers 2

1

cut can do this:

pax> echo 'AB.TEXT.OMN.BUFFER.INSERT.123.20130315.CSV' | cut -d. -f1-5,7-
AB.TEXT.OMN.BUFFER.INSERT.20130315.CSV

pax> echo 'AB.TEXT.OMN.BUFFER.APPEND.5345667.20130315.CSV' | cut -d. -f1-5,7-
AB.TEXT.OMN.BUFFER.APPEND.20130315.CSV

The -d. simply sets the delimiter to . and the -f1-5,7- gives you all fields except the sixth one.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for you quick response, I actually need to rename all files in a dir . could you please help me how to rename the file using this cut command
@Naga, if you want to rename files, you should look into prename (sometimes rename) - it's meant for just that purpose, giving you the full regex capabilies of Perl to rename files. See for example stackoverflow.com/questions/12292232/… and stackoverflow.com/questions/10138322/…
Thanks for your suggestions, I have created the below script to rename the file. #!/bin/sh for i in ls -a *.CSV do echo "$i" newFileName=$( echo "$i" | cut -d. -f1-5,7-) echo $newFileName mv $i $newFileName done
0

Input.txt

AB.TEXT.OMN.BUFFER.INSERT.123.20130315.CSV 
AB.TEXT.OMN.BUFFER.APPEND.5345667.20130315.CSV

Command

awk -F "." '{ $6=""; print $0 }' Input.txt | sed -e 's/ /\./g' -e 's/\.\./\./g'

Output

AB.TEXT.OMN.BUFFER.INSERT.20130315.CSV   
AB.TEXT.OMN.BUFFER.APPEND.20130315.CSV

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.