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
how to search a keyword within a file using a for loop lucho_1 Shell Programming and Scripting 6 03-10-2008 02:57 PM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 10:24 AM
Search for a pattern from the result of search boopathi_d Shell Programming and Scripting 3 12-05-2007 09:54 AM
Advanced Search Problems.. Search by User Name Neo Post Here to Contact Site Administrators and Moderators 1 05-19-2003 01:28 AM
how to get the similar function in while loop or for loop trynew Shell Programming and Scripting 3 06-17-2002 12:09 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 09-22-2004
whatisthis's Avatar
whatisthis whatisthis is offline
Registered User
  
 

Join Date: Aug 2004
Posts: 175
use loop to search

Hi,
I need to search through a file to find the last one occurence of my data.


File1 is like this:
sososo
abc123
ssssss
abc456
ssssss
abc345

I need to loop through to identify the last occurence of "abc".
So "abc345" is the record that I need.

Can someone give me some ideas?
  #2 (permalink)  
Old 09-22-2004
RTM's Avatar
RTM RTM is offline Forum Advisor  
Hog Hunter
  
 

Join Date: Apr 2002
Location: On my motorcycle
Posts: 3,039
It matters how your file is formated and where else abc might come up, but for the example you showed, this will work.

grep "abc" file1|tail -1
  #3 (permalink)  
Old 09-22-2004
whatisthis's Avatar
whatisthis whatisthis is offline
Registered User
  
 

Join Date: Aug 2004
Posts: 175
Sorry. I forgot to mention this will be a loop.
It means that I will not stop after I found 'abc'. I will need to save it to a file and continue to search another pattern like the last occurence of "ss" and append it to the same file.

Basically I need to compare the first 5 charactors of each record and only pick the last occurence of the matched(as long as it matches on the first 5 chars) record.I need to search through the whole file to identify any record that first 5 chars are duplicated and only pick the last record.
I hope this helps...

??
  #4 (permalink)  
Old 09-22-2004
encrypted's Avatar
encrypted encrypted is offline Forum Advisor  
Registered User
  
 

Join Date: Feb 2004
Location: Oslo, Norway
Posts: 219
Quote:
I will need to save it to a file and continue to search another pattern like the last occurence of "ss" and append it to the same file.
As suggested by RTM you can use grep.
Place your "search patterns" in a file say patternfile
and search for them in your datafile

/usr/xpg4/bin/grep -f patternfile datafile

As always you can redirect the output to a file of choice

Chill
enc.
  #5 (permalink)  
Old 09-22-2004
whatisthis's Avatar
whatisthis whatisthis is offline
Registered User
  
 

Join Date: Aug 2004
Posts: 175
The problem is that I don't know my exact 'search pattern'.

It can be any duplicated record in a file.

It's like I need to do some clean up in the house.
  #6 (permalink)  
Old 09-22-2004
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,787
You will get a decent answer if you specify the whole problem, not just the pieces that you think you need.

Can you explain how you are identifying duplicates? For example, it sounds like you need sort.

sort allows you to sort on a keyfield and eliminate duplicates in the keyfield. If we knew what was needed we could probably find an answer.
  #7 (permalink)  
Old 09-22-2004
whatisthis's Avatar
whatisthis whatisthis is offline
Registered User
  
 

Join Date: Aug 2004
Posts: 175
sort command

Sorry about my poor English.
I won't have totally identical records. Maybe two records will be like this:

12345apple
12345pear
12345orange

I need to only pick '12345orange' because it's the last one that showed up with duplicated '12345'. I only care first 5 charactors in each row.

The whole file will be like the above format. I only care the first 5 charactors in each row. If duplicates happens, I only need to pick the last occurence. If no duplicates, I need to pick that only one.

Sort command is a good hint. I am thinking how to utilize it...
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 11:56 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