alexgor
asked on
Regular Expression (sed editor)
Hello !
In file "tmp.txt" i want to replace each string
"<TAB or SPACE>source" by string "destination.
I have to do it using sed command.
Following command doesn't work:
sed s/"[\t, ]source"/"destinations"/g tmp.txt
Can you please help?
Txs,
Alex
In file "tmp.txt" i want to replace each string
"<TAB or SPACE>source" by string "destination.
I have to do it using sed command.
Following command doesn't work:
sed s/"[\t, ]source"/"destinations"/g tmp.txt
Can you please help?
Txs,
Alex
ASKER
Hello!
I have created file tmp.txt
+-----------------------+
source
source
+------------------------+
On this file have run following command:
>>sed s/"[TAB ]source"/"destination"/g tmp.txt
I have received following output:
+-----------------------+
source
destination
+------------------------+
In other words string "<TAB>source" was not replaced.
Thanks.
I have created file tmp.txt
+-----------------------+
source
source
+------------------------+
On this file have run following command:
>>sed s/"[TAB ]source"/"destination"/g tmp.txt
I have received following output:
+-----------------------+
source
destination
+------------------------+
In other words string "<TAB>source" was not replaced.
Thanks.
sed 's/[TAB ]source/destination/g'
ASKER
When command
>>sed 's/[TAB ]source/destination/g' tmp.txt
is used the results is same as for
>>sed s/"[TAB ]source"/"destination"/g tmp.txt
command
Thanks.
>>sed 's/[TAB ]source/destination/g' tmp.txt
is used the results is same as for
>>sed s/"[TAB ]source"/"destination"/g tmp.txt
command
Thanks.
sed 's/[TAB ]*source/destination/g' tmp.txt
# hope you used the TAB key, and not the string literal TAB
# hope you used the TAB key, and not the string literal TAB
ASKER
Hello !
I have created file tmp.txt
+-----------------------+
source #TAB
source #SPACE
source #Nothing
+------------------------+
Running command
>>sed 's/[TAB ]*source/destination/g' tmp.txt
creates following output
+-----------------------+
destination #TAB
destination #SPACE
destination #Nothing
+------------------------+
The desired output is:
+-----------------------+
destination #TAB
destination #SPACE
source #Nothing
+------------------------+
And as I understand following command should be used:
>>sed 's/[TAB ]source/destination/g' tmp.txt
But this command creates following output:
+-----------------------+
source #TAB
destination #SPACE
source #Nothing
+------------------------+
In other words string "<TAB>source" is not found.
Thanks.
I have created file tmp.txt
+-----------------------+
source #TAB
source #SPACE
source #Nothing
+------------------------+
Running command
>>sed 's/[TAB ]*source/destination/g' tmp.txt
creates following output
+-----------------------+
destination #TAB
destination #SPACE
destination #Nothing
+------------------------+
The desired output is:
+-----------------------+
destination #TAB
destination #SPACE
source #Nothing
+------------------------+
And as I understand following command should be used:
>>sed 's/[TAB ]source/destination/g' tmp.txt
But this command creates following output:
+-----------------------+
source #TAB
destination #SPACE
source #Nothing
+------------------------+
In other words string "<TAB>source" is not found.
Thanks.
ASKER
In file tmp.txt in the last comment
there is space before beginning of the second line
+-----------------------+
| source #TAB
| source #SPACE
|source #Nothing
+------------------------+
there is space before beginning of the second line
+-----------------------+
| source #TAB
| source #SPACE
|source #Nothing
+------------------------+
sed 's/[TAB ]*source/destination/g' tmp.txt
still should do it, but you may try:
sed 's/\([TAB ]*source\)/destination/g' tmp.txt
still should do it, but you may try:
sed 's/\([TAB ]*source\)/destination/g' tmp.txt
ASKER
Hello !
As I understand * signs
"Maximal match zero or more of previous char" and i
need "Maximal match one or more of previous char"
Thanks,
Alex
As I understand * signs
"Maximal match zero or more of previous char" and i
need "Maximal match one or more of previous char"
Thanks,
Alex
sed 's/[TAB ][TAB ]*source/destination/g' tmp.txt
ASKER
Hello AHoffman !
Thanks for your answer. My last problem is TAB matching.
If i create file tmp.txt
+------------------------- ---------- ------
source #There is tab before source
+------------------------- ---------- ------
and run following command
>>sed 's/[TAB]source/destination /g' tmp.txt
I receive following output
+------------------------- ---------- ------
source #There is tab before source
+------------------------- ---------- ------
When I expect following output
+------------------------- ---------- ------
destination #There is tab before source
+------------------------- ---------- ------
I have tryed also:
>>sed 's/\tsource/destination/g' tmp.txt
This command doesn't work too.
Thanks,
Alex
Thanks for your answer. My last problem is TAB matching.
If i create file tmp.txt
+-------------------------
source #There is tab before source
+-------------------------
and run following command
>>sed 's/[TAB]source/destination
I receive following output
+-------------------------
source #There is tab before source
+-------------------------
When I expect following output
+-------------------------
destination #There is tab before source
+-------------------------
I have tryed also:
>>sed 's/\tsource/destination/g'
This command doesn't work too.
Thanks,
Alex
again, \t does not work in sed (probably in modern GNU sed). Dot.
if sed 's/[TAB]source/destination /g' tmp.txt
did not substitute, then the pattern [TAB]source did not match. Dot. Try pattern TABsource or TAB*source
If all fail, have a look at your file what's really there:
od -c tmp.txt
if sed 's/[TAB]source/destination
did not substitute, then the pattern [TAB]source did not match. Dot. Try pattern TABsource or TAB*source
If all fail, have a look at your file what's really there:
od -c tmp.txt
ASKER
Hello AHoffman !
sed 's/[TAB]source/destination /g' tmp.txt
and
sed 's/TABsource/destination/g ' tmp.txt
and
sed 's/TAB*source/destination/ g' tmp.txt
don't work.
Output of command "od -c tmp.txt" is following:
0000000 \t s o u r c e # T h e r e i
0000020 s t a b b e f o r e s o u
0000040 r c e \n
0000044
Thanks,
Alex
sed 's/[TAB]source/destination
and
sed 's/TABsource/destination/g
and
sed 's/TAB*source/destination/
don't work.
Output of command "od -c tmp.txt" is following:
0000000 \t s o u r c e # T h e r e i
0000020 s t a b b e f o r e s o u
0000040 r c e \n
0000044
Thanks,
Alex
all variants work for me on any OS with your example
2 possibilities:
1. you have a corrupted sed
2. you did not use a real TAB (0x09) in your sed command
(still asked this silly question)
2 possibilities:
1. you have a corrupted sed
2. you did not use a real TAB (0x09) in your sed command
(still asked this silly question)
ASKER
Hello AHoffman !
Thank you for your patience, I am new Unix user.
I'm sorry, I have indeed used string literal TAB.
But if I try to use a real TAB key after
>>sed 's/
I receive following error:
"s/ not found"
Note that I'm using Linux OS, and I am doing it on PC,
not on Silicon Graphics or something like that.
Thanks,
Alex
Thank you for your patience, I am new Unix user.
I'm sorry, I have indeed used string literal TAB.
But if I try to use a real TAB key after
>>sed 's/
I receive following error:
"s/ not found"
Note that I'm using Linux OS, and I am doing it on PC,
not on Silicon Graphics or something like that.
Thanks,
Alex
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
things could be so simple, just look at my first 2 comments :-))
Tnaks for the A.
Tnaks for the A.
also remove the , in the brackets