Removing the trailing date from a filename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Removing the trailing date from a filename
# 1  
Old 01-25-2011
Removing the trailing date from a filename

Hi
I have 3 files (say) in a folder as in the example below

Code:
abc_01012011.csv
def_01012011.csv
xyz_01012011.csv

I need to move these files to a different folder as follows
Code:
abc.csv
def.csv
xyz.csv

I am trying to put together a script with a for loop which reads the source filenames from a txt file

Code:
fname=${filename%%.*}
newname=sed 's/\(.*[a-zA-Z]\).*/\1/g' ${fname}
mv ${filename} /destination folder/${newname}.csv

This doesnt seem to work.. any ideas what is wrong or if there is a easier solution

Thanks

Last edited by radoulov; 01-25-2011 at 05:28 PM.. Reason: Code tags, please!
# 2  
Old 01-25-2011
Try:

Code:
for f in *.csv; do
  mv -- "$f" "dest/${f%%_*}.csv"  
done

# 3  
Old 01-25-2011
Hi
Thanks for the quick reply. It works but with a hiccup.
The Files are not exactly
Code:
abc_01012011.csv
def_01012011.csv
xyz_01012011.csv

They are more like
Code:
abc_report_01012011.csv
def_test_01012011.csv
xyz_post_01012011.csv

The script you truncates the name at the first underscore "_"

I would like to keep the name till the date
Code:
abc_report.csv
def_test.csv
xyz_post.csv

Thanks

Last edited by radoulov; 01-25-2011 at 05:41 PM.. Reason: Code tags!
# 4  
Old 01-25-2011
So the next time post representative data Smilie
Code:
for f in *.csv; do
  mv -- "$f" "dest/${f%_*}.csv"  
done

This User Gave Thanks to radoulov For This Post:
# 5  
Old 01-25-2011
I think radoulov's solution is cleaner but this should work also:

Code:
ls -1  *.csv | awk '{print "mv " $1 " newdir/"$1}' | sed -E 's/([[:alpha:]]+)_[^_]+(\.csv)/\1\2/2'  | csh

The regex takes one or more characters or _, followed by _, followed by anything but _ (you could say [[:digit:]] instead if the last bit is always a bunch of digits), followed by .csv, and only prints the first part (your abc_def_ghi bit) and the last part (.csv). The final "2" says to operate only on the second occurrence, that is the second time it appears in the awk output.

The regex looks wrong to me (the :alpha: should not match any _ but it appears to do so) but it worked on my test .csv files -- if you use it, probably would be a good idea to test it a bit.
# 6  
Old 01-26-2011
Try:

Code:
for file in *.csv
do
file_new=`echo ${file} |awk 'BEGIN {FS="_"} {print $1 "_" $2 ".csv"}'`
mv ${file} ${file_new}
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing Trailing Line

I have been trying to remove empty lines and lines just filled with spaces. I have used the following command which does work. sed -i "/^\s*$/d" Except it leaves one single trailing line at the very end of the file. For the life of me I cant figure out why I cant remove that last trailing... (2 Replies)
Discussion started by: user8282892
2 Replies

2. UNIX for Dummies Questions & Answers

Removing trailing x'0A' characters.

I am trying to remove trailing carriage return (x'0a') from a source program. What is a good way to do this for the whole file? TIA (4 Replies)
Discussion started by: wbport
4 Replies

3. UNIX for Dummies Questions & Answers

Removing trailing characters

I have been given a shell script that I need to amend. To do the following extract the filename from the flag file by removing the .flag extension. # Local variables # Find if the flag files exists MASK=coda_mil2*.flag # Are there any files? bookmark="40" fileFound=0 ls -1... (3 Replies)
Discussion started by: andymay
3 Replies

4. Shell Programming and Scripting

Removing just the trailing commas :-(

Hi all, I haven't needed to do any shell based editing for nearly 20 years, and no amount of searching around has found me a solution to this very simple problem :-( I have a csv file. Some lines have three commas at the end. This means the invoice hasn't been paid. I'd like to use sed / grep... (4 Replies)
Discussion started by: chardyzulu
4 Replies

5. Shell Programming and Scripting

Displaying trailing spaces in a filename; Korn Shell

Korn Shell on AIX 6.1 In one of our directories, we have 2 files with same names but one of those file's name has 3 trailing spaces ls *.ctl rconf.ctl rconf.ctl #this file has 3 trailing spaces Is there any way we could display these trailing spaces ? (6 Replies)
Discussion started by: polavan
6 Replies

6. Shell Programming and Scripting

Removing trailing zeros using sed

Hello All, I have a csv file with 3 columns. The file which looks like this 47850000,100,233 23560000,10000,456 78650000,560000,54 34000000,3456,3 The first column has 4 trailing zeros. I have to remove 4 trailing zeroes from 1st field. The output file should appear as follows. ... (12 Replies)
Discussion started by: grajp002
12 Replies

7. Shell Programming and Scripting

Removing trailing zeroes

So, I can't figure out how to do a previous question with printf, so I'm taking a different approach. Suppose I have a set of numbers: 1200,135.000000,12.30100,3212.3200,1.759403,,1230,101.101010,100.000000 I want to remove all trailing zeroes after the decimal, and, if it ends up orphaned,... (8 Replies)
Discussion started by: treesloth
8 Replies

8. Shell Programming and Scripting

sed: removing any and all trailing digits?

We have a large number of oracle database related scripts that utilize the environment variables $ORACLE_SID and $DBNAME. In a single instance database the $ORACLE_SID is the same as the database name $DBNAME. So we have simply set DBNAME = $ORACLE_SID. However, now that we are clustering with RAC,... (5 Replies)
Discussion started by: Squeakygoose
5 Replies

9. UNIX for Dummies Questions & Answers

trailing question mark in filename

I have a script(ex.sh) with one line in it, running in bash shell. ls -l > /usr/ngasi/contexts/tdevoe/private/ex.txt when I run it , it creates the file with a trailing question mark -rwx------ 1 tdevoe webapp 59 Jun 7 06:42 ex.sh -rw------- 1 tdevoe webapp 3761 Jun ... (3 Replies)
Discussion started by: devoetfd
3 Replies

10. Shell Programming and Scripting

re: removing trailing space from lines

Not sure why this thread was closed without any explanation, but you can do what you're asking with sed 's/]*$//g' < sourceFile > destFile (1 Reply)
Discussion started by: oombera
1 Replies
Login or Register to Ask a Question