suprimir la expansión variable en el comando awk

I want to get some specific lines from a file a.txt, code goes like this:

cat a.txt | awk '{if($0 > 100) print}'

y funciona bien

BUT, for some reason, I have to put the above command into a variable like this:

cmd="cat a.txt | awk '{if($0 > 100) print}'"

and I got a problem made $0, because it'll expand to the first parameter of the script, far from what it actually should be.

How could I resolve it?

preguntado el 05 de mayo de 13 a las 15:05

Más bien que cat a.txt | awk ..., solo haz awk '$0 > 100' a.txt -

Why do you want the command in a string? It just makes life very difficult, as you've just demonstrated. -

There is a bug in some part of the script you aren't showing us. Most likely a mismatched single quote character so the opening quote on the awk command is being treated as the closing quote from that earlier unmatched quote. -

@WilliamPursell, absolutely I knew that I it should be like awk '$0 >100' a.txt, I just improvised the stupid code to illustrate what my problem resembles. -

@JonathanLeffler, the truth is I should put that command in a hadoop trabajos mapperdel ADN, tales como los -mapper "*** | awk '{if($0 > 100) print}'" , ¿ver? -

1 Respuestas

Just add backslashes before the $ hasta que funcione.

contestado el 05 de mayo de 13 a las 15:05

Rather than adding backslashes "until it works", it might be a good idea to think about why the backslashes are necessary. If the string contains multiple special characters, exhausting over the permutations of added escape characters will be tedious and fragile! - Guillermo Pursell

No!!! This is absolutely the wrong thing to do. There is a bug elsewhere in your script that's causing the behavior you're seeing at this line and sticking random characters in various locations to try to get the error message to go away is absolutely going to come back and bite you down the road. - Ed Morton

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.