Regex/sed - matching any char,space,underscore between : and /


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex/sed - matching any char,space,underscore between : and /
# 1  
Old 02-18-2009
Regex/sed - matching any char,space,underscore between : and /

trying to remove the portion in red:

Data:

mds_ar/bin/uedw92wp.ksh: $AI_SQL/wkly.sql
mds_ar/bin/uedw92wp.ksh: $EDW_TMP/wkly.sql

output to be:

mds_ar/bin/uedw92wp.ksh: wkly.sql
mds_ar/bin/uedw92wp.ksh: wkly.sql

SED i'm trying to use:


Code:
sed 's/:[$a-zA-Z_ ]+\//: /g' input_file.dat > output_file.dat




from what i understand of Regex i've coded means:
  1. look for : followed by any lower/upper case character, including $ and spaces followed by / and replace with :space
What am i missing?
# 2  
Old 02-18-2009
Code:
echo 'mds_ar/bin/uedw92wp.ksh: $AI_SQL/wkly.sql' | sed 's#^\([^:][^:]*\):[^/][^/]*/\(.*\)#\1: \2#'
OR
echo 'mds_ar/bin/uedw92wp.ksh: $AI_SQL/wkly.sql' | sed 's#:[^/][^/]*/#: #'

# 3  
Old 02-18-2009
Your code is correct but for one char:
Code:
sed 's/:[$a-zA-Z_ ]\+\//: /g' input_file.dat > output_file.dat

sed has the strange thing that it needs it qualifiers escaped, otherwise it treads it as characters.
# 4  
Old 02-18-2009
This seems to work
Code:
 sed 's/:[ $A-Za-z]\//:/'

Code:
echo "mds_ar/bin/uedw92wp.ksh: $AI_SQL/wkly.sql
mds_ar/bin/uedw92wp.ksh: $EDW_TMP/wkly.sql" |
  sed 's/:[ $A-Za-z]\//:/'

Code:
csadev:/home/jmcnama> t.sh
mds_ar/bin/uedw92wp.ksh:wkly.sql
mds_ar/bin/uedw92wp.ksh:wkly.sql

# 5  
Old 02-18-2009
Quote:
Originally Posted by jim mcnamara
This seems to work
Code:
 sed 's/:[ $A-Za-z]\//:/'

Code:
echo "mds_ar/bin/uedw92wp.ksh: $AI_SQL/wkly.sql
mds_ar/bin/uedw92wp.ksh: $EDW_TMP/wkly.sql" |
  sed 's/:[ $A-Za-z]\//:/'

Code:
csadev:/home/jmcnama> t.sh
mds_ar/bin/uedw92wp.ksh:wkly.sql
mds_ar/bin/uedw92wp.ksh:wkly.sql

Doesn't work on Solaris - you're looking for ONE char between ':' and '/':
Code:
$ echo 'mds_ar/bin/uedw92wp.ksh: $AI_SQL/wkly.sql' | sed 's/:[ $A-Za-z]\//:/'
mds_ar/bin/uedw92wp.ksh: $AI_SQL/wkly.sql

# 6  
Old 02-18-2009
Yes, the + means more characters in the same character class.
# 7  
Old 02-18-2009
Quote:
Originally Posted by jim mcnamara
Yes, the + means more characters in the same character class.
Right, but you don't have it Smilie
BTW, not all sed-s support '+' - Solaris' version doesn't - have to simulate it with '[c][c]*'.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed: -e expression #1, char 20: unterminated address regex

I am trying to add word in last of particular line. the same command syntex is running on prompt. but in bash script give error."sed: -e expression #1, char 20: unterminated address regex" Please help. for i in `cat servername`; do ssh -q -t root@$i sed -i '/simple_allow_groups =/s/$/,... (4 Replies)
Discussion started by: yash_message
4 Replies

2. Shell Programming and Scripting

Replace a space with underscore in a file

i have a file with following data. { EqName "Tan 1" .... .... } { EqName "Sin 2" ... ... } I have to replace the value of EqName to Tan_1 and Sin_2 in file.Can i use sed or awk ? cat file|grep EqName|awk '{print $2 $3}'|sed -i 's//_/g' I tried with this but it... (2 Replies)
Discussion started by: Jag02
2 Replies

3. Shell Programming and Scripting

Sed: -e expression #1, char 16: unterminated address regex

I am trying to grep for a particular text (Do action on cell BL330) in a text file(sample.gz) which is searched in the content filtered by date+timestamp (2016-09-14 01:09:56,796 to 2016-09-15 04:10:29,719) on a remote machine and finally write the output into a output file on a local machine. ... (23 Replies)
Discussion started by: rbadveti
23 Replies

4. Shell Programming and Scripting

Awk, function of underscore char.

Hello Friends, I would appreciate so much if you could explain how the underscores works at the following code? Sorry if it sounds a bit novice question. awk -F',' 'NR==FNR{_=1;next}!_{print}' exclude infile KR, Eagle (6 Replies)
Discussion started by: EAGL€
6 Replies

5. Shell Programming and Scripting

SED: Pattern repitition regex matching

Fairly straightforward, but I'm having an awful time getting what I thought was a simple regex to work. I'll give the command I was playing with, and I'm aware why this one doesn't work (the 1,3 is off the A-Z, not the whole expression), I just don't know what the fix is: Actual Output(s): $... (5 Replies)
Discussion started by: Vryali
5 Replies

6. Shell Programming and Scripting

Using of gsub function in AWK to replace space by underscore

I must design a UNIX script to monitor files whose size is over a threshold of 5 MB in a specific UNIX directory I meet a problem during the for loop in my script. Some file names contain spaces. ls -lrt | awk '$5>=5000000 && length($8)==5 {gsub(/ /,"_",$9); print};' -rw-r--r-- 1 was61 ... (2 Replies)
Discussion started by: Scofield38
2 Replies

7. UNIX for Dummies Questions & Answers

blank space in regex pattern using sed

why does sed 's/.* //' show the last word in a line and sed 's/ .*//' show the first word in a line? How is that blank space before or after the ".*" being interpreted in the regex? i would think the first example would delete the first word and the next example would delete the second... (1 Reply)
Discussion started by: glev2005
1 Replies

8. UNIX for Advanced & Expert Users

Sed - add text to start of line if 1st char anything but space

Problem: I have a lot of files, the files first line should always have 4 spaces before any text. Occasionally some of the files will miss the leading spaces and it's a problem. This is only in the first line. So if there are 4 spaces then text, do nothing. If there are not 4 spaces, add 4... (2 Replies)
Discussion started by: Vryali
2 Replies

9. UNIX for Dummies Questions & Answers

Replacing underscore with white space after comma.

Hi all, I'm hoping you can help.. I've used this forum a couple of times and I am back again now i've moved onto something more complex (for me!) I have some data which looks like: "AL1_1,AL1_1," "AL1_1.AL1_1A.AL1_1AE,AL1_1AE," "AL1_1.AL1_1A.AL1_1AG,AL1_1AG," "AL1_1.AL1_1A.AL1_1AJ,AL1_1AJ,"... (10 Replies)
Discussion started by: gman
10 Replies

10. Shell Programming and Scripting

sed - print only matching regex

Hi folks, Lets say I have the following text file: name, lastname, 1234, name.lastname@test.com name1, lastname1, name2.lastname2@test.com, 2345 name, 3456, lastname, name3.lastname3@test.com 4567, name, lastname, name4.lastname4@test.com I now need the following output: 1234... (5 Replies)
Discussion started by: domi55
5 Replies
Login or Register to Ask a Question