0

I wanna check if 2 lines have equal $4 my shell must check if the diffrence between $3 in line 2 and $2 in line 3 is equal 1 it must concatenate both line into one like this

1;1000000000;1099999999;2
2;1100000000;1199999999;3
3;1200000000;1200004444;3

Output

1;1000000000;1099999999;2
2;1100000000;1200004444;3

I dont know even to use awk or something else

3
  • Can you explain better what you want? This is totally unclear: "I wanna check if 2 lines have equal $4 my shell must check..." Dec 10, 2013 at 8:04
  • if there exist any 2 lines in my file having equal $4 (for example 3 in the above file line 2 and 3 have equal values ) I need to Concatenate them to something like the output
    – Mina
    Dec 10, 2013 at 8:21
  • What about doing simply sort -t';' -k4 -u file?
    – user54813
    Dec 10, 2013 at 8:41

1 Answer 1

3

Perl to the rescue:

perl -F';' -lane '
    if ($L[2] + 1 == $F[1] and $L[3] == $F[3]) {
        $L[2] = $F[2]
    } else {
        print join ";", @L if @L;
        @L = @F
    }
    END { print join ";", @L }
' file.csv
2
  • Please if i want to add another field at the beggining this filed is i which represent index and increments every line ??
    – Mina
    Dec 10, 2013 at 10:17
  • @Mina: ++$i increments the variable $i and returns its new value. Insert it as the second parameter to each print join.
    – choroba
    Dec 10, 2013 at 14:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .