Re: Help: How to add a character in the line
- From: Janis Papanagnou <Janis_Papanagnou@xxxxxxxxxxx>
- Date: Sun, 12 Aug 2007 20:08:16 +0200
Kenan Kalajdzic wrote:
Janis Papanagnou <Janis_Papanagnou@xxxxxxxxxxx> wrote:
Amy Lee wrote:
On Sun, 12 Aug 2007 13:59:43 +0200, Kenan Kalajdzic wrote:
Amy Lee <openlinuxsource@xxxxxxxxx> wrote:
Hi,
I'm a Linux user, I'd like to know how to use sed or other command to add
a character in the line. Like this:
Original line:
16jh_1010
Hope it to be:
16>jh_1010
Here is a simple example of how to solve your problem:
$ echo '16jh_1010' >file
$ cat file
16jh_1010
$ sed 's/^16/&>/' file
16>jh_1010
If you are serious about using 'sed', you should learn as much as
possible about regular expressions and the syntax of 'sed'. There
are several good tutorials on the web and also a book published by
O'Reilly named "sed & awk".
Here is a good place to start:
http://www.grymoire.com/Unix/index.html
Thank you sir very much. And I have another question about this, the
number "16" will change if necessary, so could I use [:digit:] and
[:lower:] to separate them?
Sure. To extend Kenan's example...
sed 's/^[[:digit:]]\+/&>/'
This will work with GNU sed, but not with regular sed. Although OP
mentioned he is a Linux user, it should be noted that sed uses basic
regular expressions, so the plus sign cannot be used to match one or
more occurences of a RE. To do it portably, an interval expression
should be used instead:
You mean "portably" as defined by POSIX, I suppose, not generally.
sed 's/^[[:digit:]]\{1,\}/&>/'
According to my old UNIX System V based book "interval expressions" are
available with the regex library only on _some_ systems (while the '+'
is documented to be part of the regex lib; I'm quite sure though that
I've heard of regex applications where '+' is indeed not available).
If you want to be on the safe side you may want to avoid both
sed 's/^[[:digit:]][[:digit:]]\*/&>/'
or even avoid the character classes which cannot be assumed on old
sed's
sed 's/^[0-9][0-9]\*/&>/'
(assuming no EBCDIC or similar). Anyway, we're on Linux as you noted.
Janis
.
- References:
- Help: How to add a character in the line
- From: Amy Lee
- Re: Help: How to add a character in the line
- From: Kenan Kalajdzic
- Re: Help: How to add a character in the line
- From: Amy Lee
- Re: Help: How to add a character in the line
- From: Janis Papanagnou
- Re: Help: How to add a character in the line
- From: Kenan Kalajdzic
- Help: How to add a character in the line
- Prev by Date: Re: rcp : permission denied
- Next by Date: Top 10 posters comp.unix.shell
- Previous by thread: Re: Help: How to add a character in the line
- Index(es):
Relevant Pages
|