Visit Our UNIX and Linux User Community


awk to print before and after lines then count of patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to print before and after lines then count of patterns
# 1  
Old 03-30-2016
awk to print before and after lines then count of patterns

What i'm trying to do here is show X amount of lines before and after the string "serialNumber" is found.

Code:
BEFORE=3
AFTER=2
gawk '{a[NR]=$0} {count=0} /serialNumber/ && /./ {for(i=NR-'"${BEFORE}"';i<=NR;i++){count++ ;print a[i]}for(i=1;i<'"${AFTER}"';i++){getline; print ; count ++; print count}exit}' datafile

it's not outputting correctly.

the goal here is to have the awk command do the following:

1. show the 3 lines that come BEFORE 'serialNumber'
2. show the actual line containing 'serialNumber'
3. show the 2 lines that come AFTER 'serialNumber'
4. show the count of lines found containing 'serialNumber'

if more than one line is found containing 'serialNumber', only steps 1 through 3 should be performed on each line found.

then at the very end of the output, there should be the total number of lines containing 'serialNumber'.

i know its complicated. can this be done?
# 2  
Old 03-30-2016
Deleted. More concise script provided by Chubler_XL on this thread.

Last edited by rdrtx1; 03-31-2016 at 12:39 AM.. Reason: updated for req. 4 (count in block of lines)
This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 03-30-2016
Can you post a sample of the input file...
# 4  
Old 03-30-2016
We could simplify rdrtx1's the code to:

Code:
awk '
{l[NR]=$0;}
/serialNumber/ {c++; t++; for(i=NR-3; i<=NR+2; i++) o[i]=c; next}
{c=0}
END {
   for(i=1; i<=NR; i++) {
       if (o[i]) print l[i];
       if (o[i]>1 && !(o[i+1]))
            print "cnt: " o[i];
   }
   print "total count: " t+0;
}
' infile

# 5  
Old 03-31-2016
Never mind then.

Last edited by rdrtx1; 03-31-2016 at 12:38 AM..
# 6  
Old 03-31-2016
Some further testing has highlighted issues when display regions overlap e.g.:

Code:
1
2
3
4 serialNumber
5
6
7
8
9
10
11
12
13 serialNumber
14 serialNumber
15
16
17 serialNumber
18 serialNumber
19 serialNumber
20 serialNumber
21

This updated code should display correct "cnt: " subtotals: and uses variables BEFORE and AFTER

Code:
awk -v BEFORE=3 -v AFTER=2 '
{l[NR]=$0}
/serialNumber/ {
   c++
   t++
   for(i=NR-BEFORE; i<=NR+AFTER; i++)
      if(i>NR||!o[i]) o[i]=c
}
!o[NR-BEFORE]{c=0}
END {
   for(i=1; i<=NR; i++) {
       if (o[i]) {
           print l[i]
           if (i==NR||o[i+1]<o[i])
            print "cnt: " o[i]
       }
   }
   print "total count: " t+0
}' infile


Last edited by Chubler_XL; 03-31-2016 at 01:48 AM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #378
Difficulty: Medium
AIX was UNIX 03 compliant.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. Shell Programming and Scripting

How to print different multiple lines after two patterns?

Hello, I need to print some lines as explained below, TXT example 1111 2222 3333 4444 5555 6666 7777 8888 6666 9999 1111 2222 3333 4444 5555 (8 Replies)
Discussion started by: liuzhencc
8 Replies

3. Shell Programming and Scripting

Match 2 different patterns and print the lines

Hi, i have been trying to extract multiple lines based on two different patterns as below:- file1 @jkm|kdo|aas012|192.2.3.1 blablbalablablkabblablabla sjfdsakfjladfjefhaghfagfkafagkjsghfalhfk fhajkhfadjkhfalhflaffajkgfajkghfajkhgfkf jahfjkhflkhalfdhfwearhahfl @jkm|sdf|wud08q|168.2.1.3... (8 Replies)
Discussion started by: redse171
8 Replies

4. Shell Programming and Scripting

Perl : to print the lines between two patterns

Hello experts, I have a text file from which I need to print all the lines between the patterns. Could anyone please help me with the perl script. names.txt ========= Badger Bald Eagle Bandicoot Bangle Tiger Barnacle Barracuda Basilisk Bass Basset Hound Beetle Beluga... (7 Replies)
Discussion started by: scriptscript
7 Replies

5. Shell Programming and Scripting

Print all lines between patterns

Hi Gurus, I have a requirement where I need to display all lines between 2 patterns except the line where the first pattern in it. I tried the following command using awk but it is printing all lines except the lines where the 2 patterns exist. awk '/TRANSF_/{ P=1; next } /Busy/ {exit} P'... (9 Replies)
Discussion started by: svajhala
9 Replies

6. Shell Programming and Scripting

How to print only lines in between patterns?

Hi, I want to print only lines (green-italic lines) in between first and last strings in column 9. there are different number of lines between each strings. 10 AUGUSTUS exon 4558 4669 . - . 10.g1 10 AUGUSTUS exon 8771 8889 . ... (6 Replies)
Discussion started by: jamo
6 Replies

7. Shell Programming and Scripting

Need to print between patterns AND a few lines before

I need to print out sections (varying numbers of lines) of a file between patterns. That alone is easy enough: sed -n '/START/,/STOP/' I also need the 3 lines BEFORE the start pattern. That alone is easy enough: grep -B3 START But I can't seem to combine the two so that I get everything between the... (2 Replies)
Discussion started by: Finja
2 Replies

8. Shell Programming and Scripting

print lines between 2 matching patterns

Hi Guys, I have file like below, I want to print all lines between test1231233 to its 10 occurrence(till line 41) test1231233 qwe qwe qweq123 test1231233 qwe qwe qweq23 test1231233 qwe qwe qweq123 test1231233 qwe qwe qweq123131 (3 Replies)
Discussion started by: jagnikam
3 Replies

9. Shell Programming and Scripting

print lines between two patterns in unix

Detroit Chicago Newyork Battlecreek Jackson Brooklyn How would I print only lines match between Detroit and Brooklyn used awk ? I don't want print Detroit and Brooklyn output should be : Chicago Newyork Battlecreek Jackson Thanks Jhonny (2 Replies)
Discussion started by: jhonnyrip
2 Replies

10. Shell Programming and Scripting

Count lines between two patterns inside a file

Hi, Im doing a script to find the number of lines included inside a file newly. These lines are in between #ifdef FLAG1 and #else or #endif or #else and #endif. I tried like this, awk '/#ifdef Flag1/,/#e/{print}' aa.c | wc -l awk '/#ifndef Flag1/,/#endif/{print}' aa.c | awk... (6 Replies)
Discussion started by: priyadarshini
6 Replies

Featured Tech Videos