The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
displaying $ in data ammu UNIX for Dummies Questions & Answers 2 02-19-2008 03:00 PM
searching and displaying most commonly used words arunsubbhian UNIX for Dummies Questions & Answers 2 09-10-2007 07:58 PM
displaying with ls vivekshankar UNIX for Dummies Questions & Answers 2 05-23-2005 03:46 PM
vi editor not displaying? ananthu_m SCO 3 10-14-2003 12:51 AM
Displaying what a command is doing/has done quantumechanix UNIX for Dummies Questions & Answers 1 07-02-2003 08:40 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-06-2009
ajay41aj ajay41aj is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 15
searching and displaying help

I have two input files like this:

File-1
-----

a1234 abc town
f2345 def village
t5678 pqr county

File-2
------

123456 test1 test2 test3 id-a1234
789012 test2 test4 id-t5678
456789 test7 id-b1234

I want to check the lines that match the first field of File-1 in File-2 (where it appears after the "id") and create two output files as follows (based on whether the item is found in File-2 or not):

Output-1 (when the first field of File-1 is found at File-2)
-------------------------------------------------------

a1234 123456
t5678 789012

Output-2 (when the first field of File-1 is not found in File-2)
----------------------------------------------------------

f2345 Not Found

How to do this in a shell script?

Any help will be really appreciated.

Thanks,

Ajay
  #2 (permalink)  
Old 02-07-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,301
Something like this?

Code:
awk -F " |-" 'NR==FNR{a[$NF]=$1;next}
a[$1]{print $1,a[$1];next}
{print $1 " not found"
}' File-2 File-1
Regards
  #3 (permalink)  
Old 02-09-2009
ajay41aj ajay41aj is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 15
:(

hmm, that didn't work
  #4 (permalink)  
Old 02-09-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,301
Quote:
Originally Posted by ajay41aj View Post
hmm, that didn't work
Be more precisely, what didn't work? No output, wrong output, warnings, errors?

Regards
  #5 (permalink)  
Old 02-09-2009
ajay41aj ajay41aj is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 15
I get Output-2 but not Output-1.
  #6 (permalink)  
Old 02-10-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,301
To print the output to the files you can redirect the outputs like this:

Code:
 awk -F " |-" 'NR==FNR{a[$NF]=$1;next}
a[$1]{print $1,a[$1];next}
{print $1 " not found" > "Output-2"
}' File-2 File-1 > Output-1
This is what I get:

Code:
$ cat File-1
a1234 abc town
f2345 def village
t5678 pqr county
$
$ cat File-2
123456 test1 test2 test3 id-a1234
789012 test2 test4 id-t5678
456789 test7 id-b1234
$
$ awk -F " |-" 'NR==FNR{a[$NF]=$1;next}
a[$1]{print $1,a[$1];next}
{print $1 " not found" > "Output-2"
}' File-2 File-1 > Output-1
$
$ cat Output-1
a1234 123456
t5678 789012
$
$ cat Output-2
f2345 not found
$
Regards
  #7 (permalink)  
Old 02-17-2009
ajay41aj ajay41aj is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 15
Thanks for the script.

But, I have issues when using it when the input file-2 has "test-2" instead of "test-2" or "test-3" instead of "test3" (the use of "-" in other columns.)

So, I am looking for the script to look only for the field that has the "id-" but must be able to ignore if the "-" presents in any other column.

Any suggestions?
Closed Thread

Bookmarks

Tags
search, string manipulation

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:31 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0