Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

share|improve this question
    
Can you explain better what you want? This is totally unclear: "I wanna check if 2 lines have equal $4 my shell must check..." –  Radu Rădeanu Dec 10 '13 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 '13 at 8:21
    
What about doing simply sort -t';' -k4 -u file? –  Herman Torjussen Dec 10 '13 at 8:41

1 Answer 1

up vote 2 down vote accepted

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
share|improve this answer
    
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 '13 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 '13 at 14:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.