Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using Awk command

2 views
Skip to first unread message

Praveen

unread,
Mar 3, 2009, 6:06:03 PM3/3/09
to
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

Any suggestion in this regard would be helpful

Chris F.A. Johnson

unread,
Mar 3, 2009, 6:27:35 PM3/3/09
to
On 2009-03-03, Praveen wrote:
> 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

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

Maxwell Lol

unread,
Mar 3, 2009, 7:24:31 PM3/3/09
to
Praveen <rprave...@gmail.com> writes:

> 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}'

Praveen

unread,
Mar 3, 2009, 9:59:14 PM3/3/09
to
On Mar 3, 7:24 pm, Maxwell Lol <nos...@com.invalid> wrote:

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....

Harry

unread,
Mar 3, 2009, 11:40:19 PM3/3/09
to
On Mar 3, 6:59 pm, Praveen <rpraveen1...@gmail.com> wrote:

> 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

Ed Morton

unread,
Mar 4, 2009, 12:17:04 AM3/4/09
to

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.

Ed Morton

unread,
Mar 4, 2009, 12:23:33 AM3/4/09
to

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.

Michael Schindler

unread,
Mar 4, 2009, 9:30:02 AM3/4/09
to
Praveen wrote:

try this:

awk ' { $1=""; print; } ' infile.txt


Luuk

unread,
Mar 4, 2009, 1:53:40 PM3/4/09
to
Michael Schindler schreef:

awk '{ a=length($1); print substr($0,a+1)}' infile.txt

above would preserve the double spaces between 'ff' and 'gg'.....

--
Luuk

Ed Morton

unread,
Mar 4, 2009, 2:18:09 PM3/4/09
to

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.

Janis Papanagnou

unread,
Mar 4, 2009, 6:11:36 PM3/4/09
to

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.

Luuk

unread,
Mar 5, 2009, 1:33:44 PM3/5/09
to
Ed Morton schreef:

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

Ed Morton

unread,
Mar 5, 2009, 5:01:40 PM3/5/09
to

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.

Luuk

unread,
Mar 5, 2009, 5:25:09 PM3/5/09
to

thanks, i harldy ever use the extra options on the command-line, if i
use awk...

--
Luuk

0 new messages