Visit Our UNIX and Linux User Community

How to specify beginning-of-line/end-of-line characters inside a regex range

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to specify beginning-of-line/end-of-line characters inside a regex range
# 1  
Old 06-20-2012
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:

sed 's/\([|^]\)/<do something here>/g' file1

It is obviously interpreting ^ as the literal caret character. Any way out?
# 2  
Old 06-20-2012
This works on solaris using ksh93:

$ cat x.dat
$ sed 's/[|^]test/found_it/' x.dat

# 3  
Old 06-20-2012
I don't think I made myself clear. Let me give an example. Suppose the file I have is:


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:


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 :

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  
Old 06-20-2012
I believe some versions of sed allow a logical or. Mine does not. Try this:

sed 's/^test\||test/found_it/g' filename

If portability is a concern, you might want to stick with something like this:
sed 's/|test/FOUND/g;s/^test/FOUND/g' filename


Previous Thread | Next Thread
Test Your Knowledge in Computers #57
Difficulty: Easy
In a Database Management System (DBMS), the content and the location of the data is defined by the meta data.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Inserting n characters to beginning of line if match

I would like to insert n number of characters at the beginning of each line that starts with a given character. If possible, I would be most appreciative for a sed or awk solution. Given the data below, I would like to be able to insert either 125 spaces or 125 "-" at the beginning of every line... (6 Replies)
Discussion started by: jvoot
6 Replies

2. Shell Programming and Scripting

Add new line at beginning and end of a file

Hi, I have a specific requirement to add text at the beginning and end of a plain text file. I tried to use "sed" with '1i' and '$a' flags but these required two separate "sed" commands separated with "|". I am looking for some command/option to join these two in single command parameter. ... (6 Replies)
Discussion started by: bhupinder08
6 Replies

3. Shell Programming and Scripting

[Solved] Inserting a line beginning with regex

I have been trying to insert a line after a regex but I can't do it. Here is the code I am using: cat saved_doc SET type = type1 SET type = STORE = y /vol/san_e1 /vol/san_5 /vol/san_e9 /vol/san_e11 /vol/san_e12 /vol/san_e13 /vol/san_e14 /vol/san_e16 /vol/san_e17 /vol/san_e18... (4 Replies)
Discussion started by: newbie2010
4 Replies

4. UNIX for Dummies Questions & Answers

sed - Add a variable line to the end of a block beginning with a regex

Hi, Need some help with sed. I have a file that has sections : e.g. a=blah b=blah d=blah e=blah There's many sections in the file. (1 Reply)
Discussion started by: andyatit
1 Replies

5. UNIX for Dummies Questions & Answers

vim copy line and paste at the beginning, middle, and end of another line

How would you do vim copy line and paste at the beginning, middle, and end of another line. I know yy copies the whole line and p pastes the whole line, but on its own separate line. Sometimes I would like to copy a line to the beginning, middle, or end of another line. I would think this would be... (3 Replies)
Discussion started by: cokedude
3 Replies

6. Shell Programming and Scripting

trying to add text to beginning and end of each line

Well here goes: I tried to write a batch file that adds a specific fixed text to each line of an already existing text file. for the adding text infront of each line I tried this: for /F "delims=" %%j in (list.txt) do echo.STARTTEXT\%%j >> list.txt for adding text after each line I... (0 Replies)
Discussion started by: pasc
0 Replies

7. UNIX for Dummies Questions & Answers

Regex for beginning of line until a comma

What is a regex for "the dalai lama, his holiness the" that would just grab "the dalai lama" and one that would just grab "his holiness the"? Both should exclude the comma.. I was trying '^.*' and many variants with no luck. (6 Replies)
Discussion started by: glev2005
6 Replies

8. Shell Programming and Scripting

adding characters end of line where line begins with..

Hi all, using VI, can anyone tell me how to add some characters onto the end of a line where the line begins with certain charactars eg a,b,c,......., r,s,t,........, a,b,c,......., all lines in the above example starting with a,b,c, I want to add an x at the end of the line so the... (6 Replies)
Discussion started by: satnamx
6 Replies

9. Shell Programming and Scripting

Get the 1st 99 characters and add new line feed at the end of the line

I have a file with varying record length in it. I need to reformat this file so that each line will have a length of 100 characters (99 characters + the line feed). AU * A01 EXPENSE 6990370000 CWF SUBC TRAVEL & MISC MY * A02 RESALE 6990788000 Y... (3 Replies)
Discussion started by: udelalv
3 Replies

10. Shell Programming and Scripting

Insert two strings at the beginning and at the end of each line of a file

Hi, excuse me for my poor english. My problem is that: I have a File i want to add to each line of that file two strings: one at the beginning of the line, one at the ending. string1="abcd" string2="efgh" i want $string1 content $string2 for each line. Is that possible? (3 Replies)
Discussion started by: Linux-fueled
3 Replies

Featured Tech Videos