truncate a string from the end


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting truncate a string from the end
# 1  
Old 03-22-2009
truncate a string from the end

hi, guys. I have a question.

If I have a long string like this:

8.0K:/home/test/brownj

How can I get a substring which starts from the last slash to the end of the string, so in this case, it will be brownj

Thank you very much for you time in advance

-Keyang
# 2  
Old 03-22-2009
if there are always 3 slashes piping into the following will work

Code:
awk -F / '{print $4}'

Not sure if that's what you wanted, but I'm a beginner too. Hope it helped a little

Last edited by mikejreading; 03-22-2009 at 09:30 PM.. Reason: Incomplete
# 3  
Old 03-22-2009
A better solution when what you want is the last field. The NF is the variable that holds the number of fields created when awk split the string using "/" as the delimiter.

Code:
string="8.0K:/home/test/brownj"

echo $string | awk -F "/" '{print $NF}'

# 4  
Old 03-23-2009
Hi,
the utility basename will give You the last portion of any "path like" string, so

Code:
basename 8.0K:/home/test/brownj

will give You brownj, even if it contains colons.

Best regards
/Lakris
# 5  
Old 03-23-2009
Or without external commands:

Code:
% s=8.0K:/home/test/brownj
% printf '%s\n' "${s##*/}"
brownj

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

2. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

3. Shell Programming and Scripting

How to truncate a string to x number characters?

Hello: I have a large file which contains lines like the following: 1/t123ab, &Xx:1:1234:12345:123456@ABCDEFG... at -$100.00% /t is a tab, spaces are as indicated the string "&Xx:1:1234:12345:123456$ABCDEFG..." has a slightly variable number of numbers and letters, but it always starts... (9 Replies)
Discussion started by: Tectona
9 Replies

4. Shell Programming and Scripting

truncate string to integer or decimal

From strings stored in variables, I need to isolate and use the first numerical value contained within them. I will need to know how to produce an integer as well as a floating point decimal. It needs to work on any string regardless of what types of characters (if any) are preceding or following... (3 Replies)
Discussion started by: bradlecat
3 Replies

5. Shell Programming and Scripting

Truncate all characters and numbers in string - using perl

hi, I have an data from file where it has 20110904 234516 <<hdd-10#console|0c.57,passed,5,28,READ,0,20822392,8,5,4,0,40,0,-1,0,29909,25000,835,3.3,0,0,0,0,implied,0,0,2011/9/5-2:3:17,2011/9/5-2:3:47,X292_0F15,TAP ,NQ09,J40LTG\r\r\n I want to remove characters till #console| i.e want... (1 Reply)
Discussion started by: asak
1 Replies

6. Shell Programming and Scripting

./TRUNCATE-PS_TXN.sh: line 54: syntax error: unexpected end of file

Hi All , My below script is for chacking vaule and then trucating table : ___________ test4@aceuatcs04:/u01/test4/SOLID/Testscript>cat TRUNCATE-PS_TXN.sh #-------------------------------------------------------------------- # Created by:Kaushlesh Yadav # Generated on: 15/07/2010 # Job... (4 Replies)
Discussion started by: kaushelsh168
4 Replies

7. Shell Programming and Scripting

Truncate string variable

Hi, I want to truncate a string variable, returned in the script. In perl I used the below and it worked. BRNo=BR12345 $BR = substr($BRNo, 2, 7) How can I do it in sh. Thanks ! (8 Replies)
Discussion started by: script2010
8 Replies

8. Shell Programming and Scripting

Truncate Specific Parts of a String

How do you truncate specific parts of a string. Example: 1 This is the string Goal: This is the string As you can see I'm trying to simply remove the first two characters of the string the number one and the space between the one and the word "this." Your help is appreciated. ... (8 Replies)
Discussion started by: royarellano
8 Replies

9. UNIX for Dummies Questions & Answers

I don't want to truncate trailing spaces and ^M at the end of line

I have a script wherein I access each line of the file using a FOR loop and then perform some operations in each line. The problem is each line that gets extracted in FOR loop truncates trailing blank spaces and control characters (^M) that is present at the end of each line. I don't wan this to... (5 Replies)
Discussion started by: Shobana_s
5 Replies

10. Shell Programming and Scripting

truncate/round down 2nd string print both

I have a file with a name and then a number: Apple 1.1 Apple 1.5 Apple 1.9 Banana 1.3 Banana 2.7 Banana 3.3 I'd like to print out the first string followed by the second number rounded down or truncated: Apple 1 Apple 1 Apple 1 Banana 1 Banana 2 Banana 3 I was trying: (3 Replies)
Discussion started by: dcfargo
3 Replies
Login or Register to Ask a Question