How to extract the required lines?


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract the required lines?
# 1  
MySQL How to extract the required lines?

Hi Techies,

I got struck in middle of a problem. I have the following data whose format is:
Code:
a 1
a 2
a 3
a 4
b 1
b 2
b 3
b 4
c 1
c 2
c 3
c 4

I wanted the output from this to be:
Code:
a 3 
a 4
b 3
b 4
c 3
c 4

there can be any number of "a", "b","c","d"....might extened further to "e","f".........so on.

I wanted the last N (given 2 in the sample shown) lines from each "a","b","c"........

i had no clue where to start.

Can AWK do the job required ?
I donno but trying to learn it.

If anyone could give any idea, it would of great help.

Thank You,
14341
# 2  
Hi,

Try this..........

Code:
grep "[34]" $filename

# 3  
Quote:
Originally Posted by pravin27
Hi,

Try this..........

Code:
grep "[34]" $filename

I have just given a sample there.
it is not 3,4 always.

the contents could be anything.

Thanks for your try.

Regards,
14341
# 4  
Code:
awk 'NR>1 && $1!=x{print a ORS b}{a=b;b=$0;x=$1}' file

# 5  
Another approach...using gawk,nawk or awk:-

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

SmilieSmilieSmilie
# 6  
Quote:
Originally Posted by danmero
Code:
awk 'NR>1 && $1!=x{print a ORS b}{a=b;b=$0;x=$1}' file

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.


Quote:
Originally Posted by ahmad.diab
Another approach...using gawk,nawk or awk:-

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

SmilieSmilieSmilie
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
# 7  
You can crack these kind of problems in a couple of minutes in perl,

Code:
my %hash;

while(<DATA>)  {
    # get only the first field ( a b c d )
    $firstfield = (split / /,$_)[0];

    # if already it arrived, move that to index 1, 
    if ( defined $hash{$firstfield}->[0] )  {
        $hash{$firstfield}->[1] = $hash{$firstfield}->[0];
    }    
    # get the current line to 0th index.
    $hash{$firstfield}->[0] = $_;
}

# print the two elements.
foreach ( keys %hash )  {
    print $hash{$_}->[0];
    print $hash{$_}->[1] if defined ( $hash{$_}->[1] );
}

__DATA__
a 1
a 2
a 3
a 4
b 1
b 2
b 3
b 4
c 1
c 2
c 3
c 4


Code:
perl t1.pl
c 4
c 3
a 4
a 3
b 4
b 3

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #856
Difficulty: Medium
React was created by Jordan Renke, a software engineer at Facebook.
True or False?

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

Featured Tech Videos