Re: Help: How to add a character in the line



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
.



Relevant Pages

  • Re: regex, negations, grep, find and replace (a few questions)
    ... I do not know much regex. ... But it seems as if you define each character ... be aware that different tools may use slightly different syntaxes for the same regular expressions. ... expresion" might mean "matching everything not matching the regular expression" or, in other words, removing everything matching the regexp. ...
    (alt.os.linux)
  • Re: problem using Regex in a C# addin
    ... Macros in VS.Net don't use the .Net regular expressions synatx, ... > i'm trying to comment out a statement in an addin using Regex with the ... > special character so that the statements can be restored at a later ... > string s, t, sPattern; ...
    (microsoft.public.vsnet.ide)
  • Re: Help: How to add a character in the line
    ... I'm a Linux user, I'd like to know how to use sed or other command to add ... a character in the line. ... possible about regular expressions and the syntax of 'sed'. ...
    (comp.unix.shell)
  • Help: How to add a character in the line
    ... I'm a Linux user, I'd like to know how to use sed or other command to add ... a character in the line. ... Amy Lee ...
    (comp.unix.shell)