Urgent help pls.how to extract two lines having same starting number

 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support Urgent help pls.how to extract two lines having same starting number
# 8  
Old 03-18-2010
So it should print lines only if followed by the same starting sequence?
# 9  
Old 03-18-2010
Thanks for the reply. Ya you are wright like , it should check the following line is also =035 if so print the second column value, if followed by =245 should skip it.Please guide

Regards
uma
# 10  
Old 03-19-2010
This does what you want (I think...)

Code:
$ perl -0777 -nle 'print "\nBefore:\n$_\n\n"; s/^(.*)/\1\n/sg; s/=035\s+.*?\n=245.*?\n//g; print "Eliminate pattern:\n$_\n\n"; while (/=035\s+(.*)/g) {print "$1\n";}' data4.txt
Before:
=245 this is testing
=035 abc123
=245 this is testing1
=035 abc124
=245 this is testing2
=035 abc125
=035 abc126
=245 this is testing3
=035 abc127
=035 abc128
=035 abc129
=245 this is testing 4

Eliminate pattern:
=245 this is testing
=035 abc125
=035 abc127
=035 abc128


abc125
abc127
abc128


Last edited by drewk; 03-19-2010 at 12:59 AM.. Reason: Bug in first posting
# 11  
Old 03-19-2010
Code:
diff urfile <(uniq -f 2 urfile)
7d6
< =035 abc126
10,11d8
< =035 abc128
< =035 abc129

# 12  
Old 03-19-2010
Hope this will help.

Code:
awk '!/035/ {f=0;s="";}f{ print s} /035/ {f++; s=$NF; } ' file

Example:

Quote:
/home/nosadm>cat file
=245 this is testing
=035 abc123
=245 this is testing1
=035 abc124
=245 this is testing2
=035 abc125
=035 abc124
=035 abc120
=035 abc126
=245 this is testing3
=035 abc127
=035 abc128
=035 abc129
=245 this is testing 4
/home/nosadm>awk '!/035/ {f=0;s="";}f{ print s} /035/ {f++; s=$NF; } ' file
abc125
abc124
abc120
abc127
abc128
# 13  
Old 03-19-2010
Hi,Drewk thanks a lot , it works perfect, can u also help me to understand the codelogic.

Hi Dennis ,thanks when i run your code, its more or less correct but extracting few others which is followed by =245 , this is happening especially in the starting and in the end.

Hi rdcwayx, sorry diff is not working.
# 14  
Old 03-19-2010
Quote:
Originally Posted by umapearl
Hi,Drewk thanks a lot , it works perfect, can u also help me to understand the codelogic.

Hi Dennis ,thanks when i run your code, its more or less correct but extracting few others which is followed by =245 , this is happening especially in the starting and in the end.

Hi rdcwayx, sorry diff is not working.
Glad it worked. It is not that hard once you get the right regex pattern.

Here is a simpler, easier version to understand:

Code:
$ cat data4.txt
=245 this is testing
=035 abc123
=245 this is testing1
=035 abc124
=245 this is testing2
=035 abc125
=035 abc126
=245 this is testing3
=035 abc127
=035 abc128
=035 abc129
=245 this is testing 4

$ perl -0777 -nle 'while (/=035\s+(.*)\n(?!=245)/g) { print "$1\n"; }' data4.txt
abc125
abc127
abc128

So two things to note:

perl -0777 -nle 'while (/=035\s+(.*)\n(?!=245)/g) { print "$1\n"; }' data4.txt

1) The invocation of perl with -0777 means slurp the whole file. This means the entire file will be in memory since you are referring to multiple lines. You could write something that will read multiple lines, but that is more complex logic. Perl can handle very big files this way, but nonetheless, it may fail with really huge files...

2) Note the Regex of "/=035\s+(.*)\n(?!=245)/g" used in the while loop. Here are the details:

"=035\s+" matches the =035 then any number of non CR whitespace until anything that is not whitespace;
"(.*)" captures the remainder of the line, up to the \n;
"\n" matches the end of line;
"(?!=245)" is a 'zero-width negative lookahead assertion'. In plain English, that means 'don't match the last bit if the next bit is true;'
"g" means all of these patterns.

