SED with Wildcards and Special Characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED with Wildcards and Special Characters
# 1  
Old 06-30-2008
SED with Wildcards and Special Characters

Hi All,

Need you guys' help to achieve the following:

I have some strings and i wish to threw off the end part that's in the file path.

From:
/directoryname1/subdirectoryname1/abc.txt
/directoryname2/subdirectoryname2/defggf.txt

To:
/directoryname1/subdirectoryname1/
/directoryname2/subdirectoryname2/

I could not trigger the beginning of the string as they varies. Right now,
I'm using SED to try and catch /*.txt and repalce it with nothing but i don't seem to get it to work.

Code:
$path = /var/tmp/123.txt
echo $path | sed 's/\/*\.txt//'

Outputs:
/var/tmp/123

Code:
$path = /var/tmp/123.txt
echo $path | sed 's/\/\*txt//'

Outputs:
/var/tmp/123.txt (does nothing)


I'm just tyring to replace "/*.txt" to " "(nothing). Hope you guys can help me on that.
Thanks in advance.
# 2  
Old 06-30-2008
one way
Code:
# a=/directoryname1/subdirectoryname1/abc.txt
# echo ${a%*/*}
/directoryname1/subdirectoryname1

# 3  
Old 06-30-2008
or, your sed command can be revised to the following:
echo $path | sed 's/\/[^\/]*txt$//'
# 4  
Old 07-01-2008
Quote:
Originally Posted by cuitao
or, your sed command can be revised to the following:
echo $path | sed 's/\/[^\/]*txt$//'
many thanks cuitao, mind to explain
[^\/]*txt$ part?

isn't what everything inside the [] means that it is not equal to [/] and what is the dollar sign for?

Thanks
Smilie
# 5  
Old 07-01-2008
Quote:
Originally Posted by Radeon
many thanks cuitao, mind to explain
[^\/]*txt$ part?

isn't what everything inside the [] means that it is not equal to [/] and what is the dollar sign for?

Thanks
Smilie
your guess about the [] is correct. and the $ represents the end of the line.
# 6  
Old 07-01-2008
Quote:
Originally Posted by Radeon
...
From:
/directoryname1/subdirectoryname1/abc.txt
/directoryname2/subdirectoryname2/defggf.txt

To:
/directoryname1/subdirectoryname1/
/directoryname2/subdirectoryname2/
Look like the OP need the ending slash.
Code:
$ echo '/directoryname2/subdirectoryname2/defggf.txt' | awk -F'/' '{gsub($NF,"",$0)}1'
/directoryname2/subdirectoryname2/

The other solutions end like dirname,see basename(1).
Code:
$ a=/directoryname2/subdirectoryname2/defggf.txt
$ dirname $a
/directoryname2/subdirectoryname2

# 7  
Old 07-01-2008
another
Code:
echo '/directoryname2/subdirectoryname2/defggf.txt' | awk -F'/' '{$NF=""}1' OFS="/"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed in a while loop with special characters

I have the foolowing data file: File1 <p name="A">5004</p> <p name="B">5004</p> <p name="C">5004</p> <p name="A">15004</p> <p name="B">15004</p> <p name="C">15004</p> In a while loop using sed (100 of line need to be replace), I need the output to File3:... (2 Replies)
Discussion started by: bobo
2 Replies

2. Shell Programming and Scripting

sed special characters issues

I am dusting off the sed cobwebs and had a basic question: I have a file that contains: $firewall = "on"; $cache = "on"; $dataset{'mary had a little lamb'} = "on"; and want to only change the contents of what is between the single quotes: $dataset{'big bad wolf'} = "on"; I... (3 Replies)
Discussion started by: metallica1973
3 Replies

3. Shell Programming and Scripting

Escape special characters in SED

Need help in escaping special characters in sed command. Here is the the string which i am trying to find a replace with From :- REQUEST_TYPE=PIXEL&amp;MSG_ID={//MESSAGE_ID} To :- REQUEST_TYPE=PIXEL&amp;MSG_ID= X_EDELIVERY_MESSAGE_ID &amp; BATCH_ID= X_EDELIVERY_BATCH_ID Here is the sed command i am... (2 Replies)
Discussion started by: aakishore
2 Replies

4. Shell Programming and Scripting

SED with Special characters

Hello All Seeking the right one SED command. My attempt is: From orginal.txt by SED to target.txt sed -i "/('outbound-callerid/a\$ext->add($context, $exten, '', new ext_SipAddHeader('P-Preferred-Identity', '<sip:${CALLERID(nummer)}@carrier.com>'));" orginal.txtWhat am make wrong?:wall: ... (5 Replies)
Discussion started by: mdbinder
5 Replies

5. Shell Programming and Scripting

Using sed to replace special characters

Hi everyone I have file1 contains: '7832' ' 8765 6543 I want a sed command that will format as: '7832' , '8765' , '6543' I tried sed -e s/\'//g -e 's/^*//;s/*$//' file1 > file2 sed -e :a -e '$!N; s/\n/ /; ta' file2 which gives: 7832 8765 6543 I need some help to continue with... (5 Replies)
Discussion started by: nimo
5 Replies

6. Shell Programming and Scripting

sed with many special characters

I started with this: counter1=1 cp file.txt file_${counter1}.tmp while read name1 do echo $name1 counter2=`expr $counter1 + 1` sed /'${name1}'/d file_${counter1}.txt > file_${counter2}.txt counter1=`expr $counter1 + 1` done < source.txtsource.txt contains the... (1 Reply)
Discussion started by: lakanino
1 Replies

7. Shell Programming and Scripting

sed with special characters

Hi, I am reading a file (GC_JAR.log) which has entries like: 511725.629, 0.1122672 secs] 525268.975, 0.1240036 secs] 527181.835, 0.2068215 secs] 527914.287, 0.2884801 secs] 528457.134, 0.2548725 secs] I want to replace all the entries of "secs]" with just "secs" Thus, the output... (4 Replies)
Discussion started by: itzz.me
4 Replies

8. Shell Programming and Scripting

help on sed replacing special characters

Hello, I have a file with many lines with below format: \abc\\1234 jkl\\567 def\\345 \pqr\\567 \xyz\\234 Here, i need to do 2 things. 1. replace \\ with \ 2. remove starting \ so output to be as below: (11 Replies)
Discussion started by: prvnrk
11 Replies

9. Shell Programming and Scripting

Changing Special Characters Using Sed

Hi. Does anyone know how to use the sed command to change the special border characters on this .per file. I have to edit about 80 .per files. I need a sed script to change the below 3 and A characters. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ Test Islands, Office of Public Health -- WIC... (4 Replies)
Discussion started by: cstovall
4 Replies

10. Shell Programming and Scripting

awk/sed with special characters

i have this script that searches for a pattern. However it fails if the pattern includes some special characters. So far, it fails with the following strings: 1. -Cr 2. $Mj 3. H'412 would a sed or awk be more effective? i don't want the users to put the (\) during the search (they... (5 Replies)
Discussion started by: apalex
5 Replies
Login or Register to Ask a Question