0

Here is my code.

I can give why I do this code. I try to take 1 file, take a line and guess if this line 1 have any "", if yes, I change to the second file and try to see if the line 1 have "" if no, I keep the data of file2/line1 and get in the final file.

But my question is: How doing an escape quote ?

#! /bin/bash
compteur="1"
ligne="2"
rm testfinal 2>/dev/null
touch testfinal 2>/dev/null
#########BOUCLE PERMUTATION LIGNE

while (( $ligne < "32" ))
do
        if [ 'cat test$compteur | sed -n $ligne\p | awk -F" "'{print $2}' ' == "*" ]
        then compteur=$((compteur+1));
        else
                   cat test$compteur | sed -n $ligne\p >> testfinal
                   ligne=$((ligne+1));
                   compteur=$((compteur=1));
fi
done

Edit: I find myself, the answers his: if [ "sed -n $ligne\p test$compteur | awk -F" " '{print $2}'" == "*" ]

  • The "solution" you mention in your edit will absolutely not work as you appear to intend. The string "sed [et cetera]" will never be equal to "*", the test will always fail, and the else clause will execute for literally every single case. – DopeGhoti Oct 25 '18 at 18:59
1

Your example command doesn't make any sense. You have no then or fi to go with your if, and you are incorrectly stuffing commands into a test (i. e. [) block.

Your code:

if [ 'cat test$compteur | sed -n $ligne\p | awk -F" " '{print $2}' ' == "*" ]

It looks like you're trying to compare the output of that chain of commands, and if the output is literally *, then do some undetermined thing? If so:

if [[ "$( cat test$computer | sed -n $ligne\p | awk -F' ' '{print $2}')" == "*" ]]; then
    do_something
fi

But this can be optimized a little bit; not least by getting rid of your "useless use of cat:

if [[ "$( sed -n $ligne\p test$computer | awk -F' ' '{print $2}')" == "*" ]]; then
    do_something
fi

I'm not sure about what you're doing with your sed command, but presuming that linge is a variable containing a line number to print, this could even be done just in awk:

if [[ "$( awk -F' ' -v ln=$linge 'NR==ln { print $2 }' )" == "*" test$computer ]]; then
    do_something
fi

Taking the full script you have submitted in your "answer", I have rewritten it with this changes and a couple of other small tweaks:

#!/bin/bash
compteur="1"
ligne="2"
> testfinal # clears contents of file in one step rather than rm; touch
#########BOUCLE PERMUTATION LIGNE

while [[ "$ligne" -lt 32 ]]; do
    if [[ "$( awk -F' ' -v ln=$ligne 'NR==ln { print $2 }' test$computer )" == "*" ]]; then
        compteur=$((compteur+1));
    else
        awk -v ln=$ligne 'NR==ln' >> testfinal
        ligne=$((ligne+1))
        compteur=$((compteur+1)); # I presume that the original 'compteur=1' was a typo.
    fi
done
0

Is it possible that you mix up single quotes ' and back ticks " `` " ? With back ticks (the old, deprecated "command substitution", equivalent to the new $(...)), your code line would make some sense, nonwithstanding GopeGhoti's improvement proposals.

  • No, I try but that's don't work :( – Clément Merger Oct 24 '18 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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