On the last post, I did it quickly which usually means more sloppy. The last one first printed the input, then deleted the pattern matching a line with =035.* followed by a line with =254.* -- then print the remaining =035 lines. I did it stepwise instead of one sweep...

I cannot overemphasize how easy this becomes if you use a regex tool.

Try this: Regex Powertoy (interactive regular expressions) or this RegExr: Online Regular Expression Testing Tool

Either one, you can just play with patterns on your sample text until it does what you expect. There >>can<< be some bugs, such as gskinner does not handle the negative lookahead or lookbehind assertions properly, but it sure beats scratching your head...

Cheers,
This User Gave Thanks to drewk For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies

2. Shell Programming and Scripting

Extract between lines starting with and

I have a combination of patterns to search. file.txt contains below: H2016-02-10 A74867712 I1556539758 Xjdflk534jfl W0000055722327732 W0000056029009389 A74867865 I1556536434 W0000055822970840 W0000055722325916 A74868015 I1556541270 C0000055928920421 E lines starting with A are... (5 Replies)
Discussion started by: chakrapani
5 Replies

3. UNIX for Dummies Questions & Answers

Extract lines starting with rs

hello! could u, please, help: i have a file that includes 6 columns space delimited 1 rs4477212 0 82154 0 T 1 rs6680825 0 91472 0 G 1 rs9326626 0 570178 0 T 1 rs12123356 0 724702 0 C I need to extract to a separate file lines... (5 Replies)
Discussion started by: kush
5 Replies

4. Shell Programming and Scripting

extract the lines by index number

Hi All, I want to extract the lines from file1 by using the index numbers from file2. In example, cat file1.txt 265 ABC 956 ... 698 DFA 456 ... 456 DDD 145 ... 125 DSG 154 ... 459 CGB 156 ... 490 ASF 456 ... 484 XFH 489 ... 679 hgt 481 ... 111 dfg 986 ... 356 vhn 444 ...... (7 Replies)
Discussion started by: senayasma
7 Replies

5. UNIX for Dummies Questions & Answers

Extract n number of lines from a file successively

Hello, I have a file with over 100,000 lines. I would like to be able extract 5000 lines at a time and give it as an input to another program. sed -n '1,5000p' <myfile> > myOut Similarly for 5001-10000 10001-15000 .... How can I do this in a loop? Thanks, Guss (5 Replies)
Discussion started by: Gussifinknottle
5 Replies

6. Shell Programming and Scripting

Awk to extract lines with a defined number of characters

This is my problem, my file (file A) contains the following information: Now, I would like to create a file (file B) containing only the lines with 10 or more characters but less than 20 with their corresponding ID: Then, I need to compare the entries and determine their frequency. Thus, I... (7 Replies)
Discussion started by: Xterra
7 Replies

7. Shell Programming and Scripting

awk if statement matching all lines starting w/ a number

Does awk have a syntax for a range of numbers or is this the best way? if ($1 >= 0 && $1 <= 9 ) (7 Replies)
Discussion started by: Arsenalman
7 Replies

8. Shell Programming and Scripting

extract the lines between specific line number from a text file

Hi I want to extract certain text between two line numbers like 23234234324 and 54446655567567 How do I do this with a simple sed or awk command? Thank you. ---------- Post updated at 06:16 PM ---------- Previous update was at 05:55 PM ---------- found it: sed -n '#1,#2p'... (1 Reply)
Discussion started by: return_user
1 Replies

9. Shell Programming and Scripting

How to print the number of lines from a file, the starting string should be passed`

Hi , I have file, which has the below content: line 100 a b c d line300 a s d f s line200 a s d a (3 Replies)
Discussion started by: little_wonder
3 Replies

10. UNIX for Dummies Questions & Answers

extract some specific text file urgent pls

i have a big text file . i want to create new file as extract some specific text from the big file i am using hp ux please help (2 Replies)
Discussion started by: reyazan
2 Replies
Login or Register to Ask a Question