sed - extract string before "/" from variable- linux 2.6.9-89


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed - extract string before "/" from variable- linux 2.6.9-89
# 1  
Old 03-10-2010
sed - extract string before "/" from variable- linux 2.6.9-89

Hi,

I have a FTP script which gets called from a wrapper script that exports a variable having value as:

"/export/home/dips/logs/dipsSFTP_file1.log.YYYYMMDDHHMISS". I want to extract the file name "dipsSFTP_file1.log.YYYYMMDDHHMISS" and the dir path "/export/home/dips/logs/" from this variable separately.

I could extract the file name using:

Code:
 echo "/export/home/dips/logs/dipsSFTP_file1.log.YYYYMMDDHHMISS" | sed 's/.*\/\(.*\)/\1/'

but was not able to extract the dir path. Can you provide me the sed command which can do so?

Thanks in advance.
-dips
# 2  
Old 03-10-2010
try:
Code:
basename /export/home/dips/logs/dipsSFTP_file1.log.YYYYMMDDHHMISS
dirname /export/home/dips/logs/dipsSFTP_file1.log.YYYYMMDDHHMISS

# 3  
Old 03-10-2010
Hi, dips_ag:

You can just use the basename and dirname commands for this.
Code:
$ basename "/export/home/dips/logs/dipsSFTP_file1.log.YYYYMMDDHHMISS"
dipsSFTP_file1.log.YYYYMMDDHHMISS

$ dirname "/export/home/dips/logs/dipsSFTP_file1.log.YYYYMMDDHHMISS"
/export/home/dips/logs

If you're using a reasonably modern posix-compatible shell, you could also use its parameter expanding features:
Code:
$ var="/export/home/dips/logs/dipsSFTP_file1.log.YYYYMMDDHHMISS"

$ echo "${var##*/}"
dipsSFTP_file1.log.YYYYMMDDHHMISS

$ echo "${var%/*}"
/export/home/dips/logs

Please be aware the above shell parameter expansion approaches are not even close to being general purpose substitutes for basename and dirname; they just work for this particular case.

Last edited by alister; 03-10-2010 at 09:34 AM..
# 4  
Old 03-10-2010
Thanks anchal & alister. Both of your solutions worked!

-dips
# 5  
Old 03-10-2010
You're welcome, dip_ag.

In case you're still interested in the sed case, for that particular path you can extract the dirname with:
Code:
$ echo $var | sed 's/\/[^\/]*$//'
/export/home/dips/logs

... or, using a different regular expression delimiter to avoid having to escape forward slashes in the pattern ...
Code:
$ echo $var | sed 's|/[^/]*$||'
/export/home/dips/logs

Also, your filename SED can be simplified from
Code:
 sed 's/.*\/\(.*\)/\1/'

to
Code:
sed 's/.*\///'

or with a different delimiter to
Code:
sed 's|.*/||'

Please be aware the above SED commands are not even close to being general purpose substitutes for basename and dirname; they just work for this particular case.

Hope that helps,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract delta records using with "comm" and "sort" commands combination

Hi All, I have 2 pipe delimited files viz., file_old and file_new. I'm trying to compare these 2 files, and extract all the different rows between them into a new_file. comm -3 < sort file_old < sort file_new > new_file I am getting the below error: -ksh: sort: cannot open But if I do... (7 Replies)
Discussion started by: njny
7 Replies

2. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

3. Shell Programming and Scripting

Extract string between two delimeter " "

I want a string between two delimeter like " " from below file and also send send output to the file. Input File, 2007_08_07_IA-0100-014_"January".PDF 2007_08_07_IA-0100-031_"January February March April June July".PDF 2008-02-28_KR-1022-003_"January febuary march april may".CSV ... (0 Replies)
Discussion started by: Pratik Majithia
0 Replies

4. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

5. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

6. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

7. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

8. Shell Programming and Scripting

Extract Part of a "Word", using AWK or SED????

I have been lurking on this forum for some time now and appreciate Everyone's help. I need to find a way to get the SystemID from this XML file. The file is much larger than just this one line but I can grep and get this line Printed. But really just need the "systemid". <test123: prefintem... (9 Replies)
Discussion started by: elbombillo
9 Replies

9. Shell Programming and Scripting

Sed , Replace a "variable text" inside of a statement

Please Help... I am trying to manipulte the following line Before : <user:Account_Password>002786</user:Account_Password> the password is the "variable", i need to delete / omit the password in the file, (it occurs several thousand times) so the tag line looks like After:... (4 Replies)
Discussion started by: jackn7
4 Replies
Login or Register to Ask a Question