How to remove sections of a filename?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove sections of a filename?
# 1  
Old 07-13-2013
How to remove sections of a filename?

Hello,

I need some help with renaming some files by removing a certain portion of the filename.

The current file name is: ABC_2013186197_20130708_203556.95336

I need to remove the 5 digits after the first "_". The new file name should be:

ABC_197_20130708_203556.95336

I'm not quite sure how to do this.
# 2  
Old 07-13-2013
Quote:
Originally Posted by bbbngowc
Hello,

I need some help with renaming some files by removing a certain portion of the filename.

The current file name is: ABC_2013186197_20130708_203556.95336

I need to remove the 5 digits after the first "_". The new file name should be:

ABC_197_20130708_203556.95336

I'm not quite sure how to do this.
There are lots of ways to do things like this depending on what system you're using and what shell you're using. So, what OS and shell are you using?

And, you need to explain more clearly what you want to do. If you remove the 5 digits after the first underscore from ABC_2013186197_20130708_203556.95336, I would expected to get ABC_86197_20130708_203556.95336; not ABC_197_20130708_203556.95336???
# 3  
Old 07-13-2013
Quote:
Originally Posted by Don Cragun
There are lots of ways to do things like this depending on what system you're using and what shell you're using. So, what OS and shell are you using?

And, you need to explain more clearly what you want to do. If you remove the 5 digits after the first underscore from ABC_2013186197_20130708_203556.95336, I would expected to get ABC_86197_20130708_203556.95336; not ABC_197_20130708_203556.95336???
The OS is Redhat 5 and you're right. I need to remove the 7 digits after the first "_".

---------- Post updated at 12:38 AM ---------- Previous update was at 12:34 AM ----------

oh and the shell is bash.
# 4  
Old 07-13-2013
Code:
$ echo "ABC_2013186197_20130708_203556.95336" | awk -F_ 'BEGIN{OFS="_"}{$2=substr($2,7,length($2));print}'
ABC_6197_20130708_203556.95336

$ echo "ABC_2013186197_20130708_203556.95336" | awk -F_ '{$2=substr($2,7,length($2));print}' OFS=_
ABC_6197_20130708_203556.95336

# 5  
Old 07-13-2013
And without needing to invoke awk, you can do this entirely in the shell using:
Code:
for f in *_*_*_*.*
do      echo mv "$f" "${f%%_*}_${f#*_[0-9][0-9][0-9][0-9][0-9][0-9]}"
done

Despite what you learned in the last thread you started, what you need here is filename pattern matching and parameter expansion; not regular expressions. Filename patterns and REs are similar in some ways and very different in others. With an extended regular expression, you can match a specific number of a certain RE by enclosing the RE in parentheses in and following with that with \{number\}. But that isn't available in standard filename pattern matching. The parameter expansion ${f%%_*} expands $f with the longest string starting with _ removed from the end of the string (in this case expanding to ABC) and the parameter expansion ${f#*_[0-9][0-9][0-9][0-9][0-9][0-9]} expands $f with the shortest string ending with _ followed by seven digits from the start of the string (in this case 6197_20130708_203556.95336).

