|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
How to specify beginning-of-line/end-of-line characters inside a regex range
How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: Code:
sed 's/\([|^]\)/<do something here>/g' file1 It is obviously interpreting ^ as the literal caret character. Any way out? |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
This works on solaris using ksh93: Code:
$ cat x.dat kldfhvlkjsdhv ;aslkflksdfj sadfkjlskdjf gcw|test123 gcw^test456 ;lksdkhdsf ;lkhaddhb hdashdkj $ sed 's/[|^]test/found_it/' x.dat kldfhvlkjsdhv ;aslkflksdfj sadfkjlskdjf gcwfound_it123 gcwfound_it456 ;lksdkhdsf ;lkhaddhb hdashdkj $ |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
I don't think I made myself clear. Let me give an example. Suppose the file I have is: Code:
testABCD jklmn|testDEFGH klmnoptestABCD I want the sed command to replace only the first two instances of the string "test" with "found_it", i.e. the output should contain: Code:
found_itABCD jklmn|found_itDEFGH klmnoptestABCD i.e. the string test should only be replaced with found_it if it occurs after a pipe or at the beginning of the line. I suppose this can be achieved with : Code:
sed -e 's/^test/found_it/g' -e 's/|test/found_it/g' file1 but shouldn't this be achievable using regex patterns too? |
|
#4
|
||||
|
||||
|
I believe some versions of sed allow a logical or. Mine does not. Try this: Code:
sed 's/^test\||test/found_it/g' filename If portability is a concern, you might want to stick with something like this: Code:
sed 's/|test/FOUND/g;s/^test/FOUND/g' filename |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Regex for beginning of line until a comma | glev2005 | UNIX for Dummies Questions & Answers | 6 | 02-01-2011 09:41 PM |
| reading a file inside awk and processing line by line | Anteus | Shell Programming and Scripting | 6 | 06-22-2009 11:05 AM |
| Deleting Characters at specific position in a line if the line is certain length | Cailet | Shell Programming and Scripting | 10 | 12-17-2008 01:41 PM |
| sed: delete regex line and next line if blank | one71 | Shell Programming and Scripting | 2 | 09-18-2008 05:53 AM |
| Unix Script with line number at beginning of each line. | mascorro | Shell Programming and Scripting | 5 | 06-19-2006 04:34 PM |
|
|