CUT command delimiter in reverse


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting CUT command delimiter in reverse
# 1  
Old 12-04-2010
CUT command delimiter in reverse

Hi,
I've a situation where,

Code:
 
a=xxx.yyy.zzz.txt
EXTN=`echo $a | cut -d . -f2`

Using the above code it delimites and will return "yyy.zzz.txt" to EXTN. But i need to get only the extension "txt". so as per the above code it delimits in the first "." itself. Can anyone help how to do this cut in
Quote:
reverse
so that i always get the "txt" as a result.

Thanks,
Vasanth.
# 2  
Old 12-04-2010
first, the command EXTN=`echo $a | cut -d . -f2` will return yyy

If you need get txt, thy this:

Code:
EXTN=`echo $a | cut -d . -f4`

or
Code:
EXTN=${a##*.}

# 3  
Old 12-04-2010
Code:
 
echo $a
xxx.yyy.zzz.txt
EXTN=$(echo $a | cut -d . -f 4)
echo "$EXTN"
txt

# 4  
Old 12-04-2010
Hi rdcwayx,

Thanks for you reply!
It works in both the way. But the 2nd one suits my situation well becuase somtime i can have only one .(dot) in my input or 2 or 3 .(dots) somtime. I'm little new... and i don't understand the below code Smilie

Quote:
EXTN=${a##*.}
It would be great if you can you explain me the above code.

Thanks,
Vasanth.
# 5  
Old 12-04-2010
It removes the greatest occurence of the pattern '*.' at the beginning of variable 'a'.
See all the parameter expansion posssibiities in : Bash Reference Manual
# 6  
Old 12-04-2010
Also, see POSIX version Shell Command Language to see what's possible in shells other than bash
# 7  
Old 12-04-2010
Thank You All for your guidance which was most helpful for me.
-Vasanth.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut command with dynamic passing of delimiter and position values

Hi All, We have a requirement of picking nth position value by using cut command. value would be delimited by any symbols. We have to pass delimited value and postition to get the value in a string. ex. echo "A,B,C,D,E" |cut -d "," -f3 echo "A|B|C|D|E"|cut -d "|" -f2 Kindly frame the... (5 Replies)
Discussion started by: KK230689
5 Replies

2. Shell Programming and Scripting

Problem in using cut command with pipe as a delimiter while using in a script

There is a text file in my project named as "mom.txt" in which i want to have contents like.................. LSCRM(Application Name): 1: This is my first application. 2: Today we did shell scripting automation for this app. 3: It was really a good fun in doing so. 4: Really good.| (Here i... (7 Replies)
Discussion started by: Abhijeet Anand
7 Replies

3. UNIX for Dummies Questions & Answers

How to cut part of a string in reverse?

Hi, how to cut part of a string sing delimiter in reverse input file 1,2,st-pa-tr-01,2,3,4, 2,3,ff-ht-05,6,7,8 how can i obtain strings till st-pa-tr ff-ht i.e cutting the last part og string -01 and -05 Thanks & Regards Nivi edit by bakunin: changed thread title (typo) (3 Replies)
Discussion started by: nivI
3 Replies

4. Shell Programming and Scripting

How to cut by delimiter, and delimiter can be anything except numbers?

Hi all, I have a number of strings like below: //mnt/autocor/43°13'(33")W/ and i'm trying to get the numbers in this string, for example 431333 please help thanks ahead (14 Replies)
Discussion started by: sunnydanniel
14 Replies

5. UNIX for Dummies Questions & Answers

set output delimiter as tab in cut command

I can not make it work, it prints \t rather than introduce tabs. cut -d "," -f 4,8 Samples.csv --output-delimiter="\t" | sort > out Since I am running this command within a shell script, I tried manually inserting tab in this command, still does not work. I am using bash shell Suggestions... (8 Replies)
Discussion started by: analyst
8 Replies

6. Shell Programming and Scripting

Cut or awk in reverse

I may be making this too hard on myself, but I'm trying to find a way that I can use a cut or awk string to always remove the last two delimited fields of a string. Say I have PackageName-U939393-8.2.3.4.s390x.rpm But the s390x could be any string w/o periods in it, x8664 for example,... (9 Replies)
Discussion started by: cbo0485
9 Replies

7. Shell Programming and Scripting

cut a field, but with reverse order

Hi Everyone, I have one a.txt: a b 001 c b b 002 c c c, not 002 c The output should be 001 002 002 If i use cut -f 3 -d' ', this does not work on the 3rd line, so i thought is any way to cut the field counting from the end? or any perl thing can do this?:confused: ... (3 Replies)
Discussion started by: jimmy_y
3 Replies

8. UNIX for Advanced & Expert Users

How can I use double character delimiter in the cut command

Hi All, Can the cut command have double character delimiter? If yes, how can we use it. if my data file contains : apple || mango || grapes i used cut -f1 -d"||" filename but got an error. Plz help.... Thanks. (1 Reply)
Discussion started by: AshishK
1 Replies

9. Shell Programming and Scripting

How reverse cut or read rows of lines

Hi, My records are like this BSC403_JAIN03|3153_TropicalFarm_LIMJM1-3_97| BSC403_JAIN03|3410_PantaiAceh_PCEHM1_4_97| BSC406_BMIN02|1433_JomHebohTV3_COW7M1_11_97| I want to extract the value before _97| This command BSC_ID=`echo $DATA | cut -f5 -d"_"` gives me _97|, 4, 11 and by... (16 Replies)
Discussion started by: doer
16 Replies

10. Shell Programming and Scripting

Dynamic delimiter in cut command

hello.. my prob is as follows: i have to read from a file which may have different formats depending upon the delimiter used in the data of the file.now i need that the user input the delimiter.the input delimiter is stored in a variable and is used on cut command to retrieve necessary... (3 Replies)
Discussion started by: tej.buch
3 Replies
Login or Register to Ask a Question