Help Needed with Grep


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help Needed with Grep
# 1  
Old 10-06-2010
Help Needed with Grep

Hi all,

I need some urgent help with grep.
I'm simply trying to extract the current date from the syslog file, which is "Oct 6" and then grepping this and output the messages to a new log file.
See the below commands and output, even though the log file is created, it is created with all the messages instead of the filtered date "Oct 6", which is cannot extract.
Code:
# todays_date=`tail -1 /var/adm/messages|awk '{print $1, $2}'`
# grep $todays_date /var/adm/messages > syslog.`date +%d%m%y`.log
grep: can't open 6

Thanks and regards,
Wynford

Last edited by Franklin52; 10-06-2010 at 06:35 AM.. Reason: Please use code tags, thank you!
# 2  
Old 10-06-2010
Quote the variable:
Code:
grep "$todays_date" /var/adm/messages > syslog.`date +%d%m%y`.log

# 3  
Old 10-06-2010
Hi Franklin5,

Quote:
Originally Posted by Franklin52
Quote the variable:
Code:
grep "$todays_date" /var/adm/messages > syslog.`date +%d%m%y`.log



---------- Post updated at 10:59 AM ---------- Previous update was at 10:57 AM ----------

Hi Franklin52,

Using your command;

grep "$todays_date" /var/adm/messages > syslog.`date +%d%m%y`.log

the file is created by it's empty.

---------- Post updated at 11:00 AM ---------- Previous update was at 10:59 AM ----------

Sorry, but the file created is a zero byte file, any ideas?
# 4  
Old 10-06-2010
Can you post the output of:
Code:
tail -1 /var/adm/messages

Please use code tags.
# 5  
Old 10-06-2010
Here you go;

Code:
$ tail -1 /var/adm/messages
Oct  6 11:12:06 solsw137-b02b.nls.jlrint.com 1286957: Oct  6 10:12:05: %RTD-1-LINK_FLAP: FastEthernet0/17 link down/up 22 times per min


Moderator's Comments:
Mod Comment Please use code tags!

Last edited by Franklin52; 10-06-2010 at 07:48 AM..
# 6  
Old 10-06-2010
Can you put code tags round the sample. I bet that there are two spaces between
Code:
"Oct  6".

If this is the case you can generate a matching date like this.

Code:
DDMMM="`date '+%b %e'`"            # Month day-with-space

This User Gave Thanks to methyl For This Post:
# 7  
Old 10-06-2010
As methyl mentioned, there are 2 spaces between the month and the day, hence my request to use code tags.

Try this:
Code:
todays_date=`tail -1 /var/adm/messages|awk '{print $1 FS FS $2}'`
grep "$todays_date" /var/adm/messages > syslog.`date +%d%m%y`.log

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep command help needed

Hi All, I am looking for grep command to search the following in files of .cpp/.h of a directory and its sub-directories. 1. (char *) 2. (int *) 3. (float *) Like above pattern i need to search in all the files and list. I am using for example... for char*/const char* $egrep -Rwin... (2 Replies)
Discussion started by: vijaya_svk
2 Replies

2. Shell Programming and Scripting

Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count. `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l` basically it is assigned to a variable ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh |... (6 Replies)
Discussion started by: wtolentino
6 Replies

3. UNIX for Advanced & Expert Users

Grep - Explanation needed.

grep -E '^(++){5}5000' <file_name> this command searches value 5000 in only 6th column from provided file where pipe ( | )is delimiter which separate columns... can some one plz explain me what '^(++){5}5000' actually does..? :confused: (1 Reply)
Discussion started by: Killer420
1 Replies

4. Shell Programming and Scripting

help needed with using grep in shell programming

Hi, im working on an assignment (airline ticketing system). im kinda having problems with the search function. i want users to be able to search by departure time or by flight. however, my output displays the whole chunk of data instead of what they are supposedly searching for. appreciate if... (1 Reply)
Discussion started by: crazybean
1 Replies

5. UNIX for Dummies Questions & Answers

Grep output help needed

According to linux hardening guide a PATH variable must not contain . or .. so i grep the path variable using. # echo $PATH | grep "." /usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin # echo $PATH | grep ".."... (2 Replies)
Discussion started by: pinga123
2 Replies

6. Shell Programming and Scripting

Help Needed with grep & sed

On one of my servers, it appears that a bunch of html files got the following code added to it... I was going to try to remove this line using grep & sed... as sample grep -lr -e 'apples' *.html | xargs sed -i 's/apples/oranges/g' I can get the grep portion to work... grep "<script... (7 Replies)
Discussion started by: djlane
7 Replies

7. Shell Programming and Scripting

help needed in grep and copy

need help in a command. My requirement is that i m grepping the filename based on a pattern in .tar.gz file and then copying the file into some dir below is what exactly i want to achieve step 1: grep -l '30017A6800022D1A' CurrentCollectorMeterReadBackup20081007.tar.gz step 2: file found... (2 Replies)
Discussion started by: ali560045
2 Replies

8. UNIX for Dummies Questions & Answers

grep problem with the ^ symbol, help needed please.

Hello I am new to this forum and have been perplexed by this issue for the last hour of my life, I've tried to search everywhere for a solution to my syntax error, but here is my code. grep -wvnf 'noise.dat' sample.out | sort output: 4:Java Swing 5:Swing Java 6:Software Requirements... (2 Replies)
Discussion started by: Gvsumrb
2 Replies

9. UNIX for Advanced & Expert Users

Help Needed in Grep Command

Hi i am using grep command to search a string in a file and i am able to find that string in it .. but moving forward once i find the string i need to remove the 5 lines above that and to remove 2 lines below it .. Just wanted to be clear there should be no lines existing which has a string... (7 Replies)
Discussion started by: ranga27
7 Replies

10. UNIX for Dummies Questions & Answers

Help with tail /grep needed

Hello: I'm a very newbee at UNIX/AIX. What i want to do is to tail a file from the bottom until a certain string is found and write all the lines after the found string to another file. I've tried out a lot of combination with tail and grep but doesn't find the good one. Could someone help... (4 Replies)
Discussion started by: Felix2511
4 Replies
Login or Register to Ask a Question