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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help! divide a file to two by grep result...

Status
Not open for further replies.

pooq

IS-IT--Management
Aug 15, 2002
7
US
i am just beginning to learn shell programming. but now got a task like this
which is out of my control. can somebody help me?

I have a huge log-file
and a user account file (text, 500 lines, seperate by space) like this
jsmith John Smith
gstone George Stone
....
....


need to read the first column(user account) from the account file one by one and
find out if it's shown in the log-file
I need to get 2 account files. one is the accounts who were never shown in log-file
the other one accounts who showed in log-file

how can i do it?
 
man split, grep, comm, diff -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
read some old posts and got this myself.

#!/bin/ksh
while read line
do
if [$(less log-file.gz | grep $line)]
then
write to file 1
else write to file 2
fi
done < accounts.file

I need to work out how to implement writing to file... sigh.. jamisar gave me no help. hehe :)
 
#!/bin/sh
while read line
do
if [$(less log-file.gz | grep $line)]
then
echo $line >> activeaccount.txt
else
echo $line >> inactiveaccounts.txt
fi
done < accounts.txt


got errors message
[pooq@testlinux]# ./splitaccount
./splitaccount: line 4: []: command not found
./splitaccount: line 4: [Dec: command not found
./splitaccount: line 4: []: command not found
./splitaccount: line 4: []: command not found

why???

pooq is working on it :) any comments?
 
i changed
if [$(less log-file.gz | grep $line)]

to

if $(less log-file.gz | grep $line)

still have error message like these. i don't know why.
[root@lanlinux1 cisco-log]# ./splitaccount
./splitaccount: line 4: Dec: command not found
./splitaccount: line 4: Dec: command not found
./splitaccount: line 4: Nov: command not found
but it seems the results(activeaccounts.txt inactiveaccounts.txt) are fine.

i am still waiting the script running.
(a huge logfile and 500 accounts...)
 
Hi,

Try
#!/bin/sh
while read line
do
NAME=`echo $line | awk '{print $1}'`
if [ `grep -c $NAME log-file.txt` -ne 0 ]
then
echo $line >> activeaccount.txt
else
echo $line >> inactiveaccounts.txt
fi
done < accounts.txt

but I think jamisar is able to give us one of his &quot;one-line-jamisar&quot; command ;-)
 
Just a note - not an answer - if statements need a space after '[' and before ']'
eg
if [ $(less log-file.gz | grep $line) ]
^ ^
Dickie Bird (:)-)))
 
yes, and as &quot;$(less log-file.gz | grep $line)&quot; is Korn Shell syntax, so include &quot;#!/bin/ksh&quot; at beginning of script
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top