Extract lines in awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extract lines in awk
# 1  
Old 04-23-2013
Extract lines in awk

Hi,
I have one file of the following format:

Code:
TBCD, 1521, 14585236, NSDFC
XSDF, 1845, 14525426, SDFFF
SDFC, 4524, 14523655, SDNCV
ASBC, 1845, 48754251, SDFFC
ASBC, 1845, 54542512, SDFFF
ASBC, 1845, 34212512, NSDFC
ASBC, 1845, 16890234, ASFCH
MNDG, 1896, 15842642, SFTDD
SDFC, 8524, 14523655, SDRCV
ABCD, 1321, 14585236, NSDFC
RSDF, 1845, 56321452, JKSDX

I want to be able to select a range of lines based on a value that I will input.
First I want to select all the lines from the 1st one to the value that I have input (including). Bellow the following scripts works well but doesn't handle the duplicates:
Code:
awk -v st="ASBC, 1845," '!p;index ($0, st) {p=1}' test.txt

The result is:

Code:
TBCD, 1521, 14585236, NSDFC
XSDF, 1845, 14525426, SDFFF
SDFC, 4524, 14523655, SDNCV
ASBC, 1845, 48754251, SDFFC

Desired result should be:
Code:
TBCD, 1521, 14585236, NSDFC
XSDF, 1845, 14525426, SDFFF
SDFC, 4524, 14523655, SDNCV
ASBC, 1845, 48754251, SDFFC
ASBC, 1845, 54542512, SDFFF
ASBC, 1845, 34212512, NSDFC
ASBC, 1845, 16890234, ASFCH

In the same way I want to be able to select the lines after the value I input (not including):
Code:
awk -v st="ASBC, 1845," 'index ($0, st) {show[NR+1]++} show[NR]  {p=1}p' test.txt

Result

Code:
ASBC, 1845, 54542512, SDFFF
ASBC, 1845, 34212512, NSDFC
ASBC, 1845, 16890234, ASFCH
MNDG, 1896, 15842642, SFTDD
SDFC, 8524, 14523655, SDRCV
ABCD, 1321, 14585236, NSDFC
RSDF, 1845, 56321452, JKSDX

Desired result:

Code:
MNDG, 1896, 15842642, SFTDD
SDFC, 8524, 14523655, SDRCV
ABCD, 1321, 14585236, NSDFC
RSDF, 1845, 56321452, JKSDX

Please let me know how can I get the desired results using similar commands in awk.
Thanks in advance

Last edited by alex2005; 04-23-2013 at 05:04 PM..
# 2  
Old 04-23-2013
To find the last occurrence of something in an unsorted data file you have two choices.

The first requires two passes:
1. read the whole file, because you cannot know whether the thing you want still exists further down in the file, remember the line number
2. start printing lines from the start until you hit the line you remembered.

The second requires one pass. Read the file backwards starting with the last record, as soon as you hit the string , start printing.
This reverses the order of the data.

What you are doing will only work for sorted data. Can you sort the file before you run your awk? Otherwise you have to write a lot of code.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 04-23-2013
Hi,
First of all thank you for your reply.
I've made a mistake in the input file not copying all the lines.
Please have a look on the modified file and let me know if we'll still need to sort (which I would like to avoid).
Many thanks
# 4  
Old 04-23-2013
For 1st condition:
Code:
awk -v st="ASBC, 1845," 'BEGIN{p=1}{n=match($0,st);if(n) p=0}n||p' file

For 2nd condition:
Code:
awk -v st="ASBC, 1845," 'BEGIN{p=0}{n=match($0,st);if(n) p=1}!n&&p' file

This User Gave Thanks to Yoda For This Post:
# 5  
Old 04-23-2013
If the keys are grouped (not necessarily sorted, but here are no gaps), you could try:
Code:
awk -v st="ASBC, 1845," 'NR==1||$0~st, $0~st' file

for the 1st and
Code:
awk -v st="ASBC, 1845," 'NR==1||$0~st, $0~st{next}1'  file

for the 2nd
This User Gave Thanks to Scrutinizer For This Post:
 
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. UNIX for Dummies Questions & Answers

awk - Extract 4 lines in Column to Rows Tab Delimited between tags

I have tried the following to no avail. xargs -n8 < test.txt awk '{if(NR%6!=0){p=""}else{p="\n"};printf $0" "p}' Mod_Alm_log.txt > test.txt I have tried different variations of the above, the problem is mixes lines together. And it includes the tags "%a and %A" I need them to be all tab... (16 Replies)
Discussion started by: mytouchsr
16 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

awk? extract quoted "" strings from multiple lines.

I am trying to extract multiple strings from snmp-mib files like below. ----- $ cat IF-MIB.mib <snip> linkDown NOTIFICATION-TYPE OBJECTS { ifIndex, ifAdminStatus, ifOperStatus } STATUS current DESCRIPTION "A linkDown trap signifies that the SNMP entity, acting in... (5 Replies)
Discussion started by: genzo
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

Need to extract some lines from output via AWK

Hello Friends, I have got, this output below and i want to extract the name of symlink which is highlighted in red and the path above it highlighted in blue. At the end i want to append path and symlink. /var/tmp/asirohi/jdk/jre /var/tmp/asirohi/jdk/jre/.systemPrefs... (3 Replies)
Discussion started by: asirohi
3 Replies

8. Shell Programming and Scripting

AWK: How to extract text lines between two strings

Hi. I have a text test1.txt file like:Receipt Line1 Line2 Line3 End Receipt Line4 Line5 Line6 Canceled Receipt Line7 Line8 Line9 End (9 Replies)
Discussion started by: TQ3
9 Replies

9. Shell Programming and Scripting

Need awk to extract lines and sort

Hi, My data looks like this. CHR SNP BP A1 TEST NMISS OR STAT P 0 SNP_A-8282315 0 2 ADD 1530 1.074 0.7707 0.4409 0 SNP_A-8282315 0 2... (11 Replies)
Discussion started by: genehunter
11 Replies

10. Shell Programming and Scripting

extract the lines

Hi, I have a text file with 15 columns and i want to extract those lines of which 7th column is ABCD. I think we can do this using awk but could not frame the command. Please help. TIA Prvn (2 Replies)
Discussion started by: prvnrk
2 Replies
Login or Register to Ask a Question