1752325 Members
5369 Online
108786 Solutions
New Discussion юеВ

how get two lines ...

 
SOLVED
Go to solution
Manuales
Super Advisor

how get two lines ...

Hi all ..
this is the scenario .. i have a files whit this information

cat information.txt

uno- this is a cat
dos- this is a dog

uno- this is a cat
dos- this is a duck

uno- this is a cat
dos- this is a cat

uno- this is a dog
dos- this is a cat

uno- this is a duck
dos- this is a pig

uno- this is a pig
dos- this is a pig

I need to compare both lines toguehter.
if you see they are separated by a line, i need to see if the column 5 is the same or not for each two lines, how can i do that?

how can i get the lines uno- dos- after one line ?

i mean i need to get first:

uno- this is a cat
dos- this is a dog
how can i get them?
after i need to compare the column 5 , i know how to do that
then after a white line i need to get these lines ...
uno- this is a cat
dos- this is a duck

and go on ..
please let me know...
i think i can do it with awk, i do not know how exactly to use it to get the information as i need it ..

thanks in advace.
Manuales.
25 REPLIES 25
James R. Ferguson
Acclaimed Contributor
Solution

Re: how get two lines ...

Hi:

# awk 'NF==0 {next};{getline LINE;split(LINE,A);if ($5==A[5]) {print $0"\n"LINE}}' file
uno- this is a cat
dos- this is a cat
uno- this is a pig
dos- this is a pig

Regards!

...JRF...
Manuales
Super Advisor

Re: how get two lines ...

thanks James

how can i stop the first two lines to get the value of the column 3 ... and then stop for the other 2 and go on ..?
James R. Ferguson
Acclaimed Contributor

Re: how get two lines ...

Hi (again) Manuales:

> how can i stop the first two lines to get the value of the column 3 ... and then stop for the other 2 and go on ..?

I'm not sure I understand what you are asking. If you want the third field and not the fifth, the logic would look like:

# awk 'NF==0 {next};{getline LINE;split(LINE,A);if ($3==A[3]) {print $0"\n"LINE}}'

This code skips blank lines (i.e those that have no fields: NF==0). Otherwise, we have the first line we want in '$0'. The next LINE is fetched with 'getline' into the 'LINE' variable. Since 'getline' doesn't do auto-field-spliiting, we do it with a 'split' ourselves.

Regards!

..JRF...
Manuales
Super Advisor

Re: how get two lines ...

i mean

for .....
what you told me:
awk 'NF==0 {next};{getline LINE;split(LINE,A);if ($5==A[5]) {print $0"\n"LINE}}' file.txt

a=column 5 from uno-
b=column 5 form dos_

if [[ $a = $b ]]
then
echo $ > output.txt
fi


done
James R. Ferguson
Acclaimed Contributor

Re: how get two lines ...

Hi (again) Manuales:

> what you told me:

I don't understand what you are asking. Did you try running the code I suggested using *your* input as posted; and did it produce the output you wanted?

Regards!

...JRF...
Manuales
Super Advisor

Re: how get two lines ...

ok i will explain better

once i have got the first 2 lines the script has to stop to get the value of the column 5 of the line where appears "uno_" and keep it in a variable a, then the script will get the
line where appears "dos_" , will keep the value in the variable b, then a and b will be compared, if they are similar $a will be send to an output file ....

then i need to do the same to the other two lines and go on ....


until the script has red all the file ..
Manuales
Super Advisor

Re: how get two lines ...

yes, i ran the code you gave me and worked i got all the lines i want ..... but i need to code in order to stop each two lines uno_ and dos_ and do what above i wrotte ... ow can i do that, how can i stop each two lines for being evaluated?
Manuales
Super Advisor

Re: how get two lines ...

i think i have to use something like this ..


awk 'BEGIN { }
{
...................
..................

a=`echo $line that contains the word uno_ | cut -d" " -f`

b=`echo $line that contains the word dos_ | cut -d" " -f`

if [[ $a = $b ]]
then
echo $a > output.txt
fi
...................
..................



}
}' file

it is a loop of the code you gave me for each two lines uno_ and dos_

space

the the others lines uno_ and dos_

and go on ...

how can i do that?
James R. Ferguson
Acclaimed Contributor

Re: how get two lines ...

Hi Manuales:

> once i have got the first 2 lines the script has to stop to get the value of the column 5 of the line where appears "uno_" and keep it in a variable a, then the script will get the
line where appears "dos_" , will keep the value in the variable b, then a and b will be compared, if they are similar $a will be send to an output file ....

Look at the 'awk' script I suggested. You said in your opening comments, "I think I can do it with awk...".

The script I have offered skips blank lines; reads two lines; and "stops" at that point if you want to think of it that way. The 5th field of one record is compared for equality to the 5th field of the second record just as you have asked. In the case of the 'awk' records, '$5' is one variable and 'a[5]' is the other.

Regards!

...JRF...