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

inserting 2 tab chars/spaces between field 1 and field2

27 views
Skip to first unread message

brumik

unread,
Dec 7, 2011, 7:53:39 AM12/7/11
to
Hello
I have this string:

SUNWzoner Solaris Zones (Root) 11.10.0,REV=2005.01.21.16.34
^^

I want to insert 2 tabs or a bunch of spaces between field1 and
field2, but leave the rest of the string intact. I have tried using
awk without success. Could someone help?

Ed Morton

unread,
Dec 7, 2011, 8:32:11 AM12/7/11
to
With GNU awk:

gawk '{ print
gensub(/([^[:space:]]+[[:space:]]+[^[:space:]]+)[[:space:]]+(.+)/,"\\1\t\t\\2","",$0)
}'

With other awks, consider a combination of match() and substr().

Ed.

brumik

unread,
Dec 7, 2011, 8:46:03 AM12/7/11
to
thanks, unfortunately I don't have gawk installed, are there any other
standard tools I could use to achieve this on Solaris? I have tried
sed with substitution as well but couldnt work out how to replace the
second delimiter (space) with multiple spaces.

Ed Morton

unread,
Dec 7, 2011, 9:03:29 AM12/7/11
to
sed 's/\([^[:space:]]*[[:space:]]*[^[:space:]]*\)[[:space:]]*\(.*\)/\1\t\t\2/'

Regards,

Ed.

Geoff Clare

unread,
Dec 9, 2011, 8:34:25 AM12/9/11
to
Ed Morton wrote:

> sed 's/\([^[:space:]]*[[:space:]]*[^[:space:]]*\)[[:space:]]*\(.*\)/\1\t\t\2/'

The use of \t in the replacement string is not portable. Use a real TAB
character if you want it to work on other versions of sed.

Also, since the 2nd subexpression is being replaced by itself, it doesn't
need to be there:

's/\([^[:space:]]*[[:space:]]*[^[:space:]]*\)[[:space:]]*/\1<TAB><TAB>/'

(Where <TAB> means press the TAB key, possibly preceded by CTRL-V if
typed into shell that does TAB completion.)

--
Geoff Clare <net...@gclare.org.uk>

Wayne

unread,
Dec 9, 2011, 11:27:54 AM12/9/11
to
$ str='SUNWzoner Solaris Zones (Root) 11.10.0,REV=2005.01.21.16.34'

$ echo $str |awk '{$1 = $1 "\t\t"; print}'
SUNWzoner Solaris Zones (Root) 11.10.0,REV=2005.01.21.16.34

Based on the location of your "^^", I think you meant between fields 2 and 3:

$ echo $str |awk '{$2 = $2 "\t\t"; print}'
SUNWzoner Solaris Zones (Root) 11.10.0,REV=2005.01.21.16.34

--
Wayne

David Combs

unread,
Dec 25, 2011, 11:45:00 PM12/25/11
to
In article <1cf9r8-...@leafnode-msgid.gclare.org.uk>,
Geoff Clare <ge...@clare.See-My-Signature.invalid> wrote:
>Ed Morton wrote:
>
>> sed 's/\([^[:space:]]*[[:space:]]*[^[:space:]]*\)[[:space:]]*\(.*\)/\1\t\t\2/'
>
>The use of \t in the replacement string is not portable. Use a real TAB
>character if you want it to work on other versions of sed.
>
Please explain, in some detail if you have time, the danger of
using \t in the replacement string -- as not being portable.

Does it not generate a tab char, eg a ^I. How that translates
to spaces depends on some env variable, eg usually 8 or maybe 4?

Isn't there some arg to the expand tool that says what to do
with a tab char?

Since many of us, I suppose, use \t all the time, some explicit
warnings would really be helpful.

ESPECIALLY along with some "war stories".

THANKS!



>Also, since the 2nd subexpression is being replaced by itself, it doesn't
>need to be there:
>
>'s/\([^[:space:]]*[[:space:]]*[^[:space:]]*\)[[:space:]]*/\1<TAB><TAB>/'
>
>(Where <TAB> means press the TAB key, possibly preceded by CTRL-V if
>typed into shell that does TAB completion.)
>
>--
>Geoff Clare <net...@gclare.org.uk>
>

David


Janis Papanagnou

unread,
Dec 26, 2011, 7:07:53 AM12/26/11
to
On 26.12.2011 05:45, David Combs wrote:
> In article <1cf9r8-...@leafnode-msgid.gclare.org.uk>,
> Geoff Clare <ge...@clare.See-My-Signature.invalid> wrote:
>> Ed Morton wrote:
>>
>>> sed 's/\([^[:space:]]*[[:space:]]*[^[:space:]]*\)[[:space:]]*\(.*\)/\1\t\t\2/'
>>
>> The use of \t in the replacement string is not portable. Use a real TAB
>> character if you want it to work on other versions of sed.
>>
> Please explain, in some detail if you have time, the danger of
> using \t in the replacement string -- as not being portable.

The "danger" is simply that you could not get what you expect; e.g.
two characters '\' 't', or an escaped [literal] 't', instead of the
TAB character.

>
> Does it not generate a tab char, eg a ^I. How that translates
> to spaces depends on some env variable, eg usually 8 or maybe 4?

The poster said just warned that "other versions" of sed may not
support what you expect. And that's completely unrelated with any
"expansion" to spaces or indent levels that other tools or displays
or printers may handle as they like.

>
> Isn't there some arg to the expand tool that says what to do
> with a tab char?

I am nor sure what you're asking here; WRT the above substitution
the statement was that a \t does not necessarily be translated to
a TAB character by some version of sed(1).

>
> Since many of us, I suppose, use \t all the time, some explicit
> warnings would really be helpful.

Whenever you use escaped characters you should know or check whether
the respective tools support it.

Janis

Geoff Clare

unread,
Dec 27, 2011, 12:20:59 PM12/27/11
to
David Combs wrote:

>>The use of \t in the replacement string is not portable. Use a real TAB
>>character if you want it to work on other versions of sed.
>>
> Please explain, in some detail if you have time, the danger of
> using \t in the replacement string -- as not being portable.

The POSIX description of the replacement string in a sed s command
includes the following:

The meaning of a <backslash> immediately followed by any character
other than '&' , <backslash>, a digit, or the delimiter character
used for this command, is unspecified.

Thus you cannot rely on \t working the way you expect - it might
behave differently on different implementations.

> Does it not generate a tab char, eg a ^I.

Yes, on some implementations. For example:

$ uname -sr
SunOS 5.10
$ echo x | sed 's/x/\t/'
t

$ uname -sr
HP-UX B.11.23
$ echo x | sed 's/x/\t/'
t

--
Geoff Clare <net...@gclare.org.uk>

Kenny McCormack

unread,
Dec 27, 2011, 2:01:25 PM12/27/11
to
In article <rcbps8-...@leafnode-msgid.gclare.org.uk>,
But on Linux (almost certainly the only system that OP cares about):

echo x | sed 's/x/\t/' | od -bc
0000000 011 012
\t \n
0000002

--
But the Bush apologists hope that you won't remember all that. And they
also have a theory, which I've been hearing more and more - namely,
that President Obama, though not yet in office or even elected, caused the
2008 slump. You see, people were worried in advance about his future
policies, and that's what caused the economy to tank. Seriously.

(Paul Krugman - Addicted to Bush)

Janis Papanagnou

unread,
Dec 27, 2011, 2:12:51 PM12/27/11
to
On 27.12.2011 20:01, Kenny McCormack wrote:
> In article <rcbps8-...@leafnode-msgid.gclare.org.uk>,
> Geoff Clare <ge...@clare.See-My-Signature.invalid> wrote:
>> [...]
>>
>> Thus you cannot rely on \t working the way you expect - it might
>> behave differently on different implementations.
>>
>> [...]
>
> But on Linux (almost certainly the only system that OP cares about):

Linux? - The OP said something about Solaris.

Janis

> [...]

Wayne

unread,
Jan 24, 2012, 12:29:00 AM1/24/12
to
Solaris 11 includes Gnu versions of utilities, so if it works for
Linux it will probably work on Solaris 11. (For older versions of
Solaris, Gnu versions of utilities are available.)

--
Wayne

Janis Papanagnou

unread,
Jan 24, 2012, 2:55:38 AM1/24/12
to
You said
"Linux (almost certainly the only system that OP cares about)".
which just ignores what the OP said.

And GNU utilities, whether pre-installed or not, are also not Linux.
Don't assume people can install what they like on the systems they
have to work on.

Janis

David Combs

unread,
Jan 28, 2012, 4:00:03 PM1/28/12
to
In article <jd9o1h$t4h$1...@news.m-online.net>,
Janis Papanagnou <janis_pa...@hotmail.com> wrote:
>
>Whenever you use escaped characters you should know or check whether
>the respective tools support it.
>
>Janis

Thank you. Will test first!

David

David Combs

unread,
Jan 28, 2012, 4:01:27 PM1/28/12
to
In article <rcbps8-...@leafnode-msgid.gclare.org.uk>,
Thanks much for the Posix quote -- and the examples!

David

David Combs

unread,
Jan 28, 2012, 4:07:29 PM1/28/12
to
In article <jflo5q$n3u$1...@news.m-online.net>,
Janis Papanagnou <janis_pa...@hotmail.com> wrote:
...
>
>And GNU utilities, whether pre-installed or not, are also not Linux.
>Don't assume people can install what they like on the systems they
>have to work on.
>
>Janis

Blow me over!

Linux doesn't use the GNU utilities, like ls, sed, ed, grep, etc?

I wonder why not.

David

Mirko K.

unread,
Jan 28, 2012, 6:49:31 PM1/28/12
to
Most Linux *distros* use the GNU utilities, but there's no
guarantee. There are distros who do not use them (for example
mini-distros which need to keep the system as small as possible).


Janis Papanagnou

unread,
Jan 29, 2012, 7:36:01 AM1/29/12
to
On 28.01.2012 22:07, David Combs wrote:
(You misunderstood; see the posting context that you stripped and the
OP's original posting.)

It's perfectly fine to assume GNU utilities in the context of Linux.
GNU utilities are the tools that are used in context of the Linux OS.
But you cannot assume that for the commercial Unixes - which was the
topic of the OP. The previous poster just ignored that.

The OP didn't speak of Linux (rather Solaris, IIRC); the previous poster
assumed Linux without need. The other point is that on non-Linux systems
you cannot assume that people are free to install the GNU utilities.

With Linux you get those GNU tools per default, with other (commercial)
Unixes you can neither assume they are there nor should you assume they
can be installed.

The other poster said:
"Linux (almost certainly the only system that OP cares about)".
which just ignores what the OP said.

Probably the previous poster meant to say "GNU Tools" instead of "Linux"
at some point, or maybe he meant "some Solaris version that delivers GNU
tools" instead of "Linux". We don't know. Doesn't matter either.

HTH.

Janis

>
> David
>

0 new messages