Linux Command Help

Post » Tue Sep 04, 2012 10:17 am

I'm attempting to do the homework for my Introduction to Unix class, where we have to use certain commands to manipulate data and files. However, I'm stuck on one that involves using the "cut" command. The question is:

(1 command : 3 pts) You should be in your home directory.
Create a file named earlybirds in your home directory with a listing of all people who logged in on January 3rd 2006.
  • Use the .log files and search for anyone with an entry for the date specified.
  • Look in the manual for how to use the http://linux.die.net/man/1/cut command in more detail to get just the name.
  • If the user logged in several times on that day, their name should only appear once in the file.
  • The file should be just a list of names–nothing but names.
  • Do this in steps until you get the whole thing.

The files are located in ~cs155/pub/hw2Files, and I've tried doing it every way I can think of. Most recently I tried:

cat ~cs155/pub/hw2Files/*.log | cut -c 1-6 | grep 01.03.06 ~cs155/pub/hw2Files/*.log> ~/earlybirds

The output of that isn't giving me the names of the .log files, which is what I need. I guess I don't understand how to manipulate the "cut" command to give me a file name instead of a field within the files.
User avatar
Robert Jackson
 
Posts: 3385
Joined: Tue Nov 20, 2007 12:39 am

Post » Tue Sep 04, 2012 7:10 am

I'd probably [g]awk the row that has the names... but I'm guessing that's not allowed? I've never used the cut command

If I'm reading this correctly, though, don't you want the lines inside the lof files, not names of the log files? or does each person have their own log file?
User avatar
Jynx Anthropic
 
Posts: 3352
Joined: Fri Sep 08, 2006 9:36 pm

Post » Tue Sep 04, 2012 7:31 pm

I'd probably [g]awk the row that has the names... but I'm guessing that's not allowed?
Unfortunately, we aren't allowed to use any commands that haven't been taught to use yet, so no I wouldn't be able to use that one.

Each person has their own log file, so each file is something like alex.log, jason.log, duncan.log, etc. Inside the log files are simply just dates in the MM.DD.YY format.
User avatar
Marcia Renton
 
Posts: 3563
Joined: Fri Jan 26, 2007 5:15 am

Post » Tue Sep 04, 2012 5:10 am

Unfortunately, we aren't allowed to use any commands that haven't been taught to use yet, so no I wouldn't be able to use that one.

It'd help if you told us which commands you're allowed to use.
User avatar
P PoLlo
 
Posts: 3408
Joined: Wed Oct 31, 2007 10:05 am

Post » Tue Sep 04, 2012 8:33 am

you want to cut after you grep, if you cut out the first 6 characters, which cut -c1-6 does then you won't be able to grep the date

scratch that original message, it's hard to understand what is going on with your script without examples from the log file
but your cut command is retrieving 6 characters from the log file, but you are grepping for 8 characters, which means your grep command will never find any suitable lines from the log file
User avatar
Eoh
 
Posts: 3378
Joined: Sun Mar 18, 2007 6:03 pm

Post » Tue Sep 04, 2012 4:27 pm

I'd do a for loop to grep all the files individually

I'd grep with the -q flag (will have a zero exit status if line exists)

if $? equals 0, then I'd send the file name to the file earlybirds.tmp

once the for loop is done I'd use the cut command to cut the .log extension off of the contents of earlybirds.tmp and pipe the output to earlybirds

I'd then delete earlybirds.tmp

so the question is, have you guys done for loops and if statements for bash yet? This is the "simplest" (from basic commands usage standpoint) way I can think of on how to do this.
User avatar
NeverStopThe
 
Posts: 3405
Joined: Tue Mar 27, 2007 11:25 pm

Post » Tue Sep 04, 2012 7:09 am

After a lot of guesswork and failed attempts I finally managed to figure it out:

grep 01.03.06 ~cs155/pub/hw2File/*.log | cut -d '/' -f9 | cut -d '.' -f1>~/earlybirds

Is it possible to combine both cut pipes into a single command? The first cut command takes the absolute file path and cut it down to the name of the files, while the second cut command removes the .log from the file names.
User avatar
Marion Geneste
 
Posts: 3566
Joined: Fri Mar 30, 2007 9:21 pm


Return to Othor Games