search text file in file if this file contains necessary text (awk,grep)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search text file in file if this file contains necessary text (awk,grep)
# 1  
Old 04-19-2011
search text in file if this it contains necessary text (awk,grep)

Hello friends!
Help me pls to write correct awk and grep statements for my task:

I have got files with name filename.txt
It has such structure:
Start of file
FROM: address@domen.com (12...890) abc
DATE: 11/23/2009 on Std
SUBJECT: any subject
End of file


So, I must check,
if this file DOESN'T CONTAIN mail address = mike@domen.com,
I must save text address@domen.com(after "FROM: " and before first white-space after mail-address) to variable $Address and 11/23/2009 (after "DATE: "and before first white-space after date) to variable $Date.

So, I need to write bash script like this:

PHP Code:
if [ ! grep('mike@domen.com',textfile.txt) ] then
 Address
=awk '/^FROM: /,/^ /' textfile.txt
 Date
=awk '/^DATE: /,/^ /' textfile.txt
 
echo $Address $Date
else
 echo 
"sorry, but this file contains mike@domen.com text :("
fi 


P.S. This code, of course, doesn't work. Pls help me to fix it.

Last edited by candyme; 04-19-2011 at 04:20 PM..
# 2  
Old 04-19-2011
Try:
Code:
if [ -z "$(grep mike@domen.com filename.txt)" ]; then
  Address=$(awk '/^FROM:/{print $2}' filename.txt)
  Date=$(awk '/^DATE:/{print $2}' filename.txt)
  echo $Address $Date
else
  echo "sorry, but this file contains mike@domen.com text :("
fi

# 3  
Old 04-20-2011
Thank you! Smilie

---------- Post updated 04-20-11 at 02:38 AM ---------- Previous update was 04-19-11 at 02:29 PM ----------

Sorry, but my file has some different format:
Start of file
FROM: "some name "<name@server.com>
DATE: Wed, 18 Apr 2011 02:53:12 +0400
SUBJECT: any subject
End of file


I need to get name@server.com in $Address and
18 Apr 2011 02:53:12 converting in 18.04.2011 02:52:12 in $Date

Please, change these awk statements to the correct:
PHP Code:
Address=$(awk '/^FROM:/{print $2}' filename.txt)
  
Date=$(awk '/^DATE:/{print $2}' filename.txt
---------- Post updated at 08:34 AM ---------- Previous update was at 02:38 AM ----------

Last edited by candyme; 04-20-2011 at 04:48 AM..
# 4  
Old 04-20-2011
eval $(nawk -f candy.awk filename.txt)
echo $Address
echo $Date

candy.awk:
Code:
BEGIN {
 q=sprintf("%c", 039)
 mon="JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC"
   monN=split(mon, monA, "|");
   for(i=1; i<=monN; i++) {
     monA[monA[i]]=i;
     delete monA[i];
   }
}
/^FROM:/ { n=split($0,a,"[<>]"); print "Address=" a[n-1]}
/^DATE:/ {printf("Date=%c%.02d.%.02d.%d %s%c\n",q, $3,monA[toupper($4)],$5,$6,q)}

# 5  
Old 04-20-2011
vgersh99

Thank you!

but can I use your script in this one:

Code:
if [ ! grep('mike@domen.com',textfile.txt) ] then
 Address=awk '/^FROM: /,/^ /' textfile.txt
 Date=awk '/^DATE: /,/^ /' textfile.txt
 echo $Address $Date
else
 echo "sorry, but this file contains mike@domen.com text :("
fi

??
in one script-file
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 04-20-2011 at 11:23 AM.. Reason: code tags, please!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Efficient way to search array in text file by awk

I have one array SPLNO with approx 10k numbers.Now i want to search the subscriber number from MDN.TXT file (containing approx 1.5 lac record)from the array.if subscriber number found in array it will perform below operation.my issue is that it's taking more time because for one number it's search... (6 Replies)
Discussion started by: siramitsharma
6 Replies

3. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

4. Shell Programming and Scripting

Search text beween tags and write to file using awk

Hi Friends, I have a very big text file, that has code for multiple functions. I have scan through the file and write each function in seperate file. All functions starts with BEGIN DSFNC Identifier "ABCDDataValidationfnc" and ends with END DSFNC I need create a file(using identifier)... (2 Replies)
Discussion started by: anandapani
2 Replies

5. Shell Programming and Scripting

Search and replace from file in awk using a 16 bit text file

Hello, Some time ago a helpful awk file was provided on the forum which I give below: NR==FNR{A=$0;next}{for(j in A){split(A,P,"=");for(i=1;i<=NF;i++){if($i==P){$i=P}}}}1 While it works beautifully on English and Latin characters i.e. within the ASCII range of 127, the moment a character beyond... (6 Replies)
Discussion started by: gimley
6 Replies

6. UNIX for Dummies Questions & Answers

How to grep multiple lines from a text file using another text file?

I would like to use grep to select multiple lines from a text file using a single-column text file. Basically I want to only select lines from the first text file where the second column of the first text file matches the second text file. How do I go about doing that? Thanks! (5 Replies)
Discussion started by: evelibertine
5 Replies

7. Shell Programming and Scripting

Search text file, then grep next instance of string

I need to be able to search for a beginning line header, then use grep or something else to get the very next instance of a particular string, which will ALWAYS be in "Line5". What I have is some data that appears like this: Line1 Line2 Line3 Line4 Line5 Line6 Line7 Line1 Line2 ...... (4 Replies)
Discussion started by: Akilleez
4 Replies

8. Shell Programming and Scripting

search needed part in text file (awk?)

Hello! I have text file: From aaa@bbb Fri Jun 1 10:04:29 2010 --____OSPHWOJQGRPHNTTXKYGR____ Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline My code '234565'. ... (2 Replies)
Discussion started by: candyme
2 Replies

9. Shell Programming and Scripting

text file search and replace with awk

hello all greeting for the day i have a text file as the following text.xml abcd<FIELD>123.456</FIELD>efgh i need to replace the value between <FIELD> and </FIELD> by using awk command. please throw some light on this. thank you very very much Erik (5 Replies)
Discussion started by: erikshek
5 Replies

10. Shell Programming and Scripting

Search text from a file and print text and one previous line too

Hi, Please let me know how to find text and print text and its previous line. Please don't get irritated few days back I asked text and next line. I am using HP-UX 11.11 Thanks for your help. (6 Replies)
Discussion started by: kamranjalal
6 Replies
Login or Register to Ask a Question