Cut the filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut the filename
# 1  
Old 06-29-2013
Cut the filename

Hi ,
I've the following file names and i need the part of the file name
The files are like below
Code:
FN_DATE_TODAY_20010178654321.txt
FN_DATE_LASTDAY_19990178654321.txt

and i need to cut the filenames like below
Code:
FN_DATE_TODAY
FN_DATE_LASTDAY

How can i achieve that
Thanks
# 2  
Old 06-29-2013
Use parameter substitution:
Code:
file="FN_DATE_TODAY_20010178654321.txt"
echo "${file%_*}"

# 3  
Old 06-29-2013
Quote:
Originally Posted by smile689
Hi ,
I've the following file names and i need the part of the file name
The files are like below
Code:
FN_DATE_TODAY_20010178654321.txt
FN_DATE_LASTDAY_19990178654321.txt

and i need to cut the filenames like below
Code:
FN_DATE_TODAY
FN_DATE_LASTDAY

How can i achieve that
Thanks
Assuming that you're using a shell that performs the standard parameter expansions, the following script shows one way to do what you want:
Code:
for i in FN*
do      printf "%s cuts down to %s\n" "$i" "${i%_*}"
done

which (if the only two files in the current directory having filenames starting with FN are the two you listed), produces the output:
Code:
FN_DATE_LASTDAY_19990178654321.txt cuts down to FN_DATE_LASTDAY
FN_DATE_TODAY_20010178654321.txt cuts down to FN_DATE_TODAY

# 4  
Old 06-30-2013
Quote:

Hi ,
I've the following file names and i need the part of the file name
The files are like below


Code:
FN_DATE_TODAY_20010178654321.txtFN_DATE_LASTDAY_19990178654321.txt

and i need to cut the filenames like below


Code:
FN_DATE_TODAYFN_DATE_LASTDAY

How can i achieve that
Thanks

Hello,

Assuming that you have 18 characters after your file name including digits and .txt extension.



Code:
 
ls -ltr | grep -i "FN*" > test
 
cat test| awk '{for(i=0;i<=NF;i++) sub(/...................$/,X,$i)}1'
FN_DATE_TODAY
FN_DATE_LASTDAY



Hope it may help you.


Thanks,
R. Singh
# 5  
Old 06-30-2013
Quote:
Originally Posted by RavinderSingh13
ls -ltr | grep -i "FN*" > test

cat test| awk '{for(i=0;i<=NF;i++) sub(/...................$/,X,$i)}1'
You got yourself the Useless Use of Cat Award Smilie

The purpose of cat is to concatenate, awk is capable of reading file by itself, so no need to cat and pipe the output to awk.
# 6  
Old 07-01-2013
There's a lot more wrong with that code than the relatively benign useless use of cat.

Why is ls using a long listing if only the filenames are of interest? Why is it doing a reverse mtime sort?

The grep regular expression is incorrect for two reasons: (1) it is unanchored, allowing the FN* to match anywhere and not just at the beginning, and (2) it applies a wildcard to N, so any filename with an F matches.

I haven't tested it, but I don't see how applying sub() to each field of a long ls listing generates the output shown.

I see no reason to bother with a temp file.

I think the shell solutions suggested above are just fine. However, if for some reason ls and grep are preferred:
Code:
ls | grep '^FN_' | cut -d_ -f1-3

With sed:
Code:
ls | sed '/^FN_/!d; s/_[^_]*$//p'

Regards,
Alister

Last edited by alister; 07-01-2013 at 12:38 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Print/cut/grep/sed/ date yyyymmdd on the filename only.

I have this filename "RBD_EXTRACT_a3468_d20131118.tar.gz" and I would like print out the "yyyymmdd" only. I use this command below, but if different command like cut or print....etc. Thanks ls RBD_EXTRACT* | sed 's/.*\(........\).tar.gz$/\1/' > test.txt (9 Replies)
Discussion started by: dotran
9 Replies

2. Shell Programming and Scripting

Need script to cut string from a filename

Hi, I need a script which do below 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 (3 Replies)
Discussion started by: sv0081493
3 Replies

3. 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

4. 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

5. 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

6. Shell Programming and Scripting

Filename from splitting files to have the same filename of the original file with counter value

Hi all, I have a list of xml file. I need to split the files to a different files when see the <ko> tag. The list of filename are B20090908.1100-20090908.1200_CDMA=1,NO=2,SITE=3.xml B20090908.1200-20090908.1300_CDMA=1,NO=2,SITE=3.xml B20090908.1300-20090908.1400_CDMA=1,NO=2,SITE=3.xml ... (3 Replies)
Discussion started by: natalie23
3 Replies

7. 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

8. Shell Programming and Scripting

gzcat into awk and then change FILENAME and process new FILENAME

I am trying to write a script that prompts users for date and time, then process the gzip file into awk. During the ksh part of the script another file is created and needs to be processed with a different set of pattern matches then I need to combine the two in the end. I'm stuck at the part... (6 Replies)
Discussion started by: timj123
6 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
Login or Register to Ask a Question