Solved

Regular Expression (sed editor)

Posted on 2002-07-22
17
379 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:alexgor
  • 8
  • 8
17 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7171202
sed does not know about \t, you have to use a real TAB
also remove the , in the brackets
0
 

Author Comment

by:alexgor
ID: 7171239
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 7171260
sed 's/[TAB ]source/destination/g'
0
 

Author Comment

by:alexgor
ID: 7171321
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.


0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7171330
sed 's/[TAB ]*source/destination/g' tmp.txt
# hope you used the TAB key, and not the string literal TAB
0
 

Author Comment

by:alexgor
ID: 7171488
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.




0
 

Author Comment

by:alexgor
ID: 7171493
In file tmp.txt in the last comment
there is space before beginning of the second line

+-----------------------+
|      source #TAB
| source       #SPACE
|source        #Nothing
+------------------------+



0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7171501
  sed 's/[TAB ]*source/destination/g' tmp.txt
still should do it, but you may try:
   sed 's/\([TAB ]*source\)/destination/g' tmp.txt
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:alexgor
ID: 7171664
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

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7172223
sed 's/[TAB ][TAB ]*source/destination/g' tmp.txt
0
 

Author Comment

by:alexgor
ID: 7173658
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




0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7173680
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
0
 

Author Comment

by:alexgor
ID: 7183347
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





0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7183751
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)
0
 

Author Comment

by:alexgor
ID: 7183792
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
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 500 total points
ID: 7183812
Linux, and your shell is bash ... damn, you need to key in the TAB as follows:

     Ctrl-V TAB

means hit Ctrl key together with v key, then hit TAB key
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7184807
things could be so simple, just look at my first 2 comments :-))
Tnaks for the A.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
cant install flashplugins for firefox in freebsd 8 9 45
sudo logging 5 80
OpenLDAP Proxy to Active Directy 6 214
How do disable only TLSv1.0 in Oracle Sun One 7.1 Server 9 76
When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now