CUT file name | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

CUT file name

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-26-2013
Aswex's Avatar
Aswex Aswex is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 13 November 2013, 4:20 AM EST
Location: France - EU
Posts: 173
Thanks: 73
Thanked 0 Times in 0 Posts
CUT file name

Hello,

I feel so stupid to not be able to found out how to do this ... I hope you may help me.

here is the file name exemple :


Code:
f610_gen_o_t_MASTER_AS_YB_20130324.csv

here is what I need to keep


Code:
f610_gen_o_t_EADSID_AS_YB_

thanks
Sponsored Links
    #2  
Old 03-26-2013
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 17 April 2014, 10:17 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,638
Thanks: 177
Thanked 607 Times in 565 Posts
Assuming there is a typo in your example output:

Code:
% s=f610_gen_o_t_MASTER_AS_YB_20130324.csv
% printf '%s\n' "${s%_*}_"
f610_gen_o_t_MASTER_AS_YB_

Sponsored Links
    #3  
Old 03-26-2013
Aswex's Avatar
Aswex Aswex is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 13 November 2013, 4:20 AM EST
Location: France - EU
Posts: 173
Thanks: 73
Thanked 0 Times in 0 Posts
Thanks Radoulov,

It doesn't work for me as I am getting a LOG file that contain all files like this :


Code:
f610_gen_o_t_MASTER_AS_YB_20130324.csv
f610_gen_o_t_MASTER_AS_TT_20130323.csv
f610_gen_o_t_MASTER_AS_LL_20130322.csv
f610_gen_o_t_MASTER_AS_OO_20130325.csv

I am looking something like :


Code:
cat $LOGfile | cut last part to only keep something like


Code:
f610_gen_o_t_MASTER_AS_YB_
f610_gen_o_t_MASTER_AS_TT_
f610_gen_o_t_MASTER_AS_LL_
f610_gen_o_t_MASTER_AS_OO_

Thanks
    #4  
Old 03-26-2013
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 17 April 2014, 10:17 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,638
Thanks: 177
Thanked 607 Times in 565 Posts

Code:
sed 's/[0-9]*\.csv$//' infile

And perhaps in most cases this will be sufficient:

Code:
sed 's/[0-9]*.csv//' infile

or:

Code:
sed 's/.\{12\}$//' infile

Actually, for fixed length prefixes you could use cut -c1-n
where n is the number of characters:

Code:
cut -c1-26 infile


Last edited by radoulov; 03-26-2013 at 09:16 AM..
The Following User Says Thank You to radoulov For This Useful Post:
Aswex (04-01-2013)
Sponsored Links
    #5  
Old 03-26-2013
rangarasan's Avatar
rangarasan rangarasan is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 11 April 2014, 8:41 AM EDT
Location: Chennai, India
Posts: 494
Thanks: 9
Thanked 124 Times in 120 Posts
Quote:
Originally Posted by Aswex View Post
Thanks Radoulov,

It doesn't work for me as I am getting a LOG file that contain all files like this :


Code:
f610_gen_o_t_MASTER_AS_YB_20130324.csv
f610_gen_o_t_MASTER_AS_TT_20130323.csv
f610_gen_o_t_MASTER_AS_LL_20130322.csv
f610_gen_o_t_MASTER_AS_OO_20130325.csv

I am looking something like :


Code:
cat $LOGfile | cut last part to only keep something like


Code:
f610_gen_o_t_MASTER_AS_YB_
f610_gen_o_t_MASTER_AS_TT_
f610_gen_o_t_MASTER_AS_LL_
f610_gen_o_t_MASTER_AS_OO_

Thanks
You have to do some more bit with radoulov's solution like this.

Code:
while read line
do
   printf '%s\n' "${line%_*}_"
done <"input_file"

(or)

Try this sed,

Code:
sed 's/_[^_]*$/_/' input_file

Cheers,
Ranga
The Following User Says Thank You to rangarasan For This Useful Post:
Aswex (04-01-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help required the cut the whole contents from one file and paste it into new file acidburn_007 Shell Programming and Scripting 4 06-13-2012 03:23 AM
cut the variable from the line and use it to find the file and read the content of that file rohit22hamirpur Shell Programming and Scripting 6 10-30-2011 03:05 AM
cut lines from log file and save it another file tonypalokkaran Shell Programming and Scripting 1 06-06-2011 05:17 AM
Creating file from an existing file using CUT, is it the best option? abhishekakaomi Shell Programming and Scripting 7 05-31-2011 03:39 AM



All times are GMT -4. The time now is 11:26 AM.