Help - Search - Members - Calendar
Full Version: Question regarding sed
Linuxhelp > Support > Technical Support
marbleman
a snippet of the source file
[quote]
~$ tail -5 testfile.txt
yogesh at lists.mydomain.com yogesh
yogesh-admin at lists.mydomain.com yogesh-admin
yogesh-bounces at lists.mydomain.com yogesh-bounces
yogesh-confirm at lists.mydomain.com yogesh-confirm
yogesh-join at lists.mydomain.com yogesh-join
[/quote]

snippit of what output should read follows

[quote]
INSERT INTO alias (address, goto) values ('everyone@newdomain.com', 'everyone@localhost')
INSERT INTO alias (address, goto) values ('everyone-admin@newdomain.com', 'everyone-admin@localhost')
INSERT INTO alias (address, goto) values ('everyone-bounces@newdomain.com', 'everyone-bounces@localhost')
INSERT INTO alias (address, goto) values ('everyone-confirm@newdomain.com', 'everyone-confirm@localhost')
INSERT INTO alias (address, goto) values ('everyone-join@newdomain.com', 'everyone-join@localhost')
[/quote]

Here is the command I have at the moment
CODE
cat testfile.txt|sed 's/yogesh/everyone/g'|sed 's/ at /@/g'|sed 's/lists.mydomain.com/newdomain.com/g'|awk '{print $1}'|sed "s/^/INSERT INTO alias (address, goto) values ('"/|sed "s/$/', 'something@localhost')/"

and here is the output
[quote]
INSERT INTO alias (address, goto) values ('everyone@marblemedia.com', 'something@localhost')
INSERT INTO alias (address, goto) values ('everyone-admin@marblemedia.com', 'something@localhost')
INSERT INTO alias (address, goto) values ('everyone-bounces@marblemedia.com', 'something@localhost')
INSERT INTO alias (address, goto) values ('everyone-confirm@marblemedia.com', 'something@localhost')
INSERT INTO alias (address, goto) values ('everyone-join@marblemedia.com', 'something@localhost')
[/quote]

I need to replace the string 'something@localhost' with the username from the first email address. I don't know how to do this, and I've tried playing with variations on sed, awk and xargs to plug that value in. I can't seem to get awk to work inside of a pair of ` when inside of a sed 's///" statement.

Any ideas on how to get the requested output string without using a text editor (there's thousands of entriesin the full file)? This should be easy but it escapes me :(

Thanks in advance for any input you can give me on evaluating regular expressions inside of sed.
-mm
markjr
When I get into a situation like this (where I can't get an obvious sed filter going), I just break out perl, which is pretty easy to use as a command line filter.

Try this:
CODE
$ perl -lne '/values \(.(.+)\@.+.,/;$x= $1;print$x;s/something\@localhost/$x/;print'< input.txt


Where input.txt is the datafile you posted. If I understood your request correctly, you get this:

CODE
INSERT INTO alias (address, goto) values ('everyone@marblemedia.com', 'everyone')
everyone-admin
INSERT INTO alias (address, goto) values ('everyone-admin@marblemedia.com', 'everyone-admin')
everyone-bounces
INSERT INTO alias (address, goto) values ('everyone-bounces@marblemedia.com', 'everyone-bounces')
everyone-confirm
INSERT INTO alias (address, goto) values ('everyone-confirm@marblemedia.com', 'everyone-confirm')
everyone-join
INSERT INTO alias (address, goto) values ('everyone-join@marblemedia.com', 'everyone-join')


QUOTE (marbleman @ Oct 23 2006, 11:04 AM) *
a snippet of the source file
snippit of what output should read follows
Here is the command I have at the moment
CODE
cat testfile.txt|sed 's/yogesh/everyone/g'|sed 's/ at /@/g'|sed 's/lists.mydomain.com/newdomain.com/g'|awk '{print $1}'|sed "s/^/INSERT INTO alias (address, goto) values ('"/|sed "s/$/', 'something@localhost')/"

and here is the output
I need to replace the string 'something@localhost' with the username from the first email address. I don't know how to do this, and I've tried playing with variations on sed, awk and xargs to plug that value in. I can't seem to get awk to work inside of a pair of ` when inside of a sed 's///" statement.

Any ideas on how to get the requested output string without using a text editor (there's thousands of entriesin the full file)? This should be easy but it escapes me sad.gif

Thanks in advance for any input you can give me on evaluating regular expressions inside of sed.
-mm
marbleman
excellent! Thanks so much!

I altered your command a bit to produce the result username@localhost
CODE
perl -lne '/values \(.(.+)\@.+.,/;$x= $1;s/something/$x/;print' < newtest.txt

QUOTE
INSERT INTO alias (address, goto) values ('everyone@newdomain.com', 'everyone@localhost')
INSERT INTO alias (address, goto) values ('everyone-admin@newdomain.com', 'everyone-admin@localhost')


Is there any way to pipe in the input to perl rather than using the redirector? I've never really used perl so I don't know where the switch would be. It's important to me to do it in a single command if only for the challenge wink.gif

Regards!
marbleman

QUOTE (markjr @ Oct 23 2006, 08:12 PM) *
When I get into a situation like this (where I can't get an obvious sed filter going), I just break out perl, which is pretty easy to use as a command line filter.

Try this:
CODE
$ perl -lne '/values \(.(.+)\@.+.,/;$x= $1;print$x;s/something\@localhost/$x/;print'< input.txt


Where input.txt is the datafile you posted. If I understood your request correctly, you get this:

CODE
INSERT INTO alias (address, goto) values ('everyone@marblemedia.com', 'everyone')
everyone-admin
INSERT INTO alias (address, goto) values ('everyone-admin@marblemedia.com', 'everyone-admin')
everyone-bounces
INSERT INTO alias (address, goto) values ('everyone-bounces@marblemedia.com', 'everyone-bounces')
everyone-confirm
INSERT INTO alias (address, goto) values ('everyone-confirm@marblemedia.com', 'everyone-confirm')
everyone-join
INSERT INTO alias (address, goto) values ('everyone-join@marblemedia.com', 'everyone-join')
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2017 Invision Power Services, Inc.