Need script to cut string from a filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need script to cut string from a filename
# 1  
Old 09-18-2013
Need script to cut string from a filename

Hi,

I need a script which do below

Code:
 
I have a filename: TEST2013_09_17_XX_XX_XX.csv
 
Now script should create a new file with name: XX_XX_XX.csv
 
Or I should say i need the output as XX_XX_XX.csv

Please help. Mant thanks in advance

Last edited by sv0081493; 09-18-2013 at 11:44 AM.. Reason: Some more modifications
# 2  
Old 09-18-2013
Hi,
Here examples of cut command:
Code:
$ echo 'TEST2013_09_17_XX_XX_XX.csv' | cut -d'_' -f4-
XX_XX_XX.csv
$ echo 'TEST2013_09_17_XX_XX_XX.csv' | cut -d'_' -f1-
TEST2013_09_17_XX_XX_XX.csv
$ echo 'TEST2013_09_17_XX_XX_XX.csv' | cut -d'_' -f2-
09_17_XX_XX_XX.csv
$ echo 'TEST2013_09_17_XX_XX_XX.csv' | cut -d'_' -f3-
17_XX_XX_XX.csv
$ echo 'TEST2013_09_17_XX_XX_XX.csv' | cut -d'_' -f4-
XX_XX_XX.csv
$ echo 'TEST2013_09_17_XX_XX_XX.csv' | cut -d'_' -f4-5
XX_XX
$ echo 'TEST2013_09_17_XX_XX_XX.csv' | cut -d'_' -f-5
TEST2013_09_17_XX_XX

Regards.
# 3  
Old 09-18-2013
Not sure exactly what you are after but here goes longhand:-
Code:
Last login: Wed Sep 18 11:46:02 on ttys000
AMIGA:barrywalker~> file_and_path="~/TEST2013_09_17_XX_XX_XX.csv"
AMIGA:barrywalker~> new_file_and_path="/tmp/"${file_and_path:17:12}
AMIGA:barrywalker~> echo "$new_file_and_path"
/tmp/XX_XX_XX.csv
AMIGA:barrywalker~> _

# 4  
Old 09-18-2013
Another one...
Code:
file=TEST2013_09_17_XX_XX_XX.csv
echo ${file/*[0-9]_/}

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut the filename

Hi , I've the following file names and i need the part of the file name The files are like below FN_DATE_TODAY_20010178654321.txt FN_DATE_LASTDAY_19990178654321.txt and i need to cut the filenames like below FN_DATE_TODAY FN_DATE_LASTDAY How can i achieve that Thanks (5 Replies)
Discussion started by: smile689
5 Replies

2. Shell Programming and Scripting

Help Needed! - Cut characters after a text string and append to end of filename

Hi all.. I have several unique files that contain one thing in common, and that is acct#. For all files in the directory, I want to append the 10 characters following the word "ACCOUNT:" to the end of the filename. for example: I have file 111_123 that contains ACCOUNT:ABC1234567 The file... (5 Replies)
Discussion started by: cinderella1
5 Replies

3. Shell Programming and Scripting

Bash script to sort files into folder according to a string in the filename

Hi all. I am very new to linux scripting and i have a task i can only solve with a script. I need to sort files base on the date string in their filenames and create a folder using the same date string then move the files to their respective folders. Scenario: Folder Path:... (1 Reply)
Discussion started by: ace47
1 Replies

4. Shell Programming and Scripting

URGENT!!! bash script to sort files into folder according to a string in the filename

Hi all. I am very new to linux scripting and i have a task i can only solve with a script. I need to sort files base on the date string in their filenames and create a folder using the same date string then move the files to their respective folders. Scenario: Folder Path:... (1 Reply)
Discussion started by: ace47
1 Replies

5. Shell Programming and Scripting

cut the some part in filename

Hi All, I have the file & name is "/a/b/c/d/e/xyz.dat" I need "/a/b/c/d/e/" from the above file name. I tryning with echo and awk. But it not come. Please help me in this regard. Thanks & Regards, Dathu (3 Replies)
Discussion started by: pdathu
3 Replies

6. Shell Programming and Scripting

cut filename extension

I need a small script (sh) to remove in a variable the filename extension. Example: f = "testfile.txt" and I need a $a with "testfile". Some one a idea? (4 Replies)
Discussion started by: Essbaumer
4 Replies

7. Shell Programming and Scripting

Cut out string in bash script

Hi all, I'm trying to extract string from variable in BASH. That's probably trivial for grep but I couldn't figure it out. I want to get name, there's sometimes digit after it, but it should be left out. STRING=http://name5.domain.com:8000/file.dat Could someone help me with that? Any... (10 Replies)
Discussion started by: cootue
10 Replies

8. Shell Programming and Scripting

Need to cut filename in LINUX ksh

Hi, I need to cut filename in Linux ksh. for example file name is c_xxxx_cp_200908175035.zip. I need to get variable with only c_xxxx_cp value. (10 Replies)
Discussion started by: juliyp
10 Replies

9. UNIX for Dummies Questions & Answers

is LS a fixed length? I want to use cut -c#-# to isolate filename.

-rw-r--r-- 1 fxpbftp fusion 368 Jun 10 08:34 FX_1.11840235236594E12.111234236809956 If I have a long list of files that look like this (they al begni with FX_1.#######.####) Sometimes, there may be less numbers or more in the filename, that varies. I wish to isolate just the... (8 Replies)
Discussion started by: yongho
8 Replies

10. UNIX for Dummies Questions & Answers

Shell script: Cut / (slash) character in string

I have a string "\/scratch\/databases\". I want to have a new string "\/scratch\/databases" by cutting last '\' character using shell script. I can't do this Please help me. Thanks in advance ThuongTranVN (4 Replies)
Discussion started by: ThuongTranVN
4 Replies
Login or Register to Ask a Question