awk - If field value of consecutive records are the identical print portion of lines


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk - If field value of consecutive records are the identical print portion of lines
# 1  
Old 03-16-2018
awk - If field value of consecutive records are the identical print portion of lines

I have some data that looks like this:

Code:
PXD= ZW< 1,6
QR> QRJ== 1,2(5)
QR> QRJ== 4,1(2)
QR> QRJ== 4,2
QRB= QRB 4,2
QWM QWM 6,2
R<C ZW< 11,2
R<H= R<J= 6,1
R>H XZJ= 1,2(2)
R>H XZJ= 2,6(2)
R>H XZJ= 4,1(2)
R>H XZJ= 6,2
RDP RDP 1,2

What I would like to do is if fields $1 and $2 are identical in consecutive records, I would like to print the values of field $3 in those lines, separated by semi-colons. Thus the desired output would look like this:

Code:
PXD= ZW< 1,6
QR> QRJ== 1,2(5); 4,1(2); 4,2
QRB= QRB 4,2
QWM QWM 6,2
R<C ZW< 11,2
R<H= R<J= 6,1
R>H XZJ= 1,2(2); 2,6(2); 4,1(2); 6,2
RDP RDP 1,2

My desire would be to accomplish this with awk, but am open to have this work with other rudimentary unix/linux programs (e.g., uniq, paste, tr, et al).

I can find duplicates easy enough, it is the writing to desired output that is stumping me. I have tried numerous attempts with post-incremented arrays, but nothing I'm doing seems to work and I'm so new to this that I'm hoping for an example from which I can inductively learning from.

Thank you so much in advance.
# 2  
Old 03-16-2018
Hi, there are many examples on these forums. Here is one way you could try:
Code:
awk '{n=$1 OFS $2} n!=p{if(p) print p, r; r=$3; p=n; next}{r=r "; " $3} END{if(p) print p,r}' file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 03-17-2018
Scrutinizer, this is just some brilliant stuff here! Thank you so much.

If I can impose upon you a bit more would you be willing to either point me in the direction of some of the other examples you mention or even offer a few bullet points of explanation of this code you've offered? In particular, I'm a bit confused on the were you define n!=p when p does not seem to have been defined yet (this goes for r as well).
# 4  
Old 03-17-2018
Hello jvoot,

Could you please try following and let me know if this helps you.
Code:
awk 'prev && prev!=($1$2){print val;val=""} {val=val?val OFS $3:$0;prev=$1$2} END{print val}' OFS=";"   Input_file


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 03-17-2018
Quote:
Originally Posted by jvoot
Scrutinizer, this is just some brilliant stuff here! Thank you so much.

If I can impose upon you a bit more would you be willing to either point me in the direction of some of the other examples you mention or even offer a few bullet points of explanation of this code you've offered?
As a starting point, look at the threads at the end of this page in the box with the heading "More UNIX and Linux Forum Topics You Might Find Helpful".

Quote:
In particular, I'm a bit confused on the were you define n!=p when p does not seem to have been defined yet (this goes for r as well).
In awk, if a variable is used before any value is assigned to it, the value of that variable will be an empty string if it is used as a string or it will be 0 if it is used as a number.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 03-17-2018
Thanks Don. Unfortunately I noticed those suggested threads after I had responded to Scrutinizer. I really appreciate your own explanation here too as I continue to approach awk from a discipline far removed from computer science.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print lines based on text in field and value in two additional fields

In the awk below I am trying to print the entire line, along with the header row, if $2 is SNV or MNV or INDEL. If that condition is met or is true, and $3 is less than or equal to 0.05, then in $7 the sub pattern :GMAF= is found and the value after the = sign is checked. If that value is less than... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

Combine identical lines and average the one variable field

I have the following file 299899 chrX_299716_300082 196 78.2903 299991 chrX_299982_300000 18.2538 Tajd:0.745591 FayWu:-0.245701 T2:1.45 299899 chrX_299716_300082 196 78.2903 299991 chrX_299982_300000 18.2538 Tajd:0.745591 FayWu:-0.245701 T2:0.283 311027 chrX_310892_311162 300 91.6452... (2 Replies)
Discussion started by: jfern
2 Replies

3. Shell Programming and Scripting

Awk: print lines with one of multiple pattern in the same field (column)

Hi all, I am new to using awk and am quickly discovering what a powerful pattern-recognition tool it is. However, I have what seems like a fairly basic task that I just can't figure out how to perform in one line. I want awk to find and print all the lines in which one of multiple patterns (e.g.... (8 Replies)
Discussion started by: elgo4
8 Replies

4. Shell Programming and Scripting

awk print values between consecutive lines

I have a file in below format: file01.txt TERM TERM TERM ABC 12315 68.53 12042013 165144 ABC 12315 62.12 12042013 165145 ABC 12315 122.36 12052013 165146 ABC 12315 582.18 12052013 165147 ABC 12316 2.36 12052013 165141 ABC 12316 ... (8 Replies)
Discussion started by: alex2005
8 Replies

5. Shell Programming and Scripting

sed print all lines between second and third identical lines

I am trying to extract a table of data (mysql query output) from a log file. I need to print everything below the header and not past the end of the table. I have spent many hours searching with little progress. I am matching the regexp +-\{99\} with no problem. I just can't figure out how to print... (5 Replies)
Discussion started by: godfreydanials
5 Replies

6. Shell Programming and Scripting

Scan a file in realtime and execute certain commands on encountering 5 consecutive identical lines

Mysql log has something like below: I need a bash shell script that will do the following: 1) The script will scan the mysql.log file constantly in real time (something like tail -F mysql.log) 2) If it encounters 5 consecutive identical lines then it would invoke some commands (say... (4 Replies)
Discussion started by: proactiveaditya
4 Replies

7. Shell Programming and Scripting

Using awk, print all the lines where field 8 is equal to x

Using awk, print all the lines where field 8 is equal to x I really did try, but this awk thing is really hard to figure out. file1.txt"Georgia","Atlanta","2011-11-02","x","","","","" "California","Los Angeles","2011-11-03","x","","","",""... (2 Replies)
Discussion started by: charles33
2 Replies

8. Shell Programming and Scripting

AWK: combining consecutive values in a field

Hi, Here is my sample input X 2 AAA Y 3 BBB Y 2 CCC Z 4 DDD In field 1, if the value of one line is same as that of next line, I want to concatenate the corresponding value of the second line in the third field with the value of the third field of first line. And I dont need the third... (2 Replies)
Discussion started by: polsum
2 Replies

9. Shell Programming and Scripting

Compare Tab Separated Field with AWK to all and print lines of unique fields.

Hi. I have a tab separated file that has a couple nearly identical lines. When doing: sort file | uniq > file.new It passes through the nearly identical lines because, well, they still are unique. a) I want to look only at field x for uniqueness and if the content in field x is the... (1 Reply)
Discussion started by: rocket_dog
1 Replies

10. Shell Programming and Scripting

finding and removing 2 identical consecutive words in a text

i want to write a shell script that correct a text file.for example if i have the input file: "john has has 2 apples anne has 3 oranges oranges" i want that the output file be like this: "john has 2 apples anne has 3 oranges" i've tried to read line by line from input text file into array... (11 Replies)
Discussion started by: cocostaec
11 Replies
Login or Register to Ask a Question