awk pattern matching in place of sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk pattern matching in place of sed
# 1  
Old 12-06-2013
awk pattern matching in place of sed

I have written a script to parse data from some files on a Solaris 10 system and send the output to a CSV formatted file. The code snipped i am using to pull the data is as follows....

Code:
src_line=$(sed -n "/^search_pattern$/{=;}" $file)
for i in $src_line
do
start_line1=$(( i + 9 ))
nawk -v startline1="$start_line1" '
BEGIN{i=0; printf "%s, %s, %s, %s, %s\n", "Header 1", "Header 2", "Header 3", "Header 4", "Header 5"}
NR>=startline1 {if ($2 ~ /^[0-9]/)
{if ($4 ~ /^[0-9]/)
{fld4=$4}
else{fld4=""}
if (i == 0)
{i=1; printf "%s, %s, %s, %s, %s\n", $2, fld4, $6, $8, $10}
else{i=0; printf "%s, %s, %s, %s, %s\n", $2, fld4, $6, $8, $10}}
else{exit}}' $file > results.csv
done

I use SED to find the pattern in the file and report back with a line number.
It is possible that there can be more than one match in a file and all matches must be processed. After the match is found i skip n line, 9 in this case. If the vaule of filed 2 is numeric then Fileds 2,4,6....n are printed, If not then i know i have reached the end of the table which i am pulling data from.
I am trying to find a soulution to use AWK entirely for the process above but so far i have had no luck.
I can get AWK to find the pattern match, but i am unable to get it to skip the 9 lines.

Below is a sample from of data from a file i am processing

Code:
Table Header
++++++++++++++++++++++++++++++++++++++++++++++
+ Data Desp +
++++++++++++++++++++++++++++++++++++++++++++++
+ + + AV +
+ + +++++++++++++++++++++++++++++++++
+ + + A + B + C + D +
+ Col + Pos + + + + +
++++++++++++++++++++++++++++++++++++++++++++++
+ 1 + 1 + 11 + 3 + 0.1 + 0.2 +
+ 2 + 2 + 13 + 3 + 0.1 + 0.3 +
+ 3 + 1 + 9 + 3 + 0.1 + 0.4 +
+ 4 + 2 + 5 + 3 + 0.1 + 0.5 +
+ 5 + 1 + 21 + 3 + 0.1 + 0.6 +
+ 6 + 2 + 8 + 3 + 0.1 + 0.1 +
+ 7 + 1 + 7 + 3 + 0.1 + 0.4 +
+ 8 + 2 + 18 + 3 + 0.1 + 0.6 +
+ 9 + 1 + 45 + 3 + 0.1 + 0.7 +
+ 10 + 2 + 6 + 3 + 0.1 + 0.0 +
++++++++++++++++++++++++++++++++++++++++++++++

PS daa should look like this when opens in a plain txt editor.

Image

Regards
Denis.

Last edited by electricheadx; 12-06-2013 at 06:49 AM..
# 2  
Old 12-06-2013
In awk, try
Code:
... 
/search pattern/ {L=NR+9}
NR>=L            {...}

This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-07-2013
Quote:
Originally Posted by RudiC
In awk, try
Code:
... 
/search pattern/ {L=NR+9}
NR>=L            {...}

I tried the above piece of code and it does give me the correct line to start on, but if i try to run with the code below i get no output.

Code:
nawk '/search pattern/ {L=NR+10} 
NR>=L {if ($2 ~ /^[0-9]/){print $2} else{exit}}' filename

I though it might be possile to use a for loop to run through any value stored in the varible L but i get an error which states

Code:
nawk: can't assign to L; it's an array name.

Denis.
# 4  
Old 12-07-2013
What's your search pattern? Without knowing that it's quite difficult to interpret the awk script's behaviour.

Assuming the pattern you search for be ++++..., this will print the desired fields from your input sample:
Code:
awk     'NR<L           {next}
         /^\+\+\+\+/    {if (L) exit; else L=NR+8}
         $2 ~ /^[0-9]/  {print $2, $4, $6}
        ' file
