Skip to main content
improved formatting; tags
Source Link
chaos
  • 49.3k
  • 11
  • 127
  • 147

I have a tab delimited filefile:

qrs     John    tuv
abcd    Sam     efgh
ijk     Sam     lmnp
abcd    Sam     efgh
ijk     Sam     lmnp
qrs     John    tuv

I am trying to print the line in which the second field does not match the previous line's value in the second field and to print the line in which the second field does not match the next line.

I've been playing with variations of the following but nothing is quite working like I expect:

awk -F"\t" '{
    name=$3;
    line=$0;
    getline;
    newname=$3;
    newline=$0;
    getline;
    nextname=$3;
    nextline=$0; 
    if (newname != name || name != nextname)print line"\n"nextline }' input.txt

I have a tab delimited file

qrs     John    tuv
abcd    Sam     efgh
ijk     Sam     lmnp
abcd    Sam     efgh
ijk     Sam     lmnp
qrs     John    tuv

I am trying to print the line in which the second field does not match the previous line's value in the second field and to print the line in which the second field does not match the next line.

I've been playing with variations of the following but nothing is quite working like I expect

awk -F"\t" '{
    name=$3;
    line=$0;
    getline;
    newname=$3;
    newline=$0;
    getline;
    nextname=$3;
    nextline=$0; 
    if (newname != name || name != nextname)print line"\n"nextline }' input.txt

I have a tab delimited file:

qrs     John    tuv
abcd    Sam     efgh
ijk     Sam     lmnp
abcd    Sam     efgh
ijk     Sam     lmnp
qrs     John    tuv

I am trying to print the line in which the second field does not match the previous line's value in the second field and to print the line in which the second field does not match the next line.

I've been playing with variations of the following but nothing is quite working like I expect:

awk -F"\t" '{
    name=$3;
    line=$0;
    getline;
    newname=$3;
    newline=$0;
    getline;
    nextname=$3;
    nextline=$0; 
    if (newname != name || name != nextname)print line"\n"nextline }' input.txt
Source Link
rcjohnson
  • 929
  • 6
  • 12

Print the first and last match of a field with awk

I have a tab delimited file

qrs     John    tuv
abcd    Sam     efgh
ijk     Sam     lmnp
abcd    Sam     efgh
ijk     Sam     lmnp
qrs     John    tuv

I am trying to print the line in which the second field does not match the previous line's value in the second field and to print the line in which the second field does not match the next line.

I've been playing with variations of the following but nothing is quite working like I expect

awk -F"\t" '{
    name=$3;
    line=$0;
    getline;
    newname=$3;
    newline=$0;
    getline;
    nextname=$3;
    nextline=$0; 
    if (newname != name || name != nextname)print line"\n"nextline }' input.txt