use loop to search


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting use loop to search
# 1  
Old 09-22-2004
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  
Old 09-22-2004
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  
Old 09-22-2004
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  
Old 09-22-2004
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  
Old 09-22-2004
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  
Old 09-22-2004
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  
Old 09-22-2004
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...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Loop through the folders and search for particular string in files

Hello, Opearting System Environment : HP Unix B.11.31 U I look for script to On specific folders list On specific filelist Search for given string For Example : r48_buildlib.txt contains wpr480.0_20161027 wpr480.0_20161114 wpr481.0_20161208 wpr482.0_20161222... (4 Replies)
Discussion started by: Siva SQL
4 Replies

2. UNIX for Dummies Questions & Answers

Loop with Perl (string search)

I am using a perl script to reverse and complement sequences if a string is found. The script works as expected as standalone but I would like to use it in my bash file. However, I am not getting my expected result. My test.txt file >Sample_72... (8 Replies)
Discussion started by: Xterra
8 Replies

3. Shell Programming and Scripting

Bash loop to search file

In the bash when the user inputs an id to search for the bash currently closes, and if a match is found outputs a new file (match.txt). Is it possible to have not close the bash but rather, on the screen "searching for match" and if a match is found "match found in line.." is displayed... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

Wildcard search in if loop

Practice folder contains many files and im interested in extracting file which starts with abc* ghi* xyz* . I need to do variety of operations for different files. if file starts with xyz* then i need to move to some destination otherwise some other destination. I am not able to make wildcard... (15 Replies)
Discussion started by: kumaar1986
15 Replies

5. Shell Programming and Scripting

Grep with loop till search is done

I need help to put a script where it runs the svn command grep'ing for the ticket# in the comments to see if the ticket was used in the latest commit. so on command line: ./test.sh ticket-1 ticket-2 ticket-3 It should be able to check if ticket-1 is used first and if not then check if... (2 Replies)
Discussion started by: iaav
2 Replies

6. Shell Programming and Scripting

Speeding up search and replace in a for loop

Hello, I am using sed in a for loop to replace text in a 100MB file. I have about 55,000 entries to convert in a csv file with two entries per line. The following script works to search file.txt for the first field from conversion.csv and then replace it with the second field. While it works fine,... (15 Replies)
Discussion started by: pbluescript
15 Replies

7. Shell Programming and Scripting

search replace with loop and variable

Hi, could anyone help me with this, tried several times but still not getting it right or having enough grounding to do it outside of javascript: Using awk or sed or bash: need to go through a text file using a for next loop, replacing substrings in the file that consist of a potentially multi... (3 Replies)
Discussion started by: wind
3 Replies

8. UNIX for Dummies Questions & Answers

Loop through Sub Directories and search for set of files

I have the below directory in unix environment /home/bkup/daily: ls -lrt drwxrwx--x 2 user user 256 Jan 12 18:21 20110112/ drwxrwx--x 2 user user 256 Jan 13 17:06 20110113/ drwxrwx--x 2 user user 256 Jan 14 16:44 20110114/ drwxrwx--x 2 user user ... (2 Replies)
Discussion started by: prasannarajesh
2 Replies

9. Shell Programming and Scripting

Search two file types within a for loop

I need to search for a csv and a dat file within a for loop then output them to a log table. I can do it individually but I want them together. See below code: check csv count=0 for file in $(ls *.csv) ; do count=`expr $count + 1` ... (4 Replies)
Discussion started by: Pablo_beezo
4 Replies

10. Shell Programming and Scripting

how to search a keyword within a file using a for loop

hi guys i have a problem here, im trying to stablish a relationship between a text file and an input user for example the script is going to prompt the user for some football team and what the script is going to do is return the colums in which that input is located so far this is what i have ... (6 Replies)
Discussion started by: lucho_1
6 Replies
Login or Register to Ask a Question