Linux Help
guides forums blogs
Home Desktops Distributions ISO Images Logos Newbies Reviews Software Support & Resources Linuxhelp Wiki

Welcome Guest ( Log In | Register )



Advanced DNS Management
New ZoneEdit. New Managment.

FREE DNS Is Back

Sign Up Now
 
Reply to this topicStart new topic
> Question regarding sed
marbleman
post Oct 23 2006, 10:04 AM
Post #1


Whats this Lie-nix Thing?
*

Group: Members
Posts: 2
Joined: 23-October 06
Member No.: 7,553



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
Go to the top of the page
 
+Quote Post
markjr
post Oct 23 2006, 07:12 PM
Post #2


./configure
***

Group: Admin
Posts: 62
Joined: 9-February 06
Member No.: 6,054



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
Go to the top of the page
 
+Quote Post
marbleman
post Oct 24 2006, 08:40 AM
Post #3


Whats this Lie-nix Thing?
*

Group: Members
Posts: 2
Joined: 23-October 06
Member No.: 7,553



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')
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 19th October 2017 - 05:02 PM