awk: need to extract a line before a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk: need to extract a line before a pattern
# 15  
Old 06-26-2008
I want this

I a file with lines containing 3 columns each,

If column 3 of a line is zero, then
substract the second entry of present column from
the second entry of the prvious column.

e.g.

28 -4.8743 2
29 -4.8726 2
30 -4.7833 2
31 -4.7721 2
32 -4.7690 2
33 -4.6266 2
34 -4.6203 2
35 -3.8990 1
36 -3.2009 0

37 -2.9944 0
38 -2.9934 0
39 -2.8856 0
40 -2.8820 0

then it should subtract -3.2009 from -3.8990 and give me the answer.
# 16  
Old 06-26-2008
subtraction from previous column

Hi friends,

I a file with lines containing 3 columns each,
I want that,

If column 3 of a line is zero, then
substract the second entry of present column from
the second entry of the previous column.

e.g.

28 -4.8743 2
29 -4.8726 2
30 -4.7833 2
31 -4.7721 2
32 -4.7690 2
33 -4.6266 2
34 -4.6203 2
35 -3.8990 1
36 -3.2009 0

37 -2.9944 0
38 -2.9934 0
39 -2.8856 0
40 -2.8820 0

then it should subtract -3.2009 from -3.8990
and give me the answer.

Thanks!
Smilie
# 17  
Old 06-26-2008
I am assuming the pattern appears in the first field:

Code:
$ cat c.sh
#! /bin/sh

if [ $# -ne 2 ]; then
        echo "Usage: $0 <input> <pattern>"
        exit 1
fi


awk '
$1 ~ /^'$2'$/ {
        for (i=1;i<=11;++i) {
                n=getline
                if (n==0) {
                        exit
                }
        }
        print
        exit
}' $1

$ cat c.in
Line1 a line
Line2 a line
Line3 a line
Line4 a line
Line5 a line
Line6 a line
Line7 a line
Line8 a line
Line9 a line
Line10 a line
Line11 a line
Line12 a line
Line13 a line
Line14 a line
Line15 a line

$ ./c.sh c.in Line1
Line12 a line

$ ./c.sh c.in Line4
Line15 a line

$ ./c.sh c.in Line5

$

# 18  
Old 06-29-2008
Quote:
Originally Posted by kvaibhav
then it should subtract -3.2009 from -3.8990
and give me the answer.
Oneliner :
Code:
awk '{_[NR%2]=$2;l[NR%2]=$NF}$NF==0 && l[(NR+1)%1]!=0 {print _[(NR+1)%1]-$2}' file

.. or script. Smilie
Code:
#!/bin/sh
x=0 && y=0;
while read a b c
do
  if test \( $c -gt 0 \) ; then
    x=1 && y="$b";
  elif test \( $c -eq 0 \) -a \( $x -eq 1 \)  -a \( ! -z $y \); then
    echo "$y - $b" | bc && x=0 && y=0;
  fi
done < file


Last edited by danmero; 06-30-2008 at 12:39 AM.. Reason: Add script version
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to extract and print first occurrence of pattern in each line

I am trying to use awk to extract and print the first ocurrence of NM_ and NP_ with a : before in each line. The input file is tab-delimeted, but the output does not need to be. The below does execute but prints all the lines in the file not just the patterns. Thank you :). file tab-delimeted ... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

3. Shell Programming and Scripting

Search for a pattern,extract value(s) from next line, extract lines having those extracted value(s)

I have hundreds of files to process. In each file I need to look for a pattern then extract value(s) from next line and then search for value(s) selected from point (2) in the same file at a specific position. HEADER ELECTRON TRANSPORT 18-MAR-98 1A7V TITLE CYTOCHROME... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

4. Shell Programming and Scripting

[Awk] Extract block of with a particular pattern

Hi, I have some CVS log files, which are divided into blocks. Each block has many fields of information and I want to extract those blocks with a pattern. Here is the sample input. RCS file: /cvsroot/eclipse/org.eclipse.debug.core/core/org/eclipse/debug/core/DebugPlugin.java,v head: 1.174... (7 Replies)
Discussion started by: sandeepk1611
7 Replies

5. Shell Programming and Scripting

extract specific line if the search pattern is found

Hi, I need to extract <APPNUMBER> tag alone, if the <college> haas IIT Chennai value. college tag value will have spaces embedded. Those spaces should not be suppresses. My Source file <Record><sno>1</sno><empid>E0001</empid><name>Rejsh suderam</name><college>IIT ... (3 Replies)
Discussion started by: Sekar1
3 Replies

6. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

7. Shell Programming and Scripting

Extract pattern from text line

The text line has the following formats: what.ever.bla.bla.C01G06.BLA.BLA2 what.ever.bla.bla.C11G33.BLA.BLA2 what.ever.bla.bla.01x03.BLA.BLA2 what.ever.bla.bla.03x05.BLA.BLA2 what.ever.bla.bla.Part01.BLA.BLA2 and other similar ones, I need a way to select the "what.ever.bla.bla" part out... (4 Replies)
Discussion started by: TehOne
4 Replies

8. Shell Programming and Scripting

Extract pattern from text line

Hi, the text line looks like this: "test1" " " "test2" "test3" "test4" "10" "test 10 12" "00:05:58" "filename.bin" "3.3MB" "/dir/name" "18459" what's the best way to select any of it? So I can for example get only the time or size and so on. I was trying awk -F""" '{print $N}' but... (3 Replies)
Discussion started by: TehOne
3 Replies

9. Shell Programming and Scripting

Extract pattern from text line

Gents, from these sample lines: ZUCR.MI ZUCCHI SPA RISP NC 2,5000 6 ott 0,0000 ZV.MI ZIGNAGO VETRO 3,6475 16:36 Up 0,0075 is it possible to get this: ZUCR.MI 2,5000 ZV.MI 3,6475 i.e. the first field, a separator and the first decimal number? (in Europe we... (9 Replies)
Discussion started by: vampirodolce
9 Replies

10. Shell Programming and Scripting

extract a particular start and end pattern from a line

hi In the foll example the whole text in a single line.... i want to extract text from IPTel to RTCPBase.h. want to use this acrooss the whole file Updated: IPTel\platform\core\include\RTCPBase.h \main\MWS2051_Sablime_Int\1... (7 Replies)
Discussion started by: manish205
7 Replies
Login or Register to Ask a Question