The UNIX and Linux Forums  

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
search a pattern and if pattern found insert new pattern at the begining pitagi Shell Programming and Scripting 7 02-12-2009 10:27 PM
modify a particular pattern starting from second line of the search pattern imas UNIX for Dummies Questions & Answers 1 10-12-2008 02:19 PM
modify a particular pattern starting from second line of the search pattern imas UNIX for Dummies Questions & Answers 2 10-12-2008 12:30 PM
Check whether the pattern is present or not? skyineyes Shell Programming and Scripting 8 06-27-2007 10:42 AM
Search file for pattern and grab some lines before pattern frustrated1 Shell Programming and Scripting 2 12-22-2005 03:41 PM

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 08-20-2009
imas imas is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 15
search pattern present in second field

Hi All,

I have a file with following list.

example 1
========
cat 1.txt
--------

Code:
0000cab4752c
0000dab47c2c
...
...
...


Also i have another file 2.txt in which the data is in this format as shown:

cat 2.txt
---------

Code:
4591dc00fffffc0001002369774394|4591dde101002369774394|6.14.221.2|1250753484144|5|360|1250751684144|1250753034144|1110767654|false|1249217618553|1250749884144|1250755284144|1250755284144|false|0000cab47c2c|Scope6914522|01040004000602060000cab47c2c

...
...

Now i wrote script to take each pattern from 1.txt and grep in 2.txt and if the result matches then redirect to 3.txt


Code:
for i in `cat 1.txt`
do
  grep "$i" 2.txt >>3.txt
done

But my requirement is to grep for a given pattern which should be present in the second field of the 2.txt then only print it.

example 2
-----------
1.txt
------

Code:
0000cab47c2c


2.txt
-----

Code:
4591dc00fffffc0001002369774394|4591dde---0000cab47c2c----
|6.14.221.2|1250753484144|5|360|1250751684144|1250753034144|1110767654|false|1249217618553|1250749884144|1250755284144|1250755284144|false|0000cab47c2c|Scope6914522|01040004000602060000cab47c2c

If you notice i placed a hypen --- at the start and end of the pattern to be searched.

I want this kind of result but not the one which is shown in my first example.

Your help is much appreciated.

Thanks
imas

Last edited by Franklin52; 08-20-2009 at 05:35 AM.. Reason: Please use code tags!
  #2 (permalink)  
Old 08-20-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
something like this :


Code:
for i in `cat 1.txt`
do
awk -F"|" -v ser="$i" '$2 ~ ser { print }' 2.txt  >> 3.txt
done

  #3 (permalink)  
Old 08-20-2009
imas imas is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 15
No it gives me the following error

awk: syntax error near line 1
awk: bailing out near line 1
  #4 (permalink)  
Old 08-20-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
Use nawk or gawk instead.
  #5 (permalink)  
Old 08-20-2009
imas imas is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 15
Hi Panyam,

Thank you, Thank you, Thank you very much.
Yes, finally i got the expected output.

But, Please please please do let me know which books i refer from beginner, intermediate and advance level in scripting so that i can be more independent and help dumb ppl like me who are trying to convey that unix is more user friendly, fastest and reliable O/S.

Please do post your reply.

Iam eagerly waiting for your reply.

Thanks
-imas
  #6 (permalink)  
Old 08-20-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
imas,

Hope if you google you will get more and more links and resources.

I really don't have any idea on the best books , but for awk/sed these links

are useful.



Code:
www.grymoire.com/Unix/Awk.html 

www.grymoire.com/Unix/Sed.html

  #7 (permalink)  
Old 08-20-2009
imas imas is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 15
Hi Panyam,

Thanks for your time and suggestion for the awk/sed links.
Definitely i will try to improve my knowledge.

Once again thanks to you and all unix forum members.

Regards,
imas
Closed Thread

Bookmarks

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 07:09 AM.


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