Hi all, I literally only started looking at shell a couple of hours ago.

I am looking to do just one task, which I think should be quite straight forward.

I will try explain it as best I can.........

Basically I am trying to automate the grep command from ubuntu (grep xxx yyy.csv < xxx.csv)

I have a perl script that is going through a large amount of data finding the most common users in a csv file (yyy.csv) I then want to create a list of the top 50 or so of these users and grep their specific information from the file. So it would be like so:
grep user1 yyy.csv < user1.csv

The easiest thing is to probably call the created csv file by the name of the original user (which by the way, if it makes any difference, will be a 32 key hex number)

I was looking at a few sample scripts and I assume the easiest way is to use a

(for i in *) loop
but not too sure exactly the best way to go about this. Should I save the users I want to grep into a csv file from my perl code or does anyone have any suggestions as to what way to go about this problem?

So to summarise i'm looking to generate a script that will automate the following ubuntu commands in one go:

grep user1 yyy.csv < user1.csv
grep user2 yyy.csv < user2.csv
grep user3 yyy.csv < user3.csv
grep user4 yyy.csv < user4.csv
grep user5 yyy.csv < user5.csv
grep user6 yyy.csv < user6.csv
grep user7 yyy.csv < user7.csv
grep user8 yyy.csv < user8.csv
grep user9 yyy.csv < user9.csv

and so on..........

Hope this all makes sense!
Thanks very much,

N

Recommended Answers

All 7 Replies

niall_heavey> Hope this all makes sense!
Some but not all, since it is missing information

I'll work with what you gave me. I'll explain.
You mentioned the first 50 users.

You must have, then, a way to identify those users. Assuming a file list with each user name in each line, we will call that file userlist
Now we can start working

#! /bin/bash

# read each name and pass it to grep to create a different identity file log
while read name
do
    # any line in yyy.csv that contains $name is gathered and sent to $name.csv
    grep $name yyy.csv > $name.csv
done < userlist

or

#! /bin/bash

for name in $(cat userlist)
do
    grep $name yyy.csv > $name.csv
done

Thanks for your reply.
Looking at the code it looks perfect for what i'm trying to do. I will have a file with the user names in it. Would this be preferable as a csv file or should I use something else like txt or other? Also, if there was two columns in this would it cause trouble?

Also, which would be best? The bash or shell? Or is there much of a difference? And finally, in both codes you use "name" do I use it as you have it or is it to signify something?

I will not be at my ubuntu machine which has all the files etc. until morning so I will try it then. Thanks for the help though.

N

niall_heavey> Would this be preferable as a csv file or should I use something else like txt or other?
An ASCII text file is the simplest

niall_heavey> Also, if there was two columns in this would it cause trouble?
Nope, grep looks for the term and if finds it, will send the whole line to the output of choice, in this case a file with the username

niall_heavey> Also, which would be best? The bash or shell? Or is there much of a difference?
Bash is the default shell for Ubuntu. There are many shells, Bash it one of the most popular.

niall_heavey> And finally, in both codes you use "name" do I use it as you have it or is it to signify something?
name is just a variable identifier, it is not a built-in keyword. You can use any identifier than makes sense to you, as long as it starts with an alphanumeric or underscore character
Don't forget the $ symbol in front when dereferencing it.

Ok, thanks very much for all your help.

Just used it now and it works perfect!

Thanks again!
N

#! /bin/bash
# read each name and pass it to grep to create a different identity file log
while read name
do
# any line in yyy.csv that contains $name is gathered and sent to $name.csv
grep $name yyy.csv > $name.csv
done < userlist

Just another quick question on this. If I want to read from the file yyy.csv if it is in another directory what syntax should I use?
If it was in terminal I would only need to go down one level (i.e. use cd ..) to get to the directory I want to use.
Any ideas?

Thanks,

N

#! /bin/bash
# read each name and pass it to grep to create a different identity file log
while read name
do
# any line in yyy.csv that contains $name is gathered and sent to $name.csv
grep $name yyy.csv > $name.csv
done < userlist

Just another quick question on this. If I want to read from the file yyy.csv if it is in another directory what syntax should I use?
If it was in terminal I would only need to go down one level (i.e. use cd ..) to get to the directory I want to use.
Any ideas?

Thanks,

N

Same thing. You can use an absolute path e.i. /home/niall_heavey/logfiles/yyy.csv or you can pass it a relative path e.i ../yyy.csv

Great, thanks very much!

Works a treat!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.