LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-22-2012, 12:31 AM   #1
sam_nyc
Member
 
Registered: Aug 2009
Posts: 41

Rep: Reputation: 1
pls. help with awk in search and replace two files


Hi,

I have two files. First file passwd file has user name all the users ID, UID and etc..
second file, has the new UID and user ID only. My goal is to take the second file match it with the passwd file and update the UID. The result file will be called NEW_passwd.
I never used AWK before, after reading about it, this is what I came up with. Please help me.

Code:
Example: 
Passwd file 
abe:x:1033:111:[I] Har Abe:/home/abe:/bin/false
aberenb:x:3055:3055:Alan B:/home/aberenb:/bin/tcsh
abhatta:x:11153:111:Amit b:/home/abhatta:/bin/tcsh
acart:x:5073:111:[I] Adam C:/home/acart:/bin/false

new file- 
21613   abe
21932   aberenba
21665   acart
Code:
awk 'NR==FNR{A[$2]=$2;next} $3 in A{$3=A[$4]}1' FS=, /home/sch/UID_update/newfile.txt FS="\t" OFS= "\t" /home/sch/UID_update/passwd
 > /home/sch/UID_update/NEW_passwd
 
Old 11-22-2012, 02:42 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Have a look at this;
Code:
awk 'BEGIN { FS = "[:\t]" ; OFS = ":" ; while ( ( getline < "newfile.txt" ) > 0 ) _[$2] = $1 } { if ( _[$1] ) { $3 = _[$1] } ; print $0 } ' passwd > NEW_passwd
I'm assuming that the second entry in newfile.txt is a typo (aberenba -> aberenb).

Maybe these links will help:
Bash resources:
Sed/Awk resources:
 
2 members found this post helpful.
Old 11-22-2012, 10:03 AM   #3
sam_nyc
Member
 
Registered: Aug 2009
Posts: 41

Original Poster
Rep: Reputation: 1
Thank you so much DRUUNA. The above code was very helpful. I will check the links you send also.
 
Old 11-22-2012, 10:10 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
You're welcome.

Can you put up the [SOLVED] tag.
- above the first post -> Please Mark this thread as solved if you feel a solution has been provided.
- -or- -
- first post -> Thread Tools -> Mark this thread as solved
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Using an awk array to search and replace maudgalyan Programming 10 10-17-2012 01:39 PM
[SOLVED] Pass search results to awk, and use awk output to search other files bspears1 Linux - Newbie 8 07-21-2012 10:17 AM
[SOLVED] search and replace across multiple files ottermaton Linux - General 3 06-22-2011 11:46 AM
Awk- search & replace text viceroy Linux - Newbie 7 07-22-2007 11:18 AM
search/replace in many files allelopath Linux - General 1 08-02-2005 10:21 PM


All times are GMT -5. The time now is 08:30 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration