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

Can I insert tab between printed fields in AWK?

4,304 views
Skip to first unread message

PokeChoppe

unread,
Aug 26, 1998, 3:00:00 AM8/26/98
to
How do I insert a tab between two print fields in awk?

I know that by typing awk '{print $1, $2}' testfile will insert ONE tiny space
between the $1 and the $2 fields. However, I need more space between the $1
and the $2 than the above program is giving. I've even tried:

awk '{print $1, $2}' testfile and Unix still prints ONE space
between.

What am I doing wrong? Can a tab be inserted?

Thanks in advance.

DFR

unread,
Aug 26, 1998, 3:00:00 AM8/26/98
to
awk '{
print "test \t test"
}'

--
Views expressed are personal and not necessarily shared by my employer.

Martin Smith

unread,
Aug 26, 1998, 3:00:00 AM8/26/98
to
PokeChoppe wrote:
>
> How do I insert a tab between two print fields in awk?
>
> I know that by typing awk '{print $1, $2}' testfile will insert ONE tiny space
> between the $1 and the $2 fields. However, I need more space between the $1
> and the $2 than the above program is giving. I've even tried:
>
> awk '{print $1, $2}' testfile and Unix still prints ONE space
> between.
>
> What am I doing wrong? Can a tab be inserted?
>
> Thanks in advance.

You could set the output field separator to TAB.

awk '
BEGIN {OFS="\t"}
{print $1,$2}
' testfile

but this would mean all fields would be TAB separated.

regards
Martin

Chris Thompson

unread,
Aug 26, 1998, 3:00:00 AM8/26/98
to
In article <35E421...@hotmail.com>,

Try

awk '{print $1 "\t" $2}' testfile

You don't have to use a list: use the concatenation "operator" instead!

Chris Thompson
Email: cet1 [at] cam.ac.uk

John Milone

unread,
Sep 23, 1998, 3:00:00 AM9/23/98
to
PokeChoppe wrote:

> How do I insert a tab between two print fields in awk?
>
> I know that by typing awk '{print $1, $2}' testfile will insert ONE tiny space
> between the $1 and the $2 fields. However, I need more space between the $1
> and the $2 than the above program is giving. I've even tried:
>
> awk '{print $1, $2}' testfile and Unix still prints ONE space
> between.
>
> What am I doing wrong? Can a tab be inserted?
>
> Thanks in advance.

Try this

awk '{printf("%s<tab>%s\n", $1,$2)}


DFRussell

unread,
Sep 23, 1998, 3:00:00 AM9/23/98
to

awk '{printf("%s\t%s\n", $1, $2)}'

Charles Demas

unread,
Sep 23, 1998, 3:00:00 AM9/23/98
to
In article <6uavfe$1vq4$1...@rtpnews.raleigh.ibm.com>,

DFRussell <d...@nospam.com> wrote:
>
>In article <3608F7EC...@derecho.sbi.com>,
>John Milone <jmi...@derecho.sbi.com> writes:
>|> PokeChoppe wrote:
>|>
>|> > How do I insert a tab between two print fields in awk?
>|> >
>|> > I know that by typing awk '{print $1, $2}' testfile will insert ONE tiny space
>|> > between the $1 and the $2 fields. However, I need more space between the $1
>|> > and the $2 than the above program is giving. I've even tried:
>|> >
>|> > awk '{print $1, $2}' testfile and Unix still prints ONE space
>|> > between.
>|> >
>|> > What am I doing wrong? Can a tab be inserted?
>|> >
>|> Try this
>|>
>|> awk '{printf("%s<tab>%s\n", $1,$2)}
>


Alternatively (and somewhat more simply, in this case):

awk '{print $1 "\t" $2}' testfile

note that there are no commas in this, this utilizes the string pasting
capability of awk.


Chuck Demas
Needham, Mass.

--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
de...@tiac.net | \___/ | http://www.tiac.net/users/demas

Christian Bauernfeind

unread,
Sep 23, 1998, 3:00:00 AM9/23/98
to
In article <6ub0hu$f...@news-central.tiac.net>,

de...@sunspot.tiac.net (Charles Demas) writes:
>
> Alternatively (and somewhat more simply, in this case):
>
> awk '{print $1 "\t" $2}' testfile
>
> note that there are no commas in this, this utilizes the string pasting
> capability of awk.
>


For completeness' sake:

awk '{OFS="\t"; print $1,$2}' testfile

Christian
--
Christian Bauernfeind
Not speaking for Siemens
Not even working for IBM
e-mail: v2ba...@fishkill.ibm.com

0 new messages