Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux HostsFree MagazinesJobs
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page Help on shell script
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 12-24-2007   #1 (permalink)
Linux Enthusiast
 
Lakshmipathi's Avatar
 
Join Date: Sep 2006
Location: 3rd rock from sun
Posts: 544
Question Help on shell script

Hi all,
I got two files say,
file1.txt and
file2.txt

file1.txt content (two fields)
Code:
page=node_1_0_1  category
page=node_1_0_2  brand
file2.txt content (only one field)
Code:
page=node_1_0_1
page=node_1_0_2
Now i want to replace file2.txt with the second field of file1.txt

I need output like,
Code:
page=category
page=brand
How to do this??Any ideas?
Thanks
__________________
- LakshmiPathi.G
------------------------------
www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
------------------------------
Lakshmipathi is offline   Reply With Quote
Old 12-24-2007   #2 (permalink)
Linux Enthusiast
 
Franklin52's Avatar
 
Join Date: Aug 2006
Posts: 596
Something like this:

Code:
awk 'FNR==NR{arr[$1]=$1;next} arr[$1]==$1{print}' "file2" "file1"
Regards
Franklin52 is offline   Reply With Quote
Old 12-24-2007   #3 (permalink)
Linux Enthusiast
 
Lakshmipathi's Avatar
 
Join Date: Sep 2006
Location: 3rd rock from sun
Posts: 544
Exclamation

Hi thanks for ur reply,but i'm not getting expected results.

Code:
[oss@pc021698 JAVA]$ awk 'FNR==NR{arr[$1]=$1;next} arr[$1]==$1{print}' "file2.txt" "file1.txt"
page=node_1_0_1 category
page=node_1_0_2 brand

[oss@pc021698 JAVA]$ cat file1.txt
page=node_1_0_1 category
page=node_1_0_2 brand
[oss@pc021698 JAVA]$ cat file2.txt
page=node_1_0_1
page=node_1_0_2
__________________
- LakshmiPathi.G
------------------------------
www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
------------------------------
Lakshmipathi is offline   Reply With Quote
Old 12-24-2007   #4 (permalink)
Linux Enthusiast
 
Franklin52's Avatar
 
Join Date: Aug 2006
Posts: 596
I think I misread something, to print only lines of file2 if the line exists in file1 try this:

Code:
awk '
FNR==NR{arr[$1]=$1;next} 
arr[$1]==$1 && $2{split($1,s"=");{print s[1],$2}
' file2 file1
Regards
Franklin52 is offline   Reply With Quote
Old 12-24-2007   #5 (permalink)
Linux Enthusiast
 
Lakshmipathi's Avatar
 
Join Date: Sep 2006
Location: 3rd rock from sun
Posts: 544
Question

sorry again ,
it shows errors,unexpected new line or end of string
Code:
awk: cmd. line:4: arr[$1]==$1 && $2{split($1,s"=");{print s[1],$2}
awk: cmd. line:4:                                                 ^ unexpected newline or end of string
__________________
- LakshmiPathi.G
------------------------------
www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
------------------------------
Lakshmipathi is offline   Reply With Quote
Old 12-24-2007   #6 (permalink)
Linux Enthusiast
 
Franklin52's Avatar
 
Join Date: Aug 2006
Posts: 596
I apologise, a typo, remove the { before the print statement:

Code:
awk '
FNR==NR{arr[$1]=$1;next} 
arr[$1]==$1 && $2{split($1,s,"=");print s[1],$2}
' file2 file1
Regards

Last edited by Franklin52; 12-24-2007 at 12:10 PM. Reason: typo
Franklin52 is offline   Reply With Quote
Old 12-24-2007   #7 (permalink)
Linux Enthusiast
 
Lakshmipathi's Avatar
 
Join Date: Sep 2006
Location: 3rd rock from sun
Posts: 544
Sorry again ...still not solved....some error in split cmd
Code:
awk: cmd. line:3: (FILENAME=file1 FNR=1) fatal: split: second argument is not an array
__________________
- LakshmiPathi.G
------------------------------
www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
------------------------------
Lakshmipathi is offline   Reply With Quote
Old 12-24-2007   #8 (permalink)
Linux Enthusiast
 
Franklin52's Avatar
 
Join Date: Aug 2006
Posts: 596
Sorry again, I'm just awaked, it must be:

Code:
split($1,s,"=")
Regards
Franklin52 is offline   Reply With Quote
Old 12-24-2007   #9 (permalink)
Linux Enthusiast
 
Lakshmipathi's Avatar
 
Join Date: Sep 2006
Location: 3rd rock from sun
Posts: 544
Smile

it works now
Thanks Franklin .. Thanks for your patience
__________________
- LakshmiPathi.G
------------------------------
www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
------------------------------
Lakshmipathi is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
 

Similar Threads
Thread Thread Starter Forum Replies Last Post
cronjob - shell script related problem whrkit Misc 1 10-26-2007 09:01 PM
bash script file percolate cd up to calling shell DRLDR Linux Programming & Scripting 6 09-16-2007 10:27 PM
How to run parent shell script in -x ~debug mode and all child scripts that it calls Sangal-Arun Linux Programming & Scripting 2 08-04-2007 11:26 AM
mail program on shell script didn't work, please advise. duke0001 Linux Programming & Scripting 1 09-22-2006 01:54 AM
Directory listing - Calling shell script from a CGI script seranmca Linux Programming & Scripting 1 08-10-2005 02:16 PM

Free Magazines
Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe
Systems Management News, the newspaper for IT systems administration and data center managers!
Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe
The Enterprise Newsweekly
eWeek is the essential technology information source for builders of e-business.
subscribe
Oracle Magazine
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe
Total Telecom
Total Telecom is "The Economist of the communications industry".
subscribe
More free magazines »



All times are GMT. The time now is 07:25 PM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.2.0