LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-20-2007, 02:29 AM   #1
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Rep: Reputation: 45
Shell scripting: Matching and substituing between two lists


I want List B to search against List A. If List B's ip addresses match List A's ip addresses, then add/replace List A with the user's name.

For example:
(note that IP addresses purposely OUT OF ORDER)

List A:
192.168.0.1
192.168.0.3
192.168.0.2

List B:
192.168.0.1, joe@123.com
192.168.0.2, mary@456.com
192.168.0.3, paul@789.com


I would want List A to now look like:
192.168.0.1, joe@123.com
192.168.0.3, paul@789.com
192.168.0.2, mary@456.com

Is there a way for List B to partially compare against List A in a script, and if it has a match, replace it? The only thing I could think of would be using sed, but I don't know how to automate this. sed s/192.168.0.1/192.168.0.1, joe@123.com/g etc...

Assume I have this list with over 1,000 entries in them so doing this manually one-by-one is not an option.
 
Old 05-20-2007, 02:47 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk -F", " ' NR == FNR { arr[$1]=$2;next; } {  print $1","arr[$1] } ' file2 file1
output:
Code:
# ./test.sh
192.168.0.1,joe@123.com
192.168.0.3,paul@789.com
192.168.0.2,mary@456.com
 
Old 05-20-2007, 04:58 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
grep -f listA listB

This won't give an output if the IP address isn't in listB however.

Code:
grep -f listA listB
192.168.0.1, joe@123.com
192.168.0.2, mary@456.com
192.168.0.3, paul@789.com
 
  


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
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
bourne shell pattern matching or regular expression powah Programming 2 06-30-2006 10:27 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM

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

All times are GMT -5. The time now is 11:53 PM.

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