I have created simple script:
#!/bin/sh
column=${1:-1}
awk '{colawk='$column'+1; print colawk}'
But when I run:
ls -la | ./Column.sh 4
I receive output:
5
5
5
5
But I have expected receive 5th column. Why this error?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I believe this will do what you’ve attempted in your example:
However, I don’t see why you’re adding one to the column index? You’ll then not be able to intuitively access the first column.
I’d to it this way instead:
The argument to
./Column.shwill be the column number you want,0will give you all columns, while a call without arguments will default the column index to1.In that case, how about:
Or, simply:
Two things I changed in your script:
BEGIN{}block since it only needs to be done once and not repeated for every input line.print $colawk” instead of “print colawk” so we’re printing the column indexed bycolawkinstead of its value.