I'm new to Shell scripting. I have a doubt in using the awk command.
I want to print columns from a specific column onwards
for e.g if the file consists the following
aa bb cc dd
ee ff gg hh
I want the output as follows
bb cc dd
ff gg hh
Any suggestion in this regard would be helpful
cut -d ' ' -f 2- FILE
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
> Hi,
>
> I'm new to Shell scripting. I have a doubt in using the awk command.
> I want to print columns from a specific column onwards
>
> for e.g if the file consists the following
>
> aa bb cc dd
> ee ff gg hh
>
> I want the output as follows
>
> bb cc dd
> ff gg hh
awk '{print $2, $3, $4}'
Thanks for your inputs. But this would just print till the 4th column.
If I'm uncertain of the number of the columns in the file but would
like to print everything from the second column then....
> If I'm uncertain of the number of the columns in the file but would
> like to print everything from the second column then....- Hide quoted text -
$ awk '{for(i=2;i<=NF;i++) printf " " $i;printf"\n"}' FILE
With a POSIX awk:
awk 'sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){1}/,"")'
Change "1" to however many leading fields you want to delete. With GNU
awk just add the "--re-interval" option.
Unlike the "for" loops already proposed, the above has the effect of
preserving white space between fields.
Ed.
ITYM:
awk '{sep=""; for(i=2;i<=NF;i++) {printf "%s%s",sep,$i} print ""}'
FILE
but that won't preserve white space as the OP seems to want.
Ed.
try this:
awk ' { $1=""; print; } ' infile.txt
awk '{ a=length($1); print substr($0,a+1)}' infile.txt
above would preserve the double spaces between 'ff' and 'gg'.....
--
Luuk
That would leave leading white space before the desired start field
and would fail if there was any leading white space before the first
field:
$ echo "aa bb cc" | awk '{ a=length($1); print substr($0,a+1)}'
bb cc
$ echo " aa bb cc" | awk '{ a=length($1); print substr($0,a+1)}'
a bb cc
Compare with:
$ echo "aa bb cc" | awk 'sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*)
{1}/,"")'
bb cc
$ echo " aa bb cc" | awk 'sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*)
{1}/,"")'
bb cc
Regards,
Ed.
In the OP's data the whitespace seems to be even different between input
and output. So I suspect the OP may have no interest in the exact amount
of whitespace.
Janis
>
> Ed.
ok, but it should have been:
echo "aa bb cc" | awk --posix
'sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){1}/,"")'
if used with Gnu AWK 3.1.5
--
Luuk
No, if I were to use GNU awk I'd have used:
echo "aa bb cc" | awk --re-interval 'sub(/^[[:space:]]*([^[:space:]]*
[[:space:]]*){1}/,"")'
since "--re-interval" enables RE-intervals just like "--posix" does
BUT "--posix" has the unfortunate side-effect of disabling many of
gawks more useful but non-POSIX features (e.g. gensub()) so it's best
avoided unless that's REALLY what you want, maybe for compatibility
testing.
Ed.
thanks, i harldy ever use the extra options on the command-line, if i
use awk...
--
Luuk