use loop to search


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting use loop to search
# 8  
Old 09-22-2004
Okay - try something like this:
Code:
#!/bin/ksh 
# file: dedup.sh  (be sure to chmod +x dedup.sh )
# $1 = file name - first parameter
# usage: dedup.sh myfile > newfile
old_rec="-1"
            # sort the file
sort $1 -o $1
            # process the sorted file
while read rec
do

# check first five chars of the record
        # first time thru the loop
    if [ "$old_rec" = "-1" ]; then
        old_rec="$rec"
        old_key=`expr substr "$rec" 1 5`
        continue
    fi
        # check if record keys match
    rec_key=`expr substr "$rec" 1 5`
    if [ "$old_key" = "$rec_key" ]; then
        # they match so keep reading file, use latest record
        old_rec="$rec"
        continue
    fi
# we have a new key, print last record
    echo "$old_rec"
    # set up for new record
    old_rec="$rec"
    old_key=`expr substr "$rec" 1 5`
# read from $1 the filename 
done < $1

exit

This works on a dummy file with 20 records. Print the last of the duplicated keys. key is the first five characters of the record
# 9  
Old 09-22-2004
loop to find out the last duplicate

Thanks Jim.
Hi, it's working!
I was trying to use 'sort -un 'command to pick the last duplicate.
But this command can only pick from the 3 occurence, it will not recoginize from the 4th occurence.


I ran this script and found out that it does not print the last record in the file. So I put a echo "$old_rec" command right outside the while statement. Now it's working.

But I do have a question about changing shell within script.
Can I do that?

My script was started with using '#/bin/sh'. I don't know what shell this is. I was trying to copy your script to mine. but it won't run well with this shell. so I add one line like this:
/usr/bin/ksh
to start your script. after processing, I return back to my shell by typing
/usr/bin/sh
It seems weird because I will be prompted with $ and I have to type exit and get out twice, but the result is good.
# 10  
Old 09-23-2004
>>/usr/bin/ksh
>>to start your script. after processing, I return back to my >>shell by typing
>>/usr/bin/sh


By doing above 2 things u are starting 2 more new shells over your current shell ; U had to exit 2 times to retain the current shell.


To know the shell u are working on ...

grep "userid" /etc/passwd

it gives the shell u are working on unless it is changed by .profile.



If shell gives prblem some times run the script like this .


./script

Hope this helps.
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