1826187 Members
2572 Online
109691 Solutions
New Discussion

scripting

 
SOLVED
Go to solution
Ravinder Singh Gill
Regular Advisor

scripting

How can I combine

awk -F":" 'print $1'

with

typeset -l

and

cut -c1-6



When I try to run the following:

while read testuser
do
testname = $testuser | awk -F":" '{print $1}' | typeset -l | cut -c-6
echo $testname >> testoutput

done < testfile


It gives me the following error:

videv01:/home/root/rav/test# ./testscript.sh
./testscript.sh[4]: testname: not found.
./testscript.sh[4]: testname: not found.
videv01:/home/root/rav/test# ./testscript.sh[4]: testname: not found.
./testscript.sh[4]: testname: not found.
13 REPLIES 13
Patrick Wallek
Honored Contributor

Re: scripting

try:

typeset -l testname
testname=$(echo $testuser | awk -F":" '{print $1}' | cut -c1-6)
A. Clay Stephenson
Acclaimed Contributor

Re: scripting

typeset -l testname=$(echo "${testuser}" | awk -F":" '{print $1}' | cut -c-6)

Although you could do a substr() inside awk to get rid of the and do a tolower() as well
for better efficiency.

testname=$(echo "${testuser}" | awk -F ':' '{print tolower(substr($1,1,6))}')
If it ain't broke, I can fix that.
Sandman!
Honored Contributor
Solution

Re: scripting

Need to use command substitution like:

testname=$($testuser | awk -F":" '{print $1}' | typeset -l | cut -c-6)

cheers!
James R. Ferguson
Acclaimed Contributor

Re: scripting

Hi:

You already have the answer to your question, but, as an aside -- a useful thing sometimes -- you can lowercase or uppercase all or any field by updating it within 'awk':

# echo "UPPER MADE LOWER"|awk '{$0=tolower($0);print $0}'

...changes $0 (the current line) to lowercase. Then, all subsequent references, including those to individual fields, contain lowercase letters and can be referenced/matched as such.

Regards!

...JRF...
Arturo Galbiati
Esteemed Contributor

Re: scripting

Hi,
you can use only awk to obtain your result:

echo $testuser|awk -F ":" '{print tolower(substr($1,1,6))}'|read testname

HTH,
Art
Steve Steel
Honored Contributor

Re: scripting

Hi


Not for points but for your interest


www.shelldorado.com

Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Muthukumar_5
Honored Contributor

Re: scripting

Simply as,

awk -F: '{ print tolower(substr($1,1,6)); }' testfile >> testoutput

instead all your try.

Problem is with,
testname = $testuser | awk -F":" '{print $1}' | typeset -l | cut -c-6

Change this to,

testname = $(echo $testuser | awk -F":" '{print $1}' | typeset -l | cut -c-6)

hth.
Easy to suggest when don't know about the problem!
Ravinder Singh Gill
Regular Advisor

Re: scripting

Guys thanks for your help so far. I did the following:

rm testoutput
rm testokusers
rm testnotokusers

while read testuser
do
typeset -l testname
testname=$(echo $testuser | awk -F":" '{print $1}' | cut -c1-6)
echo $testname >> testoutput

done < passgvts

#the above reads passgvts file and extracts the first 6 characters of the first
field from each line thus creating a testname (in lowercase)

while read testname
do
grep $testname vosalist.sorted
if
[ $? = 0 ]
then
echo $testname >> testokusers
else
echo $testname >> testnotokusers
fi
done < testoutput

#if testname is found in the vosalist then the username is sent to testokusers e
lse it is sent to testnotokusers

while read fakeuser
fullfakename=$ {echo $fullfakename | awk -F":" '{print $1}'}
do
grep $fakeuser passgvts
if
[ $? = o ]
then
echo $fullfakename >> queryusers
fi
done < testnotokusers


The first part is working fine. What I am then trying to do is, for all the entries in testnotokusers I am trying to get the grep for it the passgvts file and send out the full entry in the first (not just first 6 characters) to a file called queryusers. This is the part that is not working. Can anybody advise?
Andy Torres
Trusted Contributor

Re: scripting

Please throw a point toward Steve Steel on my behalf. That's a new site to me. Thanks, Steve.
Ravinder Singh Gill
Regular Advisor

Re: scripting

Ok I changed the script as follows:

rm testoutput
rm testokusers
rm testnotokusers

while read testuser
do
typeset -l testname
testname=$(echo $testuser | awk -F":" '{print $1}' | cut -c1-6)
echo $testname >> testoutput

done < passgvts

#the above reads passgvts file and extracts the first 6 characters of the first
field from each line thus creating a testname (in lowercase)

while read testname
do
grep $testname vosalist.sorted
if
[ $? = 0 ]
then
echo $testname >> testokusers
else
echo $testname >> testnotokusers
fi
done < testoutput

#if testname is found in the vosalist then the username is sent to testokusers e
lse it is sent to testnotokusers

#fullfakename=$ {echo $fullfakename | awk -F":" '{print $1}'}

while read fakeuser
do
grep $fakeuser passgvts >> queryusers
done < testnotokusers


everything works fine except one thing. In a lot of the cases I have two similar usernames in passgvts file ie:

gillars
gillarst
jonesra
jonesrat

As I am matching the entries in testnotokusers, if for example in testnotokusers I have:

gillar
gillar
jonesr
jonesr

the out put in the file queryusers is double for most entries ie:

gillars
gillarst
gillars
gillarst
jonesra
jonesrat
jonesra
jonesrat

So I want to modify my script so that it only outputs the result of grep $fakeuser passgvts to queryusers if the entry does not already exist in there. How do I do this please???
Ravinder Singh Gill
Regular Advisor

Re: scripting

any advice?
Hein van den Heuvel
Honored Contributor

Re: scripting


Hmm, you make my head spin with all those files and tweaks. Seems to me this is not all that hard a problem when using a language like perl, or the shell, if you just remember values as you see them instead of sticking them in a file.
As others indicate... if you use a tool like awk, just lett it do as many steps as it can in one go (the field select, cut, case-change)

Anyway.. back to the last part, where you want to avoid ouput twice. Here is a practicular example which 'remembers' having seen a value by incrementing an array element in perl (could use awk or shell). Only print if it was zero before the increment.
This example also reads teh file to be grepped into a memory array, and greps that.

---- test.pl ----
open (PASS,"@pass=;
while (<>) {
chomp;
if (grep(/^$_$/,@pass)) {
print "$_\n" unless $seen{$_}++;
}
}

------ usage example ----

perl test.pl < testnotokusers > queryusers


hth,
Hein.



Michael Schulte zur Sur
Honored Contributor

Re: scripting

Hi,

just a thought,

Michael

while read testuser
do
typeset -l testname
testname=$(echo $testuser | awk -F":" '{print $1}' | cut -c1-6)
echo $testname
done < passgvts | sort -u > testoutput