Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count using two txt files - Unix - KSH

Status
Not open for further replies.

HHG

Technical User
Nov 8, 2003
68
GB
Hi,

I have 2 files:-

File A contains a list of numbers but the 1st 7chrs is a unique.

File B contains duplicate sequence number (1st 7chrs)

Any ideas on how I can count how many duplicates in file B.

I suspect I will have to use subscript or substring it but not sure what to use awk/sed or both or maybe somethingelse.

I am using ksh on unix sun solaris v10

if count > 5 (This would be 5 files starting with the same
then
transfer files
else
don't want these files
fi




 
Hi

To find out how many times the first 7 characters are duplicated in file B :
Code:
uniq -w 7 -c /file/B
Tested with GNU [tt]uniq[/tt].

You can also do it with [tt]awk[/tt] but it is a bit longer. If you are stronger in [tt]sed[/tt] than Aurélio Marinho Jargas, you can try it in [tt]sed[/tt] too...

Feherke.
 
thanks but I will need to know what the 7 characters are so that I can use it e.g. mv 1234567*.txt destination.
I will have a go will let you know on Monday. thanks again.
 
Code:
COUNT=0
while read line
do
  short=$(expr "$line" : "\(.......\)*")
  egrep -q ^$short fileA && (( COUNT += 1 ))
done < fileB
if [ $COUNT -gt 5 ]
then 
  do it
else
  dont do it
fi
But there are may other ways

Ceci n'est pas une signature
Columb Healy
 
Hmm...
Looks like I misunderstod the question

Ceci n'est pas une signature
Columb Healy
 
Hi

HHG said:
thanks but I will need to know what the 7 characters are so that I can use it e.g. mv 1234567*.txt destination.
And what ? Was not there all the information you needed ?
Bash:
uniq -w 7 -c /file/B | \
while read nr name; do
  test "$nr" -ge 5 && mv ${name:0:7}*.txt destination/;
done

Feherke.
 
-w is NOT a standard (X/Open or POSIX) option for the uniq command ...
 
Hi

Yep, I had such a feeling. That is why I specified "Tested with GNU [tt]uniq[/tt].".

I bothered to share this solution because HHG still not provided details about his system although was useful in the previous thread too... But I am quite sure it was in vain.

Feherke.
 
Hi thanks for the tips guys.
I'm back on the case now. I have done the following

cat xfer_list2.txt | awk '{print substr($1,1,7)}' | uniq -c

this gives me the following output two fields :-

Fld1 Field2
6 0000016
6 0000017
6 0000018
5 0000019
6 0000020
5 0000021
6 0000022
6 0000024
1 EOF2813
2 xfer_li

Any ideas how I might do test feild1 to be greater than or equal to 5 and do an action on the second field. e.g. on the first line of my example above field1 = 6, field2=0000016

I want to be able to do mv field2* to destination
but where its less then 5 I want to move those to another destination.

any help you can give me would be most appreciated, I really need your help to finish this piece of work as I am not very strong on unix scripting. By the way I am a her not a him, just so you know. Thanks

 
Hi

You can combine that with my earlier code :
Code:
[red]awk '{print substr($1,1,7)}' xfer_list2.txt | uniq -c[/red] | \
while read nr name; do
  test "$nr" -ge 5 && mv $name*.txt destination/
done
By the way, avoid UUOC.

Feherke.
 
Actually, you feherke you already give me the answer in the while read nr name statement, which I have done and works fine.
Thanks for your help, you saved the day.
 
Just one other question, as there is not an else statement. How do you think I can move the ones that are left over ie the ones less then 4. Hmmm, maybe I do the same statement again but do a < 5 though I am not sure of the syntax for less than.
 
Ok I found it in my book. I used the same statement with but instead of -ge I have used -le

Thanks all again for your help.
 
I'd use -lt instead of -le (unless you want some error for the files with a count=5 ...)
 
Hi

If you need an else branch then better change to a proper [tt]if[/tt] statement. The [tt]&&[/tt] are nice by alone but tend to become cryptic if there are many. And for the two [tt]test[/tt] commands two expressions will need to be evaluated, while the [tt]if[/tt] would have only one.
Code:
awk '{print substr($1,1,7)}' xfer_list2.txt | uniq -c | \
while read nr name; do
  [red]if [[/red] "$nr" -ge 5 [red]]; then[/red]
    mv $name*.txt destination/
  [red]else[/red]
    [gray]# echo "too few $name* files. all $n skipped." >> log_file[/gray]
  [red]fi[/red]
done

Feherke.
 
Hi PHV

I have done the following to take into account = 5

cat xfer_list2.txt | awk '{print substr($1,1,7)}' | uniq -c |\
while read nr name
do
test "$nr" -le 4 && mv ${name}* /l/test/var/pmcli/ifc/cru/export/prexfr
done

Just about to test the script.
 
Ahhh I like that one Feherke. I might just give that a go, but might have to tweak it alittle so I can move those files into another area but can use the error trapping you have given me. Thanks your brill!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top