Help with Removing extra characters in Filename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with Removing extra characters in Filename
# 1  
Old 11-04-2010
Computer Help with Removing extra characters in Filename

Hi,

It's my first time here... anyways, I have a simple problem with these filenames. This is probably too easy for you guys:
Code:
ABC_20101.2A.2010_01
ABD_20103.2E.2010_04
ABE_20107.2R.2010_08


Expected Output:

Code:
ABC_20101
ABD_20103
ABE_20107

The only pattern available are the ff:
1) All filenames start with AB.
2) Characters before the first dot have fixed length of 9.

I tried using the ${i#...} function, but ended up ruining the entire filenames, and I don't want to do that again... Smilie On my attempt, I ended up renaming files as:
Code:
20101
20103
20107

which is terribly wrong...

can anyone please help?

(btw, I am using Tera Term 3.1.3 via Unix -- pretty old version huh Smilie)

Moderator's Comments:
Mod Comment Use code tags when posting code, data or logs to preserve formatting and enhance readability, thanks

Last edited by zaxxon; 11-04-2010 at 12:32 PM.. Reason: updated details
# 2  
Old 11-04-2010
Code:
$> while read LINE; do echo ${LINE%%.*}; done < infile
ABC_20101
ABD_20103
ABE_20107

If the input comes from a directory instead of a text file it could look like this:
Code:
$> for file in AB*; do echo ${file%%.*}; done
ABC_20101
ABD_20103
ABE_20107

# 3  
Old 11-04-2010
Hi zaxxon,

Wow, many thanks for the immediate answer.

I jsut have one more question though, isn't the 'echo' command just going to display the output? but what I need is to rename the files permanently with this expected output.
# 4  
Old 11-04-2010
As I noticed you seem not to process a text file with those names, I added a 2nd block below, working on directory entries ie. files. You could just convert the echo into a mv like:
Code:
$> for file in AB*; do mv $file ${file%%.*}; done

This User Gave Thanks to zaxxon For This Post:
# 5  
Old 11-04-2010
Code:
bash-3.2$ cat test4
ABC_20101.2A.2010_01
ABD_20103.2E.2010_04
ABE_20107.2R.2010_08
bash-3.2$ sed 'p;s/\..*$//g' test4 |xargs -n2 echo mv
mv ABC_20101.2A.2010_01 ABC_20101
mv ABD_20103.2E.2010_04 ABD_20103
mv ABE_20107.2R.2010_08 ABE_20107
bash-3.2$ sed 'p;s/\..*$//g' test4 |xargs -n2 echo mv|sh

hope this helps u!!
This User Gave Thanks to phoenix_nebula For This Post:
# 6  
Old 11-04-2010
Hi again, thanks for quick reply.

I got an error of files being identical. I could have posted the entire list of file names if I had known it will make sense to do so. Anyways, to illustrate, here are the existing filenames:
Code:
ABC_20101.2A.2010_01
ABC_20102.2C.2010_06
ABD_20103.2E.2010_04
ABD_20105.2J.2010_09
ABE_20107.2R.2010_08
ABE_20109.2B.2010_02

Expected Output:
Code:
ABC_20101
ABC_20102
ABD_20103
ABD_20105
ABE_20107
ABE_20107

when I tried to run the code, it gave me the error that pairs of AB* (two ABC, ABD, and ABE) are identical. Hence, it could not proceed with renaming these files.

any ideas please?
# 7  
Old 11-04-2010
Sorry, I can't see the problem or maybe I misunderstood:
Code:
$> ls -la
total 8
drwxr-xr-x 2 root root 4096  4. Nov 17:06 .
drwxr-x--- 3 isau isau 4096  4. Nov 17:06 ..
-rw-r--r-- 1 root root    0  4. Nov 17:06 ABC_20101.2A.2010_01
-rw-r--r-- 1 root root    0  4. Nov 17:06 ABC_20102.2C.2010_06
-rw-r--r-- 1 root root    0  4. Nov 17:06 ABD_20103.2E.2010_04
-rw-r--r-- 1 root root    0  4. Nov 17:06 ABD_20105.2J.2010_09
-rw-r--r-- 1 root root    0  4. Nov 17:06 ABE_20107.2R.2010_08
-rw-r--r-- 1 root root    0  4. Nov 17:06 ABE_20109.2B.2010_02
$> for file in AB*; do mv $file ${file%%.*}; done
$> ls -la
total 8
drwxr-xr-x 2 root root 4096  4. Nov 17:07 .
drwxr-x--- 3 isau isau 4096  4. Nov 17:06 ..
-rw-r--r-- 1 root root    0  4. Nov 17:06 ABC_20101
-rw-r--r-- 1 root root    0  4. Nov 17:06 ABC_20102
-rw-r--r-- 1 root root    0  4. Nov 17:06 ABD_20103
-rw-r--r-- 1 root root    0  4. Nov 17:06 ABD_20105
-rw-r--r-- 1 root root    0  4. Nov 17:06 ABE_20107
-rw-r--r-- 1 root root    0  4. Nov 17:06 ABE_20109

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing extra lines from file

I have a file where data looks like this: === <?xml version="1.0" encoding="utf-8"?> <xml xmlns:s='uuid:XYZ' xmlns:dt='uuid:ABC' xmlns:rs='urn:schemas-microsoft-com:rowset' xmlns:z='#RowsetSchema'> <s:Schema id='RowsetSchema'> <s:ElementType name='row'... (7 Replies)
Discussion started by: vx04
7 Replies

2. Shell Programming and Scripting

Removing extra unwanted spaces

hi, i need to remove the extra spaces in the 2nd field. Sample: abc|bd |bkd123 .. 1space abc|badf |bakdsf123 .. 2space abc|bqe |bakuowe .. 3space Output: abc|bd|bkd123 abc|badf|bakdsf123 abc|bqe|bakuowe i used the following command, (9 Replies)
Discussion started by: anshaa
9 Replies

3. Shell Programming and Scripting

Removing extra unwanted spaces

hi, i need to remove the extra spaces in the filed. Sample: abc~bd ~bkd123 .. 1space abc~badf ~bakdsf123 .. 2space abc~bqed ~bakuowe .. 3space output: abc~bd ~bkd123 .. 1space abc~badf~bakdsf123 .. 2space abc~bqed~bakuowe .. 3space i used the following command, (2 Replies)
Discussion started by: anshaa
2 Replies

4. UNIX for Dummies Questions & Answers

Removing Extra Folders From a TAR

I use an extremely simple TAR function for files at work and I have a question about cleaning them up. My command is TAR -cvf ExampleTarName.tar then the folder I wish to TAR. When my TAR finishes and I double click it to check it unarchived beautifully (I don't do this with every file, duh)... (5 Replies)
Discussion started by: Dogtown24
5 Replies

5. Shell Programming and Scripting

removing a range of characters in a filename

hi, I have quite a bunch of files with annoyingly long filenames. I wanted to cut the range of characters from 9-18 and just retain the first 8 characters and the .extension. any suggestion how to do it. thanks much. original filename: 20000105_20000105_20100503.nc.asc output filename:... (4 Replies)
Discussion started by: ida1215
4 Replies

6. UNIX for Dummies Questions & Answers

Removing extra new line characters

Hello, I have a text file that looks like: ABC123|some text|some more text|00001 00002 0003 0004 000019|000003|Item I have searched and found an example to remove the extra new line characters using grep and sed, but it (I think) assumes the lines start with a number and the... (5 Replies)
Discussion started by: c56444
5 Replies

7. UNIX for Dummies Questions & Answers

awk for removing special characters and extra commas

Hi, I have a .csv file which as empty lines with comma and some special characters in 3rd column as below. Source data 1,2,3,4,%#,6 ,,,,,, 1,2,3,4,5,6 Target Data 1,2,3,4,5,6I need to remove blank lines and special charcters I am trying to get this using the below awk awk -F","... (2 Replies)
Discussion started by: shruthidwh
2 Replies

8. Shell Programming and Scripting

removing extra files in dos

Hi, I have same file by name i want to keep only access file and want to delete rest. This is specific to DOS only. Any idea of doing this. I tried so many options but none worked for me. Thanks Namish (11 Replies)
Discussion started by: namishtiwari
11 Replies

9. AIX

Removing a filename which has special characters passed from a pipe with xargs

Hi, On AIX 5200-07-00 I have a find command as following to delete files from a certain location that are more than 7 days old. I am being told that I cannot use -exec option to delete files from these directories. Having said that I am more curious to know how this can be done. an sample... (3 Replies)
Discussion started by: jerardfjay
3 Replies

10. UNIX for Dummies Questions & Answers

removing linux/extra partition??

ok, well i never could get my internet connection setup in linux so now it is just wasting space on my system... so, how do i get rid of it and the extra partition made during install?? (1 Reply)
Discussion started by: justchillin
1 Replies
Login or Register to Ask a Question