Script to compare lines in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to compare lines in a file
# 1  
Old 08-25-2014
Script to compare lines in a file

Need help to create the script that does the following : -
1. Compare the current line "column B and C" with next line "column B and C"
2. If they are the same, print output to a file

Input file
Code:
2014-08-25 04:45:56.673|T1|JO|Begin|10
2014-08-25 04:55:56.673|T1|JO|Begin|11
2014-08-25 05:14:04.565|T1|JOR|End
2014-08-25 05:14:36.313|T2|JO|Begin|20
2014-08-25 05:19:46.439|T2|JOR|End
2014-08-25 05:19:46.563|T3|MO|Begin|30
2014-08-25 05:20:37.746|T3|MOR|End

Output file
Code:
start_dt|end_dt|transaction|transaction_type|value
2014-08-25 04:55:56.673|2014-08-25 05:14:04.565|T1|JO|11
2014-08-25 05:14:36.313|2014-08-25 05:19:46.439|T2|JO|20
2014-08-25 05:19:46.563|2014-08-25 05:20:37.746|T3|MO|30


Last edited by Scrutinizer; 08-25-2014 at 06:19 AM.. Reason: CODE tags added
# 2  
Old 08-25-2014
Any attempts from your side?

BTW - wildly guessing I infer that column B and C are columns/fields 2 and 3. But your sample output does not match your logics applied to your input file.
# 3  
Old 08-25-2014
Quote:
Originally Posted by RudiC
Any attempts from your side?

BTW - wildly guessing I infer that column B and C are columns/fields 2 and 3. But your sample output does not match your logics applied to your input file.
Hi Rudic,

column B and C are referring to column 2 and 3.
if current line's column C = 'JO' and next line's column C='JOR' and current line's column B = next line's column B then write the output to the file

Code:
2014-08-25 04:55:56.673|T1|JO|Begin|11 --> start of the transaction
2014-08-25 05:14:04.565|T1|JOR|End       --> end of the transaction

if current line's column C = 'MO' and next line's column C='MOR' and current line's column B = next line's column B then write the output to the file


Code:
2014-08-25 05:19:46.563|T3|MO|Begin|30  --> start of the transaction
2014-08-25 05:20:37.746|T3|MOR|End        --> end of the transaction


Last edited by Don Cragun; 08-25-2014 at 11:10 PM.. Reason: Add CODE tags again.
# 4  
Old 08-25-2014
What have you tried so far? And, why didn't you select lines 1 and 2 from your input file, but lines 2 and 3 instaed?
# 5  
Old 08-25-2014
Quote:
Originally Posted by RudiC
What have you tried so far? And, why didn't you select lines 1 and 2 from your input file, but lines 2 and 3 instaed?
The input file is sorted by transaction (T1,T2,T3) and datetime.
There are 2 records tied to 1 transaction (start and end).
As there is no end transaction for the first line, it will not be written in the output file.

I am thinking of using awk, but not very sure how to write it.
This User Gave Thanks to chailee For This Post:
# 6  
Old 08-25-2014
Is this a homework assignment?

You said you wanted to print lines if B and C were the same. But you don't want to print:
Code:
2014-08-25 04:45:56.673|T1|JO|Begin|10
          and
2014-08-25 04:55:56.673|T1|JO|Begin|11

where T1 and JO match in both lines. Instead you want to combine and print:
Code:
2014-08-25 04:55:56.673|T1|JO|Begin|11
          and
2014-08-25 05:14:04.565|T1|JOR|End

where T1 matches, but JO does not match JOR.

Your sample seems to show that you're looking for lines with identical values in field 2 and a line where one field 4 starts with Begin and another line where field 4 starts with End. Does field 3 really matter? If so, how do we know in the general case whether values in field 3 match?

What OS and shell are you using?
# 7  
Old 08-26-2014
Quote:
Originally Posted by Don Cragun
Is this a homework assignment?

You said you wanted to print lines if B and C were the same. But you don't want to print:
Code:
2014-08-25 04:45:56.673|T1|JO|Begin|10
          and
2014-08-25 04:55:56.673|T1|JO|Begin|11

