UNIX awk pattern matching and printing lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX awk pattern matching and printing lines
# 1  
Old 10-14-2015
UNIX awk pattern matching and printing lines

I have the below plain text file where i have some result, in order to mail that result in html table format I have written the below script and its working well. cat result.txt
Page 2015-01-01 2000
Colors 2015-02-01 3000
Landing 2015-03-02 4000

Code:
  #!/bin/sh LOG=/tmp/maillog.txt RES=/tmp/result.txt  html_log () { awk ' BEGIN {  print "<html><body><table border=1 cellspacing=0 cellpadding=3>" print "<tr>" print "<td><b>Metric</b></td>"; print "<td><b>Date</b></td>"; print "<td><b>count</b></td>"; print "</tr>" } { print "<tr>" print "<td>"$1"</td>"; print "<td>"$2"</td>"; print "<td>"$3"</td>"; print "</tr>" } END { print "</table></body></html>" } ' $RES >> $LOG }  #Function for sending mails dd_mail () { ( echo "From:xyz " echo "To: xyz" echo "MIME-Version: 1.0" echo "Subject: Emp rpt"  echo "Content-Type: text/html"  cat $LOG ) | sendmail -t  html_log dd_mail  exit 0

Now the problem is very rarely these 3 metrics are repeating (refer below), if it repeats i want to identify and generate the separate html table so that when i send email to users they can see 2 separate html tables.

cat result.txt Page 2015-01-01 2000 Colors 2015-02-01 3000 Landing 2015-03-02 4000 Page 2015-01-01 1000 Colors 2015-02-01 2000 Landing 2015-03-02 9000 I tried pattern matching and print the lines but none of my idea's worked could someone help me on this.

Thanks
# 2  
Old 10-14-2015
How do you identify the end of the first and the start of the second table? Always three lines? Always "Page" in the first line?

---------- Post updated at 12:50 ---------- Previous update was at 10:41 ----------

Try
Code:
awk '
/Page/  {print "<html><body><table border=1 cellspacing=0 cellpadding=3>"
         print "<tr>"
         print "<td><b>Metric</b></td>"
         print "<td><b>Date</b></td>"
         print "<td><b>count</b></td>"
         print "</tr>"
        }

        {print "<tr>"
         print "<td>"$1"</td>"
         print "<td>"$2"</td>"
         print "<td>"$3"</td>"
         print "</tr>"
        }

/Land/  {print "</table></body></html>"
        }

' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on pattern matching and printing the same

Hi, I need to match for the pattern '.py' in my file and print the word which contains. For example: cat testfile a b 3 4.py 5 6 a b.py c.py 4 5 6 7 8 1.py 2.py 3 4 5 6 Expected output: 4.py b.py c.py 1.py 2.py (3 Replies)
Discussion started by: Sumanthsv
3 Replies

2. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

3. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

4. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

5. Shell Programming and Scripting

Help With AWK Matching and Re-printing Lines

Hi All, I'm looking to use AWK to pattern match lines in XML file - Example patten for below sample would be /^<apple>/ The sample I wrote out is very basic compared to what I am actually working with but it will get me started I would like to keep the matched line(s) unchanged but have them... (4 Replies)
Discussion started by: rhoderidge
4 Replies

6. Shell Programming and Scripting

Multiple pattern matching using awk and getting count of lines

Hi , I have a file which has multiple rows of data, i want to match the pattern for two columns and if both conditions satisfied i have to add the counter by 1 and finally print the count value. How to proceed... I tried in this way... awk -F, 'BEGIN {cnt = 0} {if $6 == "VLY278" &&... (6 Replies)
Discussion started by: aemunathan
6 Replies

7. Shell Programming and Scripting

fgrep not printing non matching lines

I'm using this: fgrep -f file1.txt file2.txt To find lines in file1 that match patterns found in file2. When I add -v: egrep -v -f file1.txt file2.txt It won't return non matching lines, I just get a blank. Can anyone help? PS. file1.txt contains 3 million lines...each string... (2 Replies)
Discussion started by: Nonito84
2 Replies

8. Shell Programming and Scripting

Pattern Matching and printing

Dear All, I have a log file like below 13:26:31 |152.22 13:27:31 |154.25 13:28:31 |154.78 13:29:31 |151.23 13:30:31 |145.63 13:31:31 |142.10 13:32:31 |145.45 where values will be there from 00:00 hrs to 23:59 hrs. I'm matching for last occurance of 23:59 and printing 1440 lines (grep... (4 Replies)
Discussion started by: Naga06
4 Replies

9. UNIX for Dummies Questions & Answers

Pattern matching and Printing Filename

Hi, My requirement is to search for a paritcular string from a group of .gz files and to print the lines containing that string and the name of the files in which that string is present. Daily 500 odd .gz files will be generated in a directory(directory name will be in the form of... (4 Replies)
Discussion started by: krao
4 Replies

10. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies
Login or Register to Ask a Question