How to trim the file names?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to trim the file names?
# 1  
Old 02-28-2014
How to trim the file names?

How do i get the following output, irrespective of how many . 's i will have the file name.

Input:
FCBTexas_2013_12_31_16_V2.5_Masked.xml.filepart

Output:
FCBTexas_2013_12_31_16_V2.5_Masked.xml

Thank you.
# 2  
Old 02-28-2014
Code:
ls | grep "\.filepart$" | while read FILENAME
do
        echo mv "$FILENAME" "${FILENAME/.filepart/}"
done

Remove the 'echo' once you're sure it does what you want.
# 3  
Old 02-28-2014
Quote:
Originally Posted by Corona688
Code:
ls | grep "\.filepart$" | while read FILENAME
do
        echo mv "$FILENAME" "${FILENAME/.filepart/}"
done

Remove the 'echo' once you're sure it does what you want.
i cann't move the file, is there any other way i can do this using sed or awk command.

Thank you.
# 4  
Old 02-28-2014
Oh, you just want to remove it from a string, not change the filename. That's much easier.

Code:
STRING="asdf.filepart"
echo "${STRING/.filepart/}"

You can put it in the loop above to do many.

Or you can also do it this way in awk:

Code:
ls *.filepart | awk -F"." '{ NF=(NF-1) } 1'

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 02-28-2014
Code:
file="FCBTexas_2013_12_31_16_V2.5_Masked.xml.filepart"

echo "${file%.*}"

This User Gave Thanks to Yoda For This Post:
# 6  
Old 02-28-2014
Quote:
Originally Posted by Yoda
Code:
file="FCBTexas_2013_12_31_16_V2.5_Masked.xml.filepart"

echo "${file%.*}"

how do i just get the extention only the "filepart", i am also trying to get it on my side, please help.
# 7  
Old 02-28-2014
Hello,

Following may help.

Code:
echo "FCBTexas_2013_12_31_16_V2.5_Masked.xml.filepart" | awk 'gsub(/xml\..*/,"xml") 1'

Output will be as follows.
Code:
FCBTexas_2013_12_31_16_V2.5_Masked.xml


Code:
echo "FCBTexas_2013_12_31_16_V2.5_Masked.xml.filepart" | sed 's/\(.*xml\)\(.*\)/\1/g'


Output will be as follows.
Code:
FCBTexas_2013_12_31_16_V2.5_Masked.xml


To get only filepart please use the following.

Code:
echo "FCBTexas_2013_12_31_16_V2.5_Masked.xml.filepart" | awk 'gsub(/.*\.+/,X) 1'

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

2. Shell Programming and Scripting

Trim a file columnwise

Hi All, I want to trim each columns leading & trailing (like sql trim function) of a ',' separated file.. I've a file like this.. manab , c gi lucky , home babu , maa I want the output as manab,c gi lucky,home babu,maa A one liner would be a great help. (2 Replies)
Discussion started by: manab86
2 Replies

3. UNIX for Dummies Questions & Answers

Trim file using filename

I want to remove first xx letters from filename_xx . How is that possible using linux. I have many files where xx could be same in many but filename is always unique. Thanks! (1 Reply)
Discussion started by: keysoon
1 Replies

4. Shell Programming and Scripting

right trim at end of file

Hi, I'have a file as below I need to replace the comma at the end of the file into ) as below Thanks (1 Reply)
Discussion started by: dvah
1 Replies

5. Shell Programming and Scripting

right trim at end of file

Hi, I'have a file as below I need to replace the comma at the end of the file into ) as below Thanks (0 Replies)
Discussion started by: dvah
0 Replies

6. Shell Programming and Scripting

Searching for file names in a directory while ignoring certain file names

Sun Solaris Unix Question Haven't been able to find any solution for this situation. Let's just say the file names listed below exist in a directory. I want the find command to find all files in this directory but at the same time I want to eliminate certain file names or files with certain... (2 Replies)
Discussion started by: 2reperry
2 Replies

7. Shell Programming and Scripting

Trim pathname of the file

I am capturing the files in a directory to an array. I have 2 arrays with list of files in two different directories. Both the directories are supposed to have the same number of files and filenames. I want to check that the same file exists in both the directories. After I capture the... (1 Reply)
Discussion started by: Sangtha
1 Replies

8. Shell Programming and Scripting

trim spaces in a file

Hi, I'm new to shell programming. Need some help in the following requirement: I have a file origFile.txt with values: origFile.txt .00~ 145416.02~ xyz~ ram kishor .35~ 765.76~ anh reid~ kishna kerry Now each row in the file has value for 4 columns with "~" as... (7 Replies)
Discussion started by: badrimohanty
7 Replies

9. UNIX for Dummies Questions & Answers

trim file

Hi, I have a 6G log , which is unusual to read and I want to minimize it by removing some part on the upper portion( around 4GB). what should i do? can you please help me? thanks. (1 Reply)
Discussion started by: tungaw2004
1 Replies

10. AIX

trim file name extension????

Dear Chaps, What will I do if, I am not sure about the length of the file name, but only one thing that I want to remove only the last extension. e.g. abcdXXXXXX.pqrXXXXX.asc (X is any character) I want to trim only .asc (or,watever) so that resultant file name would be like... (1 Reply)
Discussion started by: vishal_ranjan
1 Replies
Login or Register to Ask a Question