where T1 and JO match in both lines. Instead you want to combine and print:
Code:
2014-08-25 04:55:56.673|T1|JO|Begin|11
          and
2014-08-25 05:14:04.565|T1|JOR|End

where T1 matches, but JO does not match JOR.

Your sample seems to show that you're looking for lines with identical values in field 2 and a line where one field 4 starts with Begin and another line where field 4 starts with End. Does field 3 really matter? If so, how do we know in the general case whether values in field 3 match?

What OS and shell are you using?

sh script will be used.
Yes, you are right, can ignore field 3, we can use field 4 to match the Begin and End record.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to compare 2 files and create a result file with unmatched lines from first file.?

HI, I have 2 text files. file1 and file2. file1.txt (There are no duplicates in this file) 1234 3232 4343 3435 6564 6767 1213 file2.txt 1234,wq,wewe,qwqw 1234,as,dfdf,dfdf 4343,asas,sdds,dsds 6767,asas,fdfd,fdffd I need to search each number in file1.txt in file2.txt's 1st... (6 Replies)
Discussion started by: Little
6 Replies

2. Shell Programming and Scripting

How to compare two lines in a csv file using shell script?

I have a file lets say input.csv having two columns like- Name,Mobile No A,111 B,222 A,333 A,123 B,213 I would like to find result in a new file lets say output.csv as- Name,Mobile No A,111 B,222 means short the file on the basis of first column and first value corresponding to the... (5 Replies)
Discussion started by: Ashish Singhal
5 Replies

3. Shell Programming and Scripting

compare lines in a file

Hi Folks, I need to compare the cron's timings from a text file. Need to display how much time does it took for that job. For example i have the below txt file, I have cron1 started at 05:23:15 and completed at 05:25:57, now i need to find how much time did it took to complete corn1 job for... (8 Replies)
Discussion started by: Sendhil.Kumaran
8 Replies

4. Shell Programming and Scripting

Compare lines within a file

could someone write a short script which is able to take a text file input and compare lines 1 and 2, 3 and 4, 5 and 6 ... ... until the end of the file. And to prompt if the lines are not equal. Thank you! (10 Replies)
Discussion started by: c_lady
10 Replies

5. Shell Programming and Scripting

Trying to do a compare with multiple lines in a file

Hey guys I am having a problem with being able to find unused profiles in a configuration check script I am trying to create for accountability purposes for managing a large number of systems. What I am trying to do is run a script that will look at the raw config data in a file and pull all the... (3 Replies)
Discussion started by: scottzx7rr
3 Replies

6. Shell Programming and Scripting

compare values in different lines of file

Hi everybody, I have a file that looks like: A B C D -1 0 E F G H -2 0 I J K L +1 M N O P -6 I would like to compare $5 of every line. If both values are negative, I calculate a mean value and write the first line and delete the second one. If the two $5 values are different only... (6 Replies)
Discussion started by: s-layer
6 Replies

7. Shell Programming and Scripting

Script to compare lines

I need a simple script to parse a file with this data in it and have that script output a warning when CurrCapacity is more that 50% of MaxCapacity for a given pool Name, can anyone help ? CurrCapacity: 5 MaxCapacity: 20 Name: fhlwDSource CurrCapacity: 5 ... (3 Replies)
Discussion started by: init0
3 Replies

8. Shell Programming and Scripting

how to compare two lines using shell script?

how to compare two lines using shell script? (1 Reply)
Discussion started by: suman_dba1
1 Replies

9. Shell Programming and Scripting

How do I compare the last two lines of a file?

Hi, I want to compare the last two lines of a files, specifically characters 32 - 50 in both lines and generate an exit code if the range of characters do not match. Please advise. Thanks in advance! (2 Replies)
Discussion started by: limshady411
2 Replies

10. Shell Programming and Scripting

Help! How to compare two lines in a file

Hello, I am newcomer and sorry for this simple question. I want to how to compare two lines in a file? For example, to compare the first line and the second line of a file to know if they are same? Thanks in advance! Leon (3 Replies)
Discussion started by: sabertooth2000
3 Replies
Login or Register to Ask a Question