I am trying to dynamically print selected fields of a file using awk.
Basically, the question is why this one works:
awk -F';' '{print $3 ";" $4 ";" }' file
but this does not:
awk -F';' '{print '`echo '$3 ";" $4 ";"'`' }' file
My final goal is to encapsulate the above in one command so that I would be able to do
my_cmd ';' 3 6 7 8 file(s)
which should display the fields #3,6,7,8 delimited by ; from file(s).
Editing my post: Found out myself that my problem was echo was inserting of course a new line caracter which was causing issue for awk :o) \c did the trick, also some \" escaping had to do (see below).
awk -F';' '{print '"`echo '$3 \";\" $4 \";\"\c'`"' }' file(s)
Now I'm only left to change it with command which will generate dinamically a string like
$2 ";' $5 ";' $6 ";' $9 ";" (number and fields should be the intput) etc. which should go between '{print ' and ' }'
Thanks to cbuckley I found out my one line command: (Issue solved).
cut -d"$1" -f `shift; echo $* | sed 's/[^ 0-9]\{1,\}.*$//;s/[ ]$//;s/[ ]\{1,\}/,/g'` `shift; printf "%s\n" $(echo $*) | grep -v '^[0-9]$'`
here $* are the input parameters and if above would be as an alias or a function in your .rc file named say filterc then the synopsys would be:
filterc delimiter column1 [column2 [column3...]] file1 [file2 [file3...]]
where:
delimiter - one char only
column1..n - a number representing the column
file1..n - files to be filtered, assumption here that files will not have names from numbers only and that all are of same format.
echoand reverse tilde has no meaning inside awk. You should better explain what you're trying to do?awkyou need to use the-vparameter. See stackoverflow.com/questions/6373379/using-awk-with-variablesecho '{print ...}'(bloody syntax highlight doesn't let me write the full code)echo '$3 \";\" $4 \";\"\c'"' }'