how to remove this substring?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to remove this substring?
# 1  
Old 03-11-2011
Question how to remove this substring?

Hello,

I need to remove a substring from a string in a ksh script, the string is like this:
Code:
LOCATION=+DATADG1/CMSPRD1/datafile/system.235.456721

or
Code:
LOCATION=/u03/oradata/CMSPRD/SYSTEM.dbf

I need to strip the last file name from that path, and get:
Code:
+DATADG1/CMSPRD1/datafile

or,
Code:
/u03/oradata/CMSPRD

I experimented with
Code:
expr index "$LOCATION" SYSTEM

trying to get the position of the "system" string and then use
Code:
substr $LOCATION $position

but the index part didn't work to begin with.

I also experimented with:
Code:
echo ${LOCATION#system}
echo ${LOCATION%%SYSTEM}

they didn't work to substract "sytem" or "SYSTEM" (they could be either lower case or upper case).

Your help is much appreciated.

-Jay

Last edited by Franklin52; 03-11-2011 at 01:19 PM.. Reason: Please use code tags, thank you
# 2  
Old 03-11-2011
Try DIR=$(basedir $STRING)
# 3  
Old 03-11-2011
Or you can do something like:
Code:
string='LOCATION=/u03/oradata/CMSPRD/SYSTEM.dbf'
newstring=$(echo "$string" | sed 's!.*=\(.*\)/.*!\1!')

# 4  
Old 03-11-2011
Quote:
Originally Posted by seafan
I also experimented with:
Code:
echo ${LOCATION#system}
echo ${LOCATION%%SYSTEM}

they didn't work to substract "sytem" or "SYSTEM" (they could be either lower case or upper case).
You can remove the last "/" and everything that follows it with:
Code:
echo "${LOCATION%/*}"

Regards,
Alister
# 5  
Old 03-11-2011
Thanks for your quick reply. It looks that I don't have basedir in ksh:

$ basedir $LOC
ksh: basedir: not found

but I have basename:

LOC=/db1/data/CMSDEV/SYSTEM.dbf

$ print $(basename ${LOC})
SYSTEM.dbf

That gets me the file name, while I need the dir path/name.
# 6  
Old 03-11-2011
Code:
string='LOCATION=/u03/oradata/CMSPRD/SYSTEM.dbf'
x=${string#*=}
newstring=${x%/*}

edit: Misunderstood question.

Try:
Code:
echo ${LOCATION%/*}


Last edited by Franklin52; 03-11-2011 at 02:03 PM..
# 7  
Old 03-11-2011
echo "${LOCATION%/*}"
returns empty/null

---------- Post updated at 12:09 PM ---------- Previous update was at 11:44 AM ----------

this returns empty/null as well:

$ echo "$LOCATION" | sed 's!.*=\(.*\)/.*!\1!'

which makes me wonder if there is some shell env variable that needs to be set for this and the previous

echo "${LOCATION%/*}"

?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove lines matching a substring in a specific column

Dear group, I have following input text file: Brit 2016 11 18 12 00 10 1.485,00 EUR Brit 2016 11 18 12 00 10 142,64 EUR Brit 2016 11 18 12 00 10 19,80 EUR Brit 2016 11 18 12 00 10 545,00 EUR Brit 2016 11 18 12 00 10 6.450,00 EUR... (3 Replies)
Discussion started by: gfhsd
3 Replies

2. UNIX for Dummies Questions & Answers

Remove when substring meets condition

Hi Masters, I need to remove lines when date format is below certain date My file input 20140906|ALASKA|USASEL|TARPUNG|2014-03-01|82176614040|20|1 20140906|ALASKA|USATENG|CRUIEX|2014-08-01|81267079997|5|0 20140906|ALASKA|USASEL|CRUIEMBANG|2013-10-01|82280779814|9|0... (4 Replies)
Discussion started by: radius
4 Replies

3. Shell Programming and Scripting

Using sed, awk or perl to remove substring of all lines except the first

Greetings All, I would like to find all occurences of a pattern and delete a substring from the all matching lines EXCEPT the first. For example: 1234::group:user1,user2,user3,blah1,blah2,blah3 2222::othergroup:user9,user8 4444::othergroup2:user3,blah,blah,user1 1234::group3:user5,user1 ... (11 Replies)
Discussion started by: jacksolm
11 Replies

4. Shell Programming and Scripting

Remove a substring from string

Good morning friends, how can i remove a string with linux scripting from a file? In specific i want to remove from a file all the tweet names and links eg @aerta and links such as http://dst.co/pIiu3i9c Thanx!!! (4 Replies)
Discussion started by: paladinaeon
4 Replies

5. UNIX for Dummies Questions & Answers

Getting Substring

Hi, I hav a string lets say aa.txt:bb:txt length of the string can vary.. I have to keep the token inside a array and the delimiter is : plz send me the code (2 Replies)
Discussion started by: Deekay.p
2 Replies

6. Shell Programming and Scripting

substring

I have a string '<Hi>abc</Hi>" How to print "abc" (6 Replies)
Discussion started by: sandy1028
6 Replies

7. Shell Programming and Scripting

how to remove a substring

I want to modify a string in shell script (/bin/sh). Could you please help me. I have a variable which can store the values like below. v10.5.2.1p001C_TN1 or v10.5.2.1p002_TN1 I have to search first whether ‘p' character exists into the string which is stored in a variable..... (5 Replies)
Discussion started by: samtekdallas
5 Replies

8. UNIX for Dummies Questions & Answers

Need help with substring

I need to check the occurrence of one string within another. code ******************** if ;then do something done ******************** Thanks (7 Replies)
Discussion started by: w020637
7 Replies

9. Shell Programming and Scripting

Substring HELP!

Hi, I am trying to do something which I thought was very simple but still being a beginner, has proved not to be. Input: val1 val2 val3 val4 val5 val6 . . . etc Desired Output: Every row in which value of val6 is a number starting with 0.0 or contains a capital E. The input... (2 Replies)
Discussion started by: awknerd
2 Replies

10. UNIX for Dummies Questions & Answers

substring

Hi, I have a value of a filepath in a variable DATAFILE with value as "customtop/gpsore37/gepspo/1.0/bin/ashoka.csv ". Now i want the value of last 4 charcters in to another variable. That is EXTENSION = .csv How can i do this in Shell scripting Thanks in advance Alla Kishore (8 Replies)
Discussion started by: alla.kishore
8 Replies
Login or Register to Ask a Question