LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2007, 08:01 AM   #1
IceOner
LQ Newbie
 
Registered: Oct 2007
Location: Amsterdam
Distribution: Debian, ubuntu
Posts: 9

Rep: Reputation: 0
Question Question about sed


Hello,

I have a question. Since I've never worked with sed, I thought you all could perhaps help me out with the following.

I have a file that will be read through line by line.
This file will be containing email addresses.
Example:

user@system.domain.com user@system.domain.com/
user2@system.domain.com user2@system.domain.com/

I'm looking for a sed command that will change the following.
The second email address line should be changed as follows:

user@system.domain.com system.domain.com/user/
user2@system.domain.com system.domain.com/user2/

Anyone that can help me sed this?
Thanks in advance

Last edited by IceOner; 12-13-2007 at 09:03 AM. Reason: Slight format error
 
Old 12-13-2007, 08:46 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

You ask for a sed solution, but I think awk would be easier:

awk -F"[@ ]" '{ print $1"@"$2" "$2"/"$1"/" }' infile

It is possible in awk to set multiple field separators, the above command uses @ and a space a separator (-f"[@ ]").

Hope this helps.
 
Old 12-13-2007, 08:47 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Provided the input is exactly as you described it (one space after first part, second address has trailing / character):
Code:
sed 's|\(^.*\) \(.*\)@\(.*\)|\1 \3\2|'
 
Old 12-13-2007, 08:49 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

@matthewg42: second field seems to be irrelevant, take a look at the second line (user2 vs user 1).

Which was changed by the OP while typing this......... which could also make my solution irrelevant.

Last edited by druuna; 12-13-2007 at 08:52 AM. Reason: OP changed original question.
 
Old 12-13-2007, 09:01 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
doh, you're quite right. I mean:
Code:
sed 's|^\(.*\)@\(.*\) \(.*\)@\(.*\)|\1@\2 \4\1|'
 
Old 12-13-2007, 09:10 AM   #6
IceOner
LQ Newbie
 
Registered: Oct 2007
Location: Amsterdam
Distribution: Debian, ubuntu
Posts: 9

Original Poster
Rep: Reputation: 0
Hey everyone,

Thanks for the help so far.
The user1 was my fault.

I've tried the awk option, and this is what came out:

test@test.com test test.com test/test/

Not exactly as I had in mind.
The input file contains the mailaddress twice, seperated by a tab.
The second time it is trailed by a slash.

test@test.com test@test.com/
test@test.test.com test@test.test.com/

And should change into:

test@test.com test.com/test/
test@test.test.com test.test.com/test/

Hope you get what I'm trying to get done.

Thanks for helping
 
Old 12-13-2007, 09:36 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Another awk solution
Code:
gawk -F"\t" '{split($2, array, "@"); print $1, array[2] array[1] "/"}' input_file
 
Old 12-13-2007, 09:55 AM   #8
IceOner
LQ Newbie
 
Registered: Oct 2007
Location: Amsterdam
Distribution: Debian, ubuntu
Posts: 9

Original Poster
Rep: Reputation: 0
Wow.. That is exactly what I was looking for!
Thanks so much, and I even get the gawk command this time..
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Another SED question 3saul Linux - Software 2 01-28-2007 12:06 PM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed question provkitir Programming 4 05-03-2005 09:41 AM
little sed question freelinuxcpp Linux - Software 3 01-20-2004 07:36 AM
A question on 'sed' Barbarian Programming 4 04-11-2002 10:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration