How to extract the required lines?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract the required lines?
# 8  
Old 12-29-2009
Quote:
Originally Posted by 14341
could you explain the logic behind this so that i can tweak this later?
Code:
$ awk 'NR>1 && $1!=x{print a ORS b}{a=b;b=$0;x=$1}' file
a 3
a 4
b 3
b 4

If you could tell the logic i will make the necessary changes such that it also prints "c 3", "c 4" in the output.

Code:
$  awk 'NR>1 && ($1 != p) {print a[c-1]"\n"a[c] ;c=0} {p=$1 ; a[++c]=$0 }' infile
a 3
a 4
b 3
b 4

"c 3"
"c 4"
are missing from the output.

Please explain the logic so that i can modify it.

I am using BASH shell.

Thank you.

Cheers,
14341

Either you add an empty line to the end of infile by using (echo "" >> infile) then execute or modify the code to below

Code:
awk 'NR>1 && ($1 != p) {print a[c-1]"\n"a[c] ; c=0} {p=$1 ; a[++c]=$0 }
END{print  a[c-1]"\n"a[c] }' infile

SmilieSmilieSmilie
# 9  
Old 12-29-2009
Except for the first line read, it checks to see when the first field changes (a->b, b->c, etc). When this happens, it prints out the desired lines (using the pr_lines function) which have been collected in an array. At end of file, pr_lines is run again to print any lines since the last printing

$ cat a.awk
Code:
function pr_lines() { for (i=j-N; i<j; i++) print a[i]; j=0 }
$1!=old1 && NR!=1 { pr_lines() }
{ a[j++]=$0; old1=$1 }
END { pr_lines() }

$ awk -v N=1 -f a.awk data
a 4
b 4
c 4

$ awk -v N=2 -f a.awk data
a 3
a 4
b 3
b 4
c 3
c 4

awk -v N=3 -f a.awk data
a 2
a 3
a 4
b 2
b 3
b 4
c 2
c 3
c 4
# 10  
Old 12-29-2009

but my code does not work like that and it take less memory than your code.
# 11  
Old 12-29-2009
Bug

Thank you danmero ,ahmad.diab, thegeek and alister for all your efforts.

It's been a good experience with AWK. I'll dig deep into this stuff.

The problem has been solved.

Thank you once again.

Cheers,
14341
# 12  
Old 12-29-2009
Quote:
Originally Posted by 14341
could you explain the logic behind this so that i can tweak this later?
Code:
$ awk 'NR>1 && $1!=x{print a ORS b}{a=b;b=$0;x=$1}' file
a 3
a 4
b 3
b 4

If you could tell the logic i will make the necessary changes such that it also prints "c 3", "c 4" in the output.
Ops , we should work after the END Smilie
Code:
awk 'END{print a,b}$1!=x && a{print a,b}{a=b;b=$0;x=$1}' OFS="\n" file

Should work now Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

2. AIX

Grepping before and after lines for required string

Hi All, I am new to AIX unix . i need to grep for a pattern and if pattern is found then i need 3 before the pattern line found and 3 lines after the pattern found. (11 Replies)
Discussion started by: coolvibh
11 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. UNIX for Dummies Questions & Answers

Extract lines with specific words with addition 2 lines before and after

Dear all, Greetings. I would like to ask for your help to extract lines with specific words in addition 2 lines before and after these lines by using awk or sed. For example, the input file is: 1 ak1 abc1.0 1 ak2 abc1.0 1 ak3 abc1.0 1 ak4 abc1.0 1 ak5 abc1.1 1 ak6 abc1.1 1 ak7... (7 Replies)
Discussion started by: Amanda Low
7 Replies

5. Shell Programming and Scripting

Help with extract info if fulfill condition required

Input file (4 DATA record shown in this case): DATA AA0110 ACCESSION AA0110 VERSION AA0110 GI:157412239 FEATURES Location/Qualifiers length 1..1170 1..1700 /length="1170" position ... (5 Replies)
Discussion started by: perl_beginner
5 Replies

6. Shell Programming and Scripting

Extract only required elements from XML.

Hi , I have an XML like this. <Request> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <version>v44</version><messageId>7247308192</messageId><timeToLive>72000000000</timeToLive> </Request>. I want to extract on version and messageId. As in my output... (13 Replies)
Discussion started by: chetan.c
13 Replies

7. Shell Programming and Scripting

Script required to extract a specific snippet from the entire file.

Hi, I have a file with the following structure. XXXXX........... YYYYY........... ................. .................. ZZZZZZ...... qwerty_start.............. .................. ................. .................. querty_end................ .............................. (1 Reply)
Discussion started by: abinash
1 Replies

8. UNIX for Dummies Questions & Answers

How get only required lines & delete the rest of the lines in file

Hiiii I have a file which contains huge data as a.dat: PDE 1990 1 9 18 51 28.90 24.7500 95.2800 118.0 6.1 0.0 BURMA event name: 010990D time shift: 7.3000 half duration: 5.0000 latitude: 24.4200 longitude: 94.9500 depth: 129.6000 Mrr: ... (7 Replies)
Discussion started by: reva
7 Replies

9. Shell Programming and Scripting

To extract the required lines from a file

I have a files in a directory in this format data data data ---BEGIN CERT----- data data data ---END CERT ----- Now, I want to extract the lines starting from --BEGIN CERT-- and write the contents till the end of file into a new file.How can I do this for all the files in the... (1 Reply)
Discussion started by: sureshg
1 Replies

10. UNIX for Dummies Questions & Answers

grep required pattern and next 2 or 3 lines

dear ones pl.kindly help me 1) how to print(grep) required pattern and following 2 or 3 lines. 2) grep required pattern(to print)+above 2 lines+below 2 or 3 lines.from a report file. ex: we have some report file kf askfjsk fksaj fk skf sjfksjd kff sjfkjs kf jskdjfklsd jfklsdf sdkfjsd fsd... (3 Replies)
Discussion started by: cvvsnm
3 Replies
Login or Register to Ask a Question