Grep Search for both First and last word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep Search for both First and last word
# 1  
Old 05-15-2011
Grep Search for both First and last word

Hi
Pretty sure this is very simple hence im banging my head o the wall as to why i cant get this to work:

Im greping against many files where there will be the following string:

Date: Thu, Aug 23 2001 09:27 PM

(of course values such as date ,time and PM/AM will vary.)

Im trying to grep where I match the first word being Date:
and the last word will be either PM or AM.

I tried
Code:
grep '^Date:.$PM' *

but I get nothing back.
# 2  
Old 05-15-2011
Code:
egrep "^Date:.*[AP]M$" file(s)

will find all records containing Date: as the first token, and either AM or PM as the last token.

In your example, the single dot following "Date:" would match just one character. To match any number of characters you need .* You also need to move the dollar sign to the end of the string. Specifying $PM will cause the shell to substitute the value of the variable PM whch might not be set and thus evaluate to nil in your pattern.

If there are tokens after the PM/AM in the file, then this won't work; drop the dollar sign and you should be OK.

Last edited by agama; 05-15-2011 at 05:10 PM.. Reason: typo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search for a word in column header that fully matches the word not partially in awk?

I have a multicolumn text file with header in the first row like this The headers are stored in an array called . which contains I want to search for each elements of this array from that multicolumn text file. And I am using this awk approach for ii in ${hdr} do gawk -vcol="$ii" -F... (1 Reply)
Discussion started by: Atta
1 Replies

2. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

3. Linux

How to search multiple word using grep command?

How to search multiple word using grep command for example i want to reserch ANJ001 AA Using ridiculous font, size, and color changes instead of normal space separated text and CODE tags obfuscates what you are trying to do and makes it difficult for volunteers who may want to help you solve... (1 Reply)
Discussion started by: na.dharma
1 Replies

4. Shell Programming and Scripting

[Solved] Search for a word and print the next word

Hi, I am trying to search for a word and print the next word. For example: My text is "<TRANSFORMATION TYPE ="Lookup Procedure">" I am searching for "TYPE" and trying to print ="Lookup Procedure" I have written a code like following: echo $line | nawk... (4 Replies)
Discussion started by: sampoorna
4 Replies

5. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word "description" excluding weird characters like $&lmp and without html tags in the new file output.txt. Help me. Thanx in advance. I have attached the input... (4 Replies)
Discussion started by: sachit adhikari
4 Replies

6. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script?

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word description excluding weird characters like $$#$#@$#@***$# and without html tags in the new file output.txt. Help me. Thanx in advance. My final goal is to... (11 Replies)
Discussion started by: sachit adhikari
11 Replies

7. UNIX for Dummies Questions & Answers

Script to search for a particular word in files and print the word and path name

Hi, i am new to unix shell scripting and i need a script which would search for a particular word in all the files present in a directory. The output should have the word and file path name. For example: "word" "path name". Thanks for the reply in adv,:) (3 Replies)
Discussion started by: virtual_45
3 Replies

8. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

9. Shell Programming and Scripting

Grep word search

Hi, I have grep command to search for a word "SUB" as below, grep -w "SUB" file1 But this is taking a word like SUB.XXY or SUB.BBB etc which is not required.. I need only if it is a word "SUB". Any suggestions ??? Thanks, Vasanth. (11 Replies)
Discussion started by: skcvasanth
11 Replies

10. Shell Programming and Scripting

grep -w Search for the expression as a word

I have a file (file1) with around 5000 records and another file (file2) with 50000 records. I want to search each word in file 1 from file 2 and spew the output of the matches in file3. Can someone please help me here. I tried doing this in ksh for i in `cat file1` do grep -w $i... (15 Replies)
Discussion started by: knijjar
15 Replies
Login or Register to Ask a Question