Extract between lines starting with and


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract between lines starting with and
# 1  
Old 03-08-2016
Extract between lines starting with and

I have a combination of patterns to search.

file.txt contains below:
Code:
H2016-02-10
A74867712
I1556539758
Xjdflk534jfl
W0000055722327732
W0000056029009389
A74867865
I1556536434
W0000055822970840
W0000055722325916
A74868015
I1556541270
C0000055928920421
E

lines starting with A are start of a recordset till we reach the next line starting with A. so in this file, i have 3 recordsets.

now from each recordset, I want to print first 2 lines, if there is no line starting with X in the recordset.

for example: from this file, i need to print:
Code:
A74867865
I1556536434

but i dont need to print:
Code:
A74868015
I1556541270

because there is no line starting with W in this recordset.

Last edited by Scrutinizer; 03-08-2016 at 04:44 PM.. Reason: code tags
# 2  
Old 03-08-2016
Do not expect us to read your mind as your post is not making any sense to me so state what you need clearly...
# 3  
Old 03-08-2016
my requirement is:

file.txt can have lines starting with either A,I,X,W,C,P, ignoring header line starting with H at the beginning of the file
and footer line starting with E at the end of file.

lines starting with A are start of a recordset till we reach the next line starting with A.

in the example i have given, tehre are 3 recordsets.

Code:
A74867712
I1556539758
Xjdflk534jfl
W0000055722327732
W0000056029009389

Code:
A74867865
I1556536434
W0000055822970840
W0000055722325916

Code:
A74868015
I1556541270
C0000055928920421


I want to print first 2 lines of those recordsets, for which there is no X line, whenever there is a W line.

so in the above 3 recordsets i need to print only the 2nd one:
i.e.
Code:
A74867865
I1556536434
W0000055822970840
W0000055722325916

i am trying with awk.

Last edited by Scrutinizer; 03-08-2016 at 04:45 PM..
# 4  
Old 03-08-2016
Please use code tags as required by forum rules!

How about
Code:
awk '
(/^A/ || /^E/) && 
 W && !X        {print A
                 print B
                }
/^A/            {W = X = 0
                 A = $0
                 getline B
                }
/^W/            {W = 1
                }
/^X/            {X = 1
                }
' file
A74867865
I1556536434

# 5  
Old 03-08-2016
Another way, first putting empty lines between records:
Code:
awk '/^[AHE]/{print x}1' file | awk '/\nW/ && !/\nX/{print $1 ORS $2}' FS='\n' RS=

# 6  
Old 03-08-2016
@RudiC:
Thanks for that it works perfectly.
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. 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

3. 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

4. Shell Programming and Scripting

Extract words starting with a pattern from a file

Hi Guys.. I have a file and i want to extract all words that starts with a pattern 'ABC_' or 'ADF_' For example, ABC.txt ---- INSERT INTO ABC_DLKFJAL_FJKLD SELECT DISTINCT S,B,C FROM ADF_DKF_KDFJ_IERU8 A, ABC_LKDJFREUE9_FJKDF B WHERE A.FI=B.EI; COMMIT; Output : ABS_DLKFJAL_FJKLD,... (5 Replies)
Discussion started by: Pramod_009
5 Replies

5. Shell Programming and Scripting

Return lines except those with starting with #, A and *

OS: RHEL 5.8 I have a text file like below $ cat mylist.txt # Multiple entries starting June 1, 2013 # # #AGENT TOUK1TOY1 TOUK1GTF1 ROUK1MET1 TOUK1GTF TOUK2TOY1 TOUK2GTF EOUK2GTF1 WOUK1RTO TOUK1RTO1 GOUK2RTO KOUK2RTO1 (2 Replies)
Discussion started by: John K
2 Replies

6. Shell Programming and Scripting

Supressing lines starting with #

Ok, I should know how to do this... I want to run crontab -l and pipe it through sed so that only those lines that do not have a # as the first character show on the screen... I know Ive doen this before but its been a decade since I scripted anything Im working in AIX 7.1 using the crontab... (3 Replies)
Discussion started by: immagikman
3 Replies

7. 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

8. Shell Programming and Scripting

Remove all lines except lines starting with [

Hello, I am trying to remove all the lines in file except lines starting with [ How can i accomplish this? Thank you very much in advance. (4 Replies)
Discussion started by: maxo
4 Replies

9. Shell Programming and Scripting

Concatenate lines between lines starting with a specific pattern

Hi, I have a file such as: --- >contig00001 length=35524 numreads=2944 gACGCCGCGCGCCGCGGCCAGGGCTGGCCCA CAGGCCGCGCGGCGTCGGCTGGCTGAG >contig00002 length=4242 numreads=43423 ATGCCGAAGGTCCGCCTGGGGCTGG CGCCGGGAGCATGTAGCG --- I would like to concatenate the lines not starting with ">"... (9 Replies)
Discussion started by: s052866
9 Replies

10. Emergency UNIX and Linux Support

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

Hi , I have a huge file like this =245 this is testing =035 abc123 =245 this is testing1 =035 abc124 =245 this is testing2 =035 abc125 =035 abc126 =245 this is testing3 here i have to pull out those lines having two =035 instead of alternative 035 and 245 i.e extract... (18 Replies)
Discussion started by: umapearl
18 Replies
Login or Register to Ask a Question