awk -v dilema


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk -v dilema
# 1  
Old 09-09-2015
awk -v dilema

Hello Folks,
How do I use awk for passing, seraching and returning certain lines above a pattern variable

Code:
 
numbers.txt
 
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen

Code:
for text in ten thirteen sixteen
do
        awk -v wText="$text" '{a[++i]=$0;}/wText/{for(j=NR-4;j<=NR;j++)print a[j];}' numbers.txt > tmpfile
done

Output needed as below
Code:
six
seven
eight
nine
ten
 
nine
ten
eleven
twelve
thirteen


Last edited by jville; 09-09-2015 at 11:27 AM..
# 2  
Old 09-09-2015
Code:
$ for text in ten thirteen sixteen; do awk -v wText="$text" '{a[++i]=$0} $0 ~ wText {for(j=NR-4; j<=NR; j++) {print a[j]} print "" }' number.txt; done
six
seven
eight
nine
ten

nine
ten
eleven
twelve
thirteen

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 09-09-2015
awk -f jv.awk numbers.txt where jv.awk is:
Code:
BEGIN {
  if (!pat) pat="ten thirteen sixteen"
  if (!lines) lines=4
  split(pat, tA, FS)
  for(i=1;i in tA;i++)
    patA[tA[i]]
}
{ a[FNR]=$0 }
END {
  for(i=1;i<=FNR;i++)
    if (a[i] in patA) {
       for(j=i-lines;j<=i;j++)
         print a[j]
       print ""
    }
}

or with specific strings to search:
awk -v pat='seven fifteen' -f jv.awk numbers.txt
or to get 4 lines above
awk -v pat='seven fifteen' -v lines=3 -f jv.awk numbers.txt

Last edited by vgersh99; 09-09-2015 at 11:54 AM..
This User Gave Thanks to vgersh99 For This Post:
# 4  
Old 09-09-2015
grep -B

On my linux box, I use:

Code:
grep -B4 teen numbers.txt

nine
ten
eleven
twelve
thirteen
fourteen
fifteen

-A option for "lines after"
# 5  
Old 09-09-2015
Resolved.
Much appreciate your valuebale solutions.

Zaxxons solution fitted the best, altough vgresh99 solution is comparable.

quirkasaurus , your solution is good as well but works only if the string has "teen" in it.
# 6  
Old 09-09-2015
Hi.

Compare egrep and cgrep:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate match with previous lines, allow overlap, cgrep.
# For cgrep, see:
# http://sourceforge.net/projects/cgrep/ verified Wed Sep  9 16:12:03 CDT 2015

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C egrep cgrep

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results, egrep, do not expect separation of overlaps:"
egrep -B4 'ten|thirteen|sixteen' $FILE

pl " Results, cgrep, \"-o\" allows display of separate overlaps:"
cgrep -E -o -4 'ten|thirteen|sixteen' $FILE

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
egrep GNU grep 2.5.3
cgrep ATT cgrep 8.15

-----
 Input data file data1:
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen

-----
 Results, egrep, do not expect separation of overlaps:
six
seven
eight
nine
ten
eleven
twelve
thirteen

-----
 Results, cgrep, "-o" allows display of separate overlaps:
========================================
six
seven
eight
nine
ten
========================================
nine
ten
eleven
twelve
thirteen

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk output yields error: awk:can't open job_name (Autosys)

Good evening, Im newbie at unix specially with awk From an scheduler program called Autosys i want to extract some data reading an inputfile that comprises jobs names, then formating the output to columns for example 1. This is the inputfile: $ more MapaRep.txt ds_extra_nikira_usuarios... (18 Replies)
Discussion started by: alexcol
18 Replies

2. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

4. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

5. Shell Programming and Scripting

Problem with awk awk: program limit exceeded: sprintf buffer size=1020

Hi I have many problems with a script. I have a script that formats a text file but always prints the same error when i try to execute it The code is that: { if (NF==17){ print $0 }else{ fields=NF; all=$0; while... (2 Replies)
Discussion started by: fate
2 Replies

6. Shell Programming and Scripting

Read content between xml tags with awk, grep, awk or what ever...

Hello, I trying to extract text that is surrounded by xml-tags. I tried this cat tst.xml | egrep "<SERVER>.*</SERVER>" |sed -e "s/<SERVER>\(.*\)<\/SERVER>/\1/"|tr "|" " " which works perfect, if the start-tag and the end-tag are in the same line, e.g.: <tag1>Hello Linux-Users</tag1> ... (5 Replies)
Discussion started by: Sebi0815
5 Replies

7. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

8. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

9. UNIX for Dummies Questions & Answers

scripting dilema

I have a script which writes to a file using date and the end filename would be filename`date +%d%b%y. Now when i have 2 files ( 2 days ), i want to compare them using an automated script and then put the differences if there are into another file. Can someone please help me how to automize... (4 Replies)
Discussion started by: family_guy
4 Replies
Login or Register to Ask a Question