Note that you need to be sure that this is going to do what you want before you actually do it. The echo in red above shows you what it will do without actually renaming any of the files. If you run the script as shown using bash and the output looks right, remove the echo and run it again to actually rename the files. If you run this twice (without the echo) it will get rid of all 10 digits between the 1st two underscores in your filenames (and you won't have any way to recreate the original names) unless you make a backup copy of your files before you rename them.

Last edited by Don Cragun; 07-13-2013 at 02:08 AM.. Reason: Fix typo.
# 6  
Old 07-13-2013
Quote:
Originally Posted by itkamaraj
Code:
$ echo "ABC_2013186197_20130708_203556.95336" | awk -F_ 'BEGIN{OFS="_"}{$2=substr($2,7,length($2));print}'
ABC_6197_20130708_203556.95336

$ echo "ABC_2013186197_20130708_203556.95336" | awk -F_ '{$2=substr($2,7,length($2));print}' OFS=_
ABC_6197_20130708_203556.95336

This sure does work. Thank you.

---------- Post updated at 01:16 AM ---------- Previous update was at 01:16 AM ----------

Quote:
Originally Posted by Don Cragun
And without needing to invoke awk, you can do this entirely in the shell using:
Code:
for f in *_*_*_*.*
do      echo mv "$f" "${f%%_*}_${f#*_[0-9][0-9][0-9][0-9][0-9][0-9]}"
done

Despite what you learned in the last thread you started, what you need here is filename pattern matching and parameter expansion; not regular expressions. Filename patterns and REs are similar in some ways and very different in others. With an extended regular expression, you can match a specific number of a certain RE by enclosing the RE in parentheses in and following with that with \{number\}. But that isn't available in standard filename pattern matching. The parameter expansion ${f%%_*} expands $f with the longest string starting with _ removed from the end of the string (in this case expanding to ABC) and the parameter expansion ${f#*_[0-9][0-9][0-9][0-9][0-9][0-9]} expands $f with the shortest string ending with _ followed by seven digits from the start of the string (in this case 6197_20130708_203556.95336).

Note that you need to be sure that this is going to do what you want before you actually do it. The echo in red above shows you what it will do without actually renaming any of the files. If you run the script as shown using bash and the output looks right, remove the echo and run it again to actually rename the files. If you run this twice (without the echo) it will get rid of all 10 digits between the 1st two underscores in your filenames (and you won't have any way to recreate the original names) unless you make a backup copy of your files before you rename them.
Excellent explanation.

Many thanks for this, I really do appreciate it!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove sections based on duplicate first line

Hi, I have a file with many sections in it. Each section is separated by a blank line. The first line of each section would determine if the section is duplicate or not. if the section is duplicate then remove the entire section from the file. below is the example of input and output.... (5 Replies)
Discussion started by: ahmedwaseem2000
5 Replies

2. Shell Programming and Scripting

Remove spaces in filename

Hi team, Here's a requirement for me. Here are the list of files i have in a unix directory. W 2 A D_2014.csv W 3 A D_2014.csv W 4 A D_2014.csv /home/kmani00-> uname -a AIX sliyyvxx 1 6 00F613E54C00 /home/kmani00-> The file names has to be without spaces as follows. W2AD_2014.csv... (1 Reply)
Discussion started by: kmanivan82
1 Replies

3. Shell Programming and Scripting

Remove the last 9 characters of a filename

Hi All! Please can someone help, I have a dir with the following files: ~-rw-r--r-- 1 emmuser users 2087361 Oct 16 15:50 MPGGSN02_20131007234519_24291.20131007 -rw-r--r-- 1 emmuser users 2086837 Oct 16 15:50 MPGGSN02_20131007233529_24272.20131007 -rw-r--r-- 1 emmuser ... (7 Replies)
Discussion started by: fretagi
7 Replies

4. Shell Programming and Scripting

Remove last character from filename

Hi All, I have different type of file (.txt,.csv,.xml) format in my current directory. My requirement is that I need to remove the last character from the file format. Example count.txt$ csp_rules.csv^ Date.xml~ Need Output: count.txt csp_rules.csv Date.xml How to do that?.... (5 Replies)
Discussion started by: suresh01_apk
5 Replies

5. Shell Programming and Scripting

remove the filename from a string

I have a string like this /Development/ST/st000001su/Outbound/Prod/PROD-732QCJ/63acf2caf91bc136cb9bcce8a85c7fa8/PGP/PGP.txt I want to remove the PGP.txt and I want only the /Development/ST/st000001su/Outbound/Prod/MCFR-732QCJ/63acf2caf91bc136cb9bcce8a85c7fa8/PGP returned. I saw an command... (2 Replies)
Discussion started by: srini0603
2 Replies

6. Shell Programming and Scripting

How to remove filename from the path

Hi, I have a list of paths with files at the end. How can strip off filenames. This is what I have: /apps/test/abc/file.txt /apps/new/home/daily/report.xml /apps/old/home/weekly/out/test.sh This is what I need: /apps/test/abc/ /apps/new/home/daily/ /apps/old/home/weekly/out/ ... (10 Replies)
Discussion started by: djanu
10 Replies

7. Shell Programming and Scripting

how to remove last two extensions of a filename

hi how to remove extensions of a file.. suppose i have a filename "gtk2-2.4.13-24.el4.x86_64.rpm" and i want the file name as "gtk2-2.4.13-24.el4" means want to remove last two "." extensions of a file can anyone help me in this thanks in advance srik (12 Replies)
Discussion started by: srikanthg
12 Replies

8. Shell Programming and Scripting

How to remove numbers from filename

Hi all, Can I edit this script: find . -type f | while read i;do && mv "$i" "${i//abc/}" ;done so that it will not only take out abc from the filename but also take out any numbers that might be in the filename as well. An example would be, Input: filename abc 2009.mov Output:... (7 Replies)
Discussion started by: Monkey Dean
7 Replies

9. Shell Programming and Scripting

Remove sections of a xml file with sed

I've been trying to remove some lines of a xml file that looks like this: <parent> <child>name1</child> <lots_of_other tags></lots_of_other_tags> </parent> <parent> <child>name2</child> <lots_of_other tags></lots_of_other_tags> </parent> <parent> <child>name3</child> ... (5 Replies)
Discussion started by: viniciusandre
5 Replies

10. Shell Programming and Scripting

Best way to remove sections of text from a file

Greetings! I found this fourm via a google search on "sed expressions". I have a file that contains notices and they are all the same length in lines. For example the file would contains 15 notices, each being 26 lines each. I need some way to eliminate notices that contain a "S" in a particular... (8 Replies)
Discussion started by: cals64
8 Replies
Login or Register to Ask a Question