1 1 11
2 2 13
3 1 9
4 2 5
5 1 21
6 2 8
7 1 7
8 2 18
9 1 45
10 2 6

It will NOT scan for further occurrences of the pattern, but this could easily be implemented
# 5  
Old 12-11-2013
Sorry for the delayed response.
The search pattern will always be the section "Table Header".
An example of header would be "Output Offset 1". The target file i am scanning is made up of 100's of instances of such tables all with different headers.
The number of occurences of rows listed in the table will range from 1 to N.
I will never know beforehand the number of rows, so this is why i have always checked to see if field 2 is numeric or not. If its not numeric i know i have reached the end of the table.

Regards
Denis.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. Shell Programming and Scripting

Help with sed pattern matching

Hi My log file is Testtmp2 cat Testtmp2 12:12:38 12:14:29 12:17:34 12:19:08 12:20:10 12:21:35 12:22:20 12:22:26 12:22:34 12:22:38 12:28:14 12:31:35 12:32:50 12:33:04 (3 Replies)
Discussion started by: rahkumar
3 Replies

4. Shell Programming and Scripting

Place digit in front of the line searching pattern using sed command

hi All, i want to add the single digit front of the line in the report file and string compare with pattern file. patter file: pattern1.txt pattern num like 4 love 3 john 2 report file: report.txt i like very much but john is good boy i will love u so after execute... (9 Replies)
Discussion started by: krbala1985
9 Replies

5. UNIX for Dummies Questions & Answers

Matching pattern script (sed or awk?)

Hi Guys, I am new to the forum and to scripting so bear with me. Thanks, Gary. I have 3 files - file1, file2, file3 I am trying to come up with a script that will check the output of these files and if the 1st nine fields are matched in all 3 files, echo "The following string had been... (2 Replies)
Discussion started by: gazza-o
2 Replies

6. Shell Programming and Scripting

help! Pattern Matching in vi or sed

I have a bunch of conf files, that contain the fully qualified names of servers. I would like to be able to use some sort of pattern matching with sed or vi, or whatever, to pull out the fully qualified server names, and dump them in a file. It just needs to work across several unix os. So, I... (4 Replies)
Discussion started by: tabini
4 Replies

7. Shell Programming and Scripting

sed pattern matching

Hi all, if I have the following piece (repeating) of text within a file and I wish to delete it via sed a b c d e <tr> <td><img alt="" height="1" width="3" src="/testweb/view/browser/images/shim.gif"></td><td><img alt="" height="1" ... (4 Replies)
Discussion started by: srage
4 Replies

8. Shell Programming and Scripting

sed - matching pattern one but not pattern two

All, I have the following file: -------------------------------------- # # /etc/pam.d/common-password - password-related modules common to all services # # This file is included from other service-specific PAM config files, # and should contain a list of modules that define the services... (2 Replies)
Discussion started by: RobertBerrie
2 Replies

9. UNIX for Dummies Questions & Answers

Pattern matching New to Sed & Awk

Hello, Despite reading the Pattern Matching chapter in the O'Reilly Sed & Awk book several times and looking at numerous examples, I cannot seem to get any kind of conditional script to work in my awk scripts! I am able to do the basic awk and grep script to capture the data but when I do with... (0 Replies)
Discussion started by: pg55
0 Replies

10. Shell Programming and Scripting

Pattern matching sed

MSG="THERE WERE XX RECORDS IN ERROR TABLE,AAAA, WHEN LOADING THE BBBB TABLE WITH EXTRACT FROM CCCC INTO TABLES FOR , DATABASE DDDD." echo "$MSG" > /tmp/mplanmsg.$$.out I wan to replace XX with the content in $recordXX cat /tmp/mplanmsg.$$.out|sed 's/XX/\$recordXX/g'| sed... (3 Replies)
Discussion started by: leemjesse
3 Replies
Login or Register to Ask a Question