Awk: Comparing arguments with in line values of file and printing the result


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk: Comparing arguments with in line values of file and printing the result
# 1  
Old 06-23-2014
Awk: Comparing arguments with in line values of file and printing the result

I need to develop a script where I will take two date arguments as parameter date1 and date2 which will in format YYYYMM.

Below is the input file say sample.txt.

sample.txt will have certain blocks starting with P1.
Each block will have a value 118,1:TIMESTAMP.

I need to compare the arguments date1 and date2 with this TIMESTAMP and if it is between date1 and date2 then I want this entire block starting from P1 to start of next P1 to be copied in a new file say output.txt.

Since the input files will be very large I want to develop in awk and not shell.

Code:
P1
10,9:13/8613003774488
,10:1
,167:0
,487:5/E.164
11,9:15/460013750015717
,10:60
,167:0
118,1:20130629031147
,2:18000
P2
1,64:1
,70:H
,97:1

P1
10,9:13/8618653221121
,10:1
,167:0
,487:5/E.164
11,9:15/460013265655903
,10:60
,167:0
17,9:5/80001
,10:1
,11:1
,12:0
,167:0
,487:5/E.164
118,1:20140113064354
,2:18000
P2
1,64:1
,70:H
,97:1
2,1:20140113064354
,2:18000
P3
42,3:1.
,4:3
,300:1.
43,3:1.

---------- Post updated at 04:54 AM ---------- Previous update was at 04:52 AM ----------

For eg. if I run the script as,

Code:
./script 201305 201307

Output should be,

Code:
P1
10,9:13/8613003774488
,10:1
,167:0
,487:5/E.164
11,9:15/460013750015717
,10:60
,167:0
118,1:20130629031147
,2:18000
P2
1,64:1
,70:H
,97:1

# 2  
Old 06-23-2014
This one relies on the empty line block separator trailing the P1 blocks:
Code:
awk 'match($0, /118,1:/) {DAT=substr($0, RSTART+6, 6)} /^P1/ && DAT>T1 && DAT<T2' RS= T1=201305 T2=201307 file

# 3  
Old 06-23-2014
Quote:
Originally Posted by RudiC
This one relies on the empty line block separator trailing the P1 blocks:
Code:
awk 'match($0, /118,1:/) {DAT=substr($0, RSTART+6, 6)} /^P1/ && DAT>T1 && DAT<T2' RS= T1=201305 T2=201307 file

Thanks Rudic. It works. Can you explain a little about your awk line. Does it work block by block or line by line ?
Sorry for asking such a basic question but I am very new to awk.

Last edited by garvit184; 06-23-2014 at 08:15 AM..
# 4  
Old 06-23-2014
@RudiC: One possible catch might be that if a consecutive block does start with p1, but does not contain "118,1:", it will get printed as well.
# 5  
Old 06-23-2014
Point taken, but the spec says:
Quote:
Each block will have a value 118,1:TIMESTAMP.
Anyhow, wouldn't hurt to delete DAT after every line...
# 6  
Old 06-23-2014
Quote:
Originally Posted by RudiC
This one relies on the empty line block separator trailing the P1 blocks:
Code:
awk 'match($0, /118,1:/) {DAT=substr($0, RSTART+6, 6)} /^P1/ && DAT>T1 && DAT<T2' RS= T1=201305 T2=201307 file


@RudiC if we have multiple matches then I need a new line character before another P1 block is printed. The script doesn't takes care of it.
# 7  
Old 06-23-2014
Try adding ORS='\n\n', so: RS= ORS='\n\n' T1=201305 T2=201307 file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

Read file lines and pass line values as arguments.

Dears, Need help to implement below requirement A file (detail.txt)contain : 1st column: Stream 2nd column: PathAddress 3rd column: Counterlimit 4th column: TransactionDateColumn 5th column: DateType 6th column: SleepValue 7th column: Status Need to write a... (1 Reply)
Discussion started by: sadique.manzar
1 Replies

3. Shell Programming and Scripting

Comparing same column from two files, printing whole row with matching values

First I'd like to apologize if I opened a thread which is already open somewhere. I did a bit of searching but could quite find what I was looking for, so I will try to explaing what I need. I'm writing a script on our server, got to a point where I have two files with results. Example: File1... (6 Replies)
Discussion started by: mitabrev83
6 Replies

4. Shell Programming and Scripting

Printing $values using awk

Hi All I had requirement where I need to re-order columns in a file by using a control file. here is the ctrl file c1 c2 c3 source file c3 | c1 | c2 a | b| c I should create output file based on the ctrl file columns o/p should look like this c1 | c2 | c3 b| c|a I wrote some... (9 Replies)
Discussion started by: gvkumar25
9 Replies

5. Shell Programming and Scripting

Command line arguments with multiple values

how can I pass multiple values from command line arguments example script.sh -arg1 op1 -arg2 op1 op2 op3 (2 Replies)
Discussion started by: nsk
2 Replies

6. UNIX for Dummies Questions & Answers

Comparing two test files and printing out the values that do not match

Hi, I have two text files with matching first columns. Some of the values in the second column do not match. I want to write a script to print out the rows (only the first column) where the values in the second column do not match. Example: Input 1 A 1 B 2 C 3 D 4 Input 2 A 2 B 2... (6 Replies)
Discussion started by: evelibertine
6 Replies

7. Shell Programming and Scripting

Sed Comparing Parenthesized Values In Previous Line To Current Line

I am trying to delete lines in archived Apache httpd logs Each line has the pattern: <ip-address> - - <date-time> <document-request-URL> <http-response> <size-of-req'd-doc> <referring-document-URL> This pattern is shown in the example of 6 lines from the log in the code box below. These 6... (1 Reply)
Discussion started by: Proteomist
1 Replies

8. Programming

Reading command line arguments and setting up values if option not provided

I have a C++ program. I read command line arguments, but if the value is not supplied, I default or make a calculation. Let's say I set it to a default value. I can code this in several ways. Here I show three ways. What would be the best way for maintaining this code? The program will get very... (2 Replies)
Discussion started by: kristinu
2 Replies

9. UNIX for Dummies Questions & Answers

Comparing two text files by a column and printing values that do not match

I have two text files where the first three columns are exactly the same. I want to compare the fourth column of the text files and if the values are different, print that row into a new output file. How do I go about doing that? File 1: 100 rs3794811 0.01 0.3434 100 rs8066551 0.01... (8 Replies)
Discussion started by: evelibertine
8 Replies

10. Shell Programming and Scripting

awk printing: strange result

Dear all, I am using awk in a bash script to extract a list of x y z coordinates from a file such as: %BEGIN 3D-SPACE COORDINATES 0.2085627338147950 0.2471306816410478 0.2085627338147950 0.1242549179185660 0.2755539793525220 0.4147884486606120 0.2030669560265720 ... (6 Replies)
Discussion started by: pauli
6 Replies
Login or Register to Ask a Question