Trim String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trim String
# 1  
Old 02-19-2013
Trim String

Hi,

I wish to grep everything before the last "/bin" in the following string

Code:
/opt/app/bin/app1/jdk150_07/bin/IA64N/java

Desired output: "/opt/app/bin/app1/jdk150_07"

Kindly help ...
# 2  
Old 02-19-2013
Code:
sed -n 's/\(.*\)\/bin.*/\1/p'

This looks a tad cleaner:
Code:
sed -n 's#\(.*\)/bin.*#\1#p'

# 3  
Old 02-19-2013
You could also use shell parameter expansion for this:
Code:
bash-2.03$ s=/opt/app/bin/app1/jdk150_07/bin/IA64N/java
bash-2.03$ printf '%s\n' "${s%/bin*}"
/opt/app/bin/app1/jdk150_07

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trim the data

i am passing below inputfils using while loop and ouput should be trim as below INPUT: src_SPS_d_Comment_Tran_File_20130417_001.dat src_SPS_d_Corp_Adv_Tran_File_20130417_001.dat src_SPS_d_Letter_Tran_File_20130417_001.dat src_SPS_d_Loan_Level_File_20130417_001.dat... (8 Replies)
Discussion started by: katakamvivek
8 Replies

2. Shell Programming and Scripting

TRIM a string in shell script

HI, I have a string "NZ-deploy-mode-1.15-Linux.x86_64.rpm" I want to get the string before -1 ie "NZ-deploy-mode" Input is "NZ-deploy-mode-1.15-Linux.x86_64.rpm" expected output is "NZ-deploy-mode" How can I do that in shell script? Thanks in advance. (6 Replies)
Discussion started by: Ananthdoss
6 Replies

3. Shell Programming and Scripting

How to trim a string in unix shell script

I am reading a string like $/folder1/folder2/filename from a file And I am storing it in a variable say $path. I want to store the path within single quotes in order to use the path within a command. if i set like path="'"$path"'" echo $path It is printing like ' $/folder1/folder2/filename'... (6 Replies)
Discussion started by: Shri123
6 Replies

4. Shell Programming and Scripting

To trim Certain field in a line of a file and replace the new string in that position

To trim 3rd field in for all the lines of a file and replace the modified string in that particular field. For example i have a file called Temp.txt having content Temp.txt ----------------- 100,234,M1234 400,234,K1734 300,345,T3456 ---------------- So the modified file output should... (4 Replies)
Discussion started by: rpadhi
4 Replies

5. Programming

How to trim the white space around a string in C program

I am coding a C program to read a plain text file. There are a lot of blank fields or a string with white spaces. I want to know is there such a function called trim() in C to clean the white space around a string? Or some other way can do this efficiently? Thanks. (18 Replies)
Discussion started by: hxm1303
18 Replies

6. UNIX for Dummies Questions & Answers

Trim String in 3rd Column in Tab Delimited File...SED/PERL/AWK?

Hey Everybody, I am having much trouble figuring this out, as I am not really a programmer..:mad: Datafile.txt Column0 Column1 Column2 ABC DEF xxxGHI I am running using WGET on a cronjob to grab a datafile, but I need to cut the first three characters from... (6 Replies)
Discussion started by: rickdini
6 Replies

7. Shell Programming and Scripting

Trim

Hello, I am passing a filename to a script to draw parameters from it. However, I want to use part of the filename as a parameter. The filename is transfer_ccf_3731_10.sh but I only need the 3731_10 part of it. Is this possible? Any help or suggestions would be appreciated! Regards, J. (4 Replies)
Discussion started by: JWilliams
4 Replies

8. UNIX for Dummies Questions & Answers

trim leading zero in certain column in a string

I would like to know how to trim leading zero only in certain column of of a string, example: hdhshdhdhd000012mmmm0002abc <===== before hdhshdhdhd 12mmmm 2abc <===== after Thanks for your help. (2 Replies)
Discussion started by: dngo
2 Replies

9. Shell Programming and Scripting

Reversing and trim a String through SHELL script

All I want to reverse and trim a string using UNIX shell scripts.Iam using Bourne shells. Can you help me? Thanx in advance Regards Deepak (5 Replies)
Discussion started by: DeepakXavier
5 Replies

10. Shell Programming and Scripting

trim whitespace?

I'm trying to find a command that will trim the white space off a string. e.g. $str = " stuf " $str = trim ( $str ) echo $str // ouput would just be stuf Thanks, Mark (4 Replies)
Discussion started by: msteudel
4 Replies
Login or Register to Ask a Question