Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 15
hi all i would like split this ID=123:one=a:two=a@yahoo.com:three=789:four=:five= 123 i tried this Code: echo "ID=123:one=a:two=a@yahoo.com:three=789:four=:five=123" | sed 's/\(.*=\)\(.*\):\(.*=\)\(.*\):\(.*=\)\(.*\):\(.*=\)\(.*\):\(.*=\)\(.*\):.*$/\/\/begin\ \ \ /' but showing out put like this [root@localhost giis4.1]# echo ...
  1. #1
    Linux Newbie
    Join Date
    Feb 2007
    Location
    hyderabad, india
    Posts
    247

    how to split

    hi all
    i would like split this
    ID=123:one=a:two=a@yahoo.com:three=789:four=:five= 123
    i tried this
    Code:
    echo "ID=123:one=a:two=a@yahoo.com:three=789:four=:five=123" | sed 's/\(.*=\)\(.*\):\(.*=\)\(.*\):\(.*=\)\(.*\):\(.*=\)\(.*\):\(.*=\)\(.*\):.*$/\/\/begin\
    \2\
    \4\
    \6/'
    but showing out put like this
    [root@localhost giis4.1]# echo "ID=123:one=a:two=a@yahoo.com:three=789:four=:five=123" | sed 's/\(.*=\)\(.*\):\(.*=\)\(.*\):\(.*=\)\(.*\):\(.*=\)\(.*\):\(.*=\)\(.*\):.*$/\/\/begin\
    > \2\
    > \4\
    > \6/'
    '//begin
    123
    a
    a@yahoo.com
    [root@localhost giis4.1]#
    i wanna output like
    //begin
    a
    a@yahoo.com
    789

    123

    in this four has blank
    coz, it has no number.

    how can can i get this

    please help me

    thank you in advance

  2. #2
    Linux Newbie birdman's Avatar
    Join Date
    Mar 2006
    Location
    Ireland
    Posts
    141
    I like sed and use it liberally but I would use cut and a bit of logic for this:

    Code:
    count=1
    test="ID=123:one=a:two=a@yahoo.com:three=789:four=:five=123"
    while [ `echo $test | cut -d: -f$count` ]
    do 
        both=`echo $test | cut -d: -f$count`
        first=`echo $test | cut -d: -f$count | cut -d= -f1`
        second=`echo $test | cut -d: -f$count | cut -d= -f2`
        if [ $first == "ID" ]
        then
            echo "//begin"
        else
            echo $second
        fi
        count=$((count + 1))
    done
    Regards

  3. #3
    Linux Newbie
    Join Date
    Feb 2007
    Location
    hyderabad, india
    Posts
    247

    thank you for quick replay

    Quote Originally Posted by birdman
    I like sed and use it liberally but I would use cut and a bit of logic for this:

    Code:
    test="ID=123:one=a:two=a@yahoo.com:three=789:four=:five=123
    one=b:two=b@yahoo.com:three=9:four=1:five=23
    one=c:two=c@yahoo.com:three=7:four=:five=12
    one=d:two=d@yahoo.com:three=:four=1:five=13
    :/"
    Regards
    if i take like this
    it is not printing any messages.
    it has to be print like this
    //begin
    a
    a@yahoo.com
    789

    123
    //begin
    b
    b@yahoo.com
    .........
    .......
    .......
    like this


    can you help me in this way

    thank you in advance

  4. #4
    Just Joined!
    Join Date
    Apr 2007
    Posts
    5

    reply

    count=1
    test="ID=123:one=a:two=a@yahoo.com:three=789:four= :five=123"
    while [ -n "$( echo $test | cut -f$count -d':' )" ]
    do
    x=$( echo $test | cut -f$count -d':' )
    if [ "$count" -eq 1 ]
    then
    echo Begin
    x=$( echo $x | cut -f2- -d'=' | cut -b2- )
    echo $x
    else
    x=$( echo $x | cut -f2 -d'=')
    echo $x
    fi
    ((count=count+1))
    done

  5. #5
    Linux Newbie
    Join Date
    Feb 2007
    Location
    hyderabad, india
    Posts
    247

    thank you for quick replay

    Quote Originally Posted by Saurabh0008
    count=1
    count=1
    test="ID=123:one=a:two=a@yahoo.com:three=789:four= :five=123"
    while [ -n "$( echo $test | cut -f$count -d':' )" ]
    do
    x=$( echo $test | cut -f$count -d':' )
    if [ "$count" -eq 1 ]
    then
    echo Begin
    x=$( echo $x | cut -f2- -d'=' | cut -b2- )
    echo $x
    else
    x=$( echo $x | cut -f2 -d'=')
    echo $x
    fi
    ((count=count+1))
    done
    it giving output like this
    Begin
    23
    a
    a@yahoo.com
    789

    123
    i no need of getting "23" after Begin.

    and the in must be
    test="ID=123:one=a:two=a@yahoo.com:three=789:four= :five=123
    one=b:two=b@yahoo.com:three=9:four=1:five=23
    one=c:two=c@yahoo.com:three=7:four=:five=12
    one=d:two=d@yahoo.com:three=:four=1:five=13
    :/"

    can you help me
    thank you in advance

  6. #6
    Linux Newbie
    Join Date
    Feb 2007
    Location
    hyderabad, india
    Posts
    247

    thank you for quick replay

    hi
    i tried this also
    Code:
    count=1
    test="list=S$ID=123:one=a:two=a@yahoo.com:three=789:four=:five=123
    ID=123:one=b:two=b@yahoo.com:three=9:four=1:five=23
    ID=123:one=c:two=c@yahoo.com:three=7:four=:five=12
    ID=123:one=d:two=d@yahoo.com:three=:four=1:five=13"
    while [ -n "$( echo $test | cut -f$count -d':' )" ]
    do
    x=$( echo $test | cut -f$count -d':' )
    if [ "$count" -eq 1 ]
    then
    echo Begin
    x=$( echo $x | cut -f2- -d'=' | cut -b3- )
    echo $x
    else
    x=$( echo $x | cut -f2 -d'=')
    echo $x
    fi
    ((count=count+1))
    done
    it is giving output like
    [root@localhost ~]# /root/Desktop/1234/ram4
    Begin
    123
    a
    a@yahoo.com
    789

    123 ID
    b
    b@yahoo.com
    9
    1
    23 ID
    c
    c@yahoo.com
    7

    12 ID
    d
    d@yahoo.com

    1
    13
    [root@localhost ~]#
    i wanna have output like
    Begin
    a
    a@yahoo.com
    789

    123
    Begin
    b
    b@yahoo.com
    9
    1
    23
    Begin
    c
    c@yahoo.com
    7

    12
    Begin
    d
    d@yahoo.com

    1
    13
    can you help me

    thank you in advance

  7. #7
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    One way to approach this in perl:
    Code:
    #!/usr/bin/perl
    
    # @(#) p1       Demonstrate non-greedy perl match.
    
    use warnings;
    use strict;
    
    my ($lines) = 0;
    my ( $i, $j, @a );
    
    while (<>) {
        $lines++;
        @a = ();
        s/$/:/;    # add closing delimiter
        (@a) = /.*?=(.*?):/g;    # extract =(anything):
    
        $j    = 0;
        $a[0] = "Begin";         # replace first element
        foreach $i (@a) {
            print $i, "\n";
            $j++;
        }
    }
    
    print STDERR " ( Lines read: $lines )\n";
    
    exit(0);
    Which produces:
    Code:
    % ./p1 data1
    Begin
    a
    a@yahoo.com
    789
    
    123
    Begin
    b
    b@yahoo.com
    9
    1
    23
    Begin
    c
    c@yahoo.com
    7
    
    12
    Begin
    d
    d@yahoo.com
    
    1
    13
     ( Lines read: 4 )
    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  8. #8
    Linux Newbie
    Join Date
    Feb 2007
    Location
    hyderabad, india
    Posts
    247

    thank you for quick replay

    Quote Originally Posted by munna_dude
    it giving output like this
    Begin
    23
    a
    a@yahoo.com
    789

    123
    i no need of getting "23" after Begin.

    and the in must be
    test="ID=123:one=a:two=a@yahoo.com:three=789:four= :five=123
    one=b:two=b@yahoo.com:three=9:four=1:five=23
    one=c:two=c@yahoo.com:three=7:four=:five=12
    one=d:two=d@yahoo.com:three=:four=1:five=13
    :/"

    can you help me
    thank you in advance
    i tried

    output is
    [root@localhost ~]# /root/Desktop/1234/pearl1 /root/Desktop/1234/filename.txt
    Begin
    Begin
    a
    a@yahoo.com
    789

    123
    Begin
    b
    b@yahoo.com
    9
    1
    23
    Begin
    c
    c@yahoo.com
    7

    12
    Begin
    d
    d@yahoo.com

    1
    13
    ( Lines read: 5 )
    [root@localhost ~]#
    Begin coming two times (at first line),

    can u help thank you in advance

  9. #9
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    I used this data set:
    Code:
    ID=123:one=a:two=a@yahoo.com:three=789:four=:five=123
    ID=123:one=b:two=b@yahoo.com:three=9:four=1:five=23
    ID=123:one=c:two=c@yahoo.com:three=7:four=:five=12
    ID=123:one=d:two=d@yahoo.com:three=:four=1:five=13
    Please post the data set you used ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  10. #10
    Just Joined!
    Join Date
    Apr 2007
    Posts
    5

    reply

    hi,
    i would like to know whether ur i/p is from a file or from the terminal.and secondly is $test getting single values i.e "id=123........................................... ........:five=...." .

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...