How to change certain field separated by comma

Discussion in 'Perl Misc' started by rashidaq@gmail.com, Sep 25, 2005.

  1. Guest

    I have file which is separated by commas.
    This is like csv file, actually a database which
    has 10 to 15 fields separated by commas.

    I can get a print of field number 6 awk on a file which has fields
    separated by commas
    awk -F, '{print $6}' file
    this prints number 6 field only.

    Now how do I substitute a certain field with another word/string on
    certain line range.

    I have tried the vi editor
    I have manage to append line #6 to #1876 to the end of lines
    here is what I do
    :2,1876s/$/stringtobeAppended/g and it works but

    now how do I replace in the middle like field number 6 separated by
    commas
    on certain line range.

    if done in PERL easily then I have to learn that too.

    thanks in advance
    Rashid
    , Sep 25, 2005
    #1
    1. Advertising

  2. On Sun, 25 Sep 2005 23:39:38 +0200, <> wrote:

    > I have file which is separated by commas.
    > This is like csv file, actually a database which
    > has 10 to 15 fields separated by commas.
    >
    > I can get a print of field number 6 awk on a file which has fields
    > separated by commas
    > awk -F, '{print $6}' file
    > this prints number 6 field only.
    >
    > Now how do I substitute a certain field with another word/string on
    > certain line range.
    >
    > I have tried the vi editor
    > I have manage to append line #6 to #1876 to the end of lines
    > here is what I do
    > :2,1876s/$/stringtobeAppended/g and it works but
    >
    > now how do I replace in the middle like field number 6 separated by
    > commas
    > on certain line range.
    >
    > if done in PERL easily then I have to learn that too.
    >
    > thanks in advance
    > Rashid
    >


    awk -F, 'BEGIN {OFS=","}
    /FirstsPattern/,/LastPattern/{$6="TheReplacement"; print}'

    -Enrique
    Enrique Perez-Terron, Sep 25, 2005
    #2
    1. Advertising

  3. Ed Morton Guest

    wrote:

    > I have file which is separated by commas.
    > This is like csv file, actually a database which
    > has 10 to 15 fields separated by commas.
    >
    > I can get a print of field number 6 awk on a file which has fields
    > separated by commas
    > awk -F, '{print $6}' file
    > this prints number 6 field only.
    >
    > Now how do I substitute a certain field with another word/string on
    > certain line range.
    >
    > I have tried the vi editor
    > I have manage to append line #6 to #1876 to the end of lines
    > here is what I do
    > :2,1876s/$/stringtobeAppended/g and it works but
    >
    > now how do I replace in the middle like field number 6 separated by
    > commas
    > on certain line range.


    awk -F, 'NR>1&&NR<1877{$6=newText}1'

    Regards,

    Ed.
    Ed Morton, Sep 26, 2005
    #3
  4. wrote:
    > I have file which is separated by commas.
    > This is like csv file, [...]


    Then why not just use the Text::CSV module?

    jue
    Jürgen Exner, Sep 26, 2005
    #4
  5. In article <lpGZe.1268$qC4.1151@trnddc02>,
    Jürgen Exner <> wrote:
    > wrote:
    >> I have file which is separated by commas.
    >> This is like csv file, [...]

    >
    >Then why not just use the Text::CSV module?


    Ah, the dangers of cross-posting.

    It should be a rule (i.e., convention) that when respoding to
    a cross-posted item, you must identiy which group you are posting from.

    (Posting from comp.unix.shell, hence having no idea what a "Text::CSV"
    is... [*])

    [*] Not true, of course. I recognize that as a Perl thing, so I am
    assuming that you are posting from one of the Perl groups, and didn't check
    the "Newsgroups" line before posting.
    Kenny McCormack, Sep 26, 2005
    #5
  6. Joe Smith Guest

    Enrique Perez-Terron wrote:

    >> now how do I replace in the middle like field number 6 separated by
    >> commas
    >> on certain line range.
    >>
    >> if done in PERL easily then I have to learn that too.
    >>
    >> thanks in advance
    >> Rashid
    >>

    >
    > awk -F, 'BEGIN {OFS=","}
    > /FirstsPattern/,/LastPattern/{$6="TheReplacement"; print}'


    And now for a perl solution:

    linux% cat Temp
    BEGIN {FS=OFS=","} /FirstsPattern/,/LastPattern/{$6="TheReplacement";
    print}
    linux% a2p Temp
    #!/usr/bin/perl
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if $running_under_some_shell;
    # this emulates #! processing on NIH machines.
    # (remove #! line above if indigestible)

    eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
    # process any FOO=bar switches

    $[ = 1; # set array base to 1
    $FS = ' '; # set field separator
    $, = ' '; # set output field separator
    $\ = "\n"; # set output record separator

    $FS = $, = ',';

    while (<>) {
    chomp; # strip record separator
    @Fld = split($FS, $_, 9999);
    if (/FirstsPattern/ .. /LastPattern/) {
    $Fld[6] = 'TheReplacement';
    print join($,,@Fld);
    }
    }
    linux%
    -Joe
    Joe Smith, Sep 26, 2005
    #6
  7. Damian James Guest

    On 25 Sep 2005 14:39:38 -0700, said:
    > I have file which is separated by commas.
    > This is like csv file, actually a database which
    > has 10 to 15 fields separated by commas.


    Is it really CSV, or as you say just database fields separated by
    commas? There are no commas in the fields? If there are, how are
    they escaped? Does this format use quote characters?

    Actual CSV is annoying to parse, and if you were writing a script to
    manipulate these files in Perl, you'd probably want to use the
    Text::CSV module as advised elswhere in this thread.

    >...
    >:2,1876s/$/stringtobeAppended/g and it works but
    > now how do I replace in the middle like field number 6 separated by
    > commas on certain line range.


    You've had a couple of awk answers, which probably make the most
    sense for a one liner.

    > if done in PERL easily then I have to learn that too.


    Perl one liner for completeness:

    perl -F, -lane '$F[6] = "new" if 2..1876; print join ",", @F' filename

    I'm sure others in clpm can golf that down somewhat ;)

    --Damian
    Damian James, Sep 26, 2005
    #7
  8. wrote:
    > I have file which is separated by commas.
    > This is like csv file, actually a database which
    > has 10 to 15 fields separated by commas.
    >
    > I can get a print of field number 6 awk on a file which has fields
    > separated by commas
    > awk -F, '{print $6}' file
    > this prints number 6 field only.
    >
    > Now how do I substitute a certain field with another word/string on
    > certain line range.
    >
    > I have tried the vi editor
    > I have manage to append line #6 to #1876 to the end of lines
    > here is what I do
    > :2,1876s/$/stringtobeAppended/g and it works but
    >
    > now how do I replace in the middle like field number 6 separated by
    > commas
    > on certain line range.
    >
    > if done in PERL easily then I have to learn that too.
    >
    > thanks in advance
    > Rashid


    awk 'BEGIN{FS=OFS=","}NR>1,NR<1877{$6="new"}1' file
    William James, Sep 26, 2005
    #8
  9. In comp.unix.solaris Logan Shaw <> wrote:
    >>> awk -F, 'BEGIN {OFS=","}
    >>> /FirstsPattern/,/LastPattern/{$6="TheReplacement"; print}'


    > And now for a slightly more concise Perl solution:


    > #! /usr/bin/perl -w
    > while (<>)
    > {
    > my @fields = split (/,/);
    > $fields[6-1] = 'TheReplacement'
    > if 6 .. 1876;
    > print join (",", @fields);
    > }


    > The code is probably a little clearer if "if 6 .. 1876" is replaced
    > with its equivalent, "if $. == 6 .. $. == 1876". But then I was
    > going for brevity.


    Or even...

    perl -F, -ane '$F[5] =~ s/bob/sue/ if 6 .. 1876 ;print join(",",@F);'

    --
    Darren Dunham
    Senior Technical Consultant TAOS http://www.taos.com/
    Got some Dr Pepper? San Francisco, CA bay area
    < This line left intentionally blank to confuse you. >
    Darren Dunham, Sep 26, 2005
    #9
  10. Damian James wrote:
    > On 25 Sep 2005 14:39:38 -0700, said:
    > > I have file which is separated by commas.
    > > This is like csv file, actually a database which
    > > has 10 to 15 fields separated by commas.

    >
    > Is it really CSV, or as you say just database fields separated by
    > commas? There are no commas in the fields? If there are, how are
    > they escaped? Does this format use quote characters?
    >
    > Actual CSV is annoying to parse, and if you were writing a script to
    > manipulate these files in Perl, you'd probably want to use the
    > Text::CSV module as advised elswhere in this thread.
    >
    > >...
    > >:2,1876s/$/stringtobeAppended/g and it works but
    > > now how do I replace in the middle like field number 6 separated by
    > > commas on certain line range.

    >
    > You've had a couple of awk answers, which probably make the most
    > sense for a one liner.
    >
    > > if done in PERL easily then I have to learn that too.

    >
    > Perl one liner for completeness:
    >
    > perl -F, -lane '$F[6] = "new" if 2..1876; print join ",", @F' filename
    >
    > I'm sure others in clpm can golf that down somewhat ;)
    >
    > --Damian


    ruby -F, -pane'$F[5]="new" if 2..1876;$_=$F*","'
    William James, Sep 26, 2005
    #10
  11. l v Guest

    William James wrote:

    >>Perl one liner for completeness:
    >>
    >> perl -F, -lane '$F[6] = "new" if 2..1876; print join ",", @F' filename
    >>
    >>I'm sure others in clpm can golf that down somewhat ;)
    >>
    >>--Damian

    >
    >
    > ruby -F, -pane'$F[5]="new" if 2..1876;$_=$F*","'
    >


    What version of Perl are you using? I can not find any Perl
    documentation regarding "ruby" on my systems.

    C:\>perldoc ruby
    No documentation found for "ruby".

    C:\>perldoc -f ruby
    No documentation for perl function `ruby' found


    Both are true for ActiveState Perl 5.6.1 build 631 and AIX 5.8.0


    Len

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    l v, Sep 26, 2005
    #11
  12. Guest

    Its actually record(db) file separated with commas
    I used the awk script given by you guys

    awk -F, 'NR>3&&NR<=7{$2="new_string"}1' file > newfile

    I was wondering rather "> newfile" can I use sed to
    edit the file.

    the PERL stuff went over my head, but I bought the
    perl book and I will open that book today.


    thank you everybody for your help

    Rashid




    William James wrote:
    > wrote:
    > > I have file which is separated by commas.
    > > This is like csv file, actually a database which
    > > has 10 to 15 fields separated by commas.
    > >
    > > I can get a print of field number 6 awk on a file which has fields
    > > separated by commas
    > > awk -F, '{print $6}' file
    > > this prints number 6 field only.
    > >
    > > Now how do I substitute a certain field with another word/string on
    > > certain line range.
    > >
    > > I have tried the vi editor
    > > I have manage to append line #6 to #1876 to the end of lines
    > > here is what I do
    > > :2,1876s/$/stringtobeAppended/g and it works but
    > >
    > > now how do I replace in the middle like field number 6 separated by
    > > commas
    > > on certain line range.
    > >
    > > if done in PERL easily then I have to learn that too.
    > >
    > > thanks in advance
    > > Rashid

    >
    > awk 'BEGIN{FS=OFS=","}NR>1,NR<1877{$6="new"}1' file
    , Sep 26, 2005
    #12
  13. Ed Morton Guest

    wrote:

    > Its actually record(db) file separated with commas
    > I used the awk script given by you guys
    >
    > awk -F, 'NR>3&&NR<=7{$2="new_string"}1' file > newfile


    Why NR<=7 instead of NR<8?

    > I was wondering rather "> newfile" can I use sed to
    > edit the file.


    You mean edit the file in-line without using a tmp file? GNU sed can do
    that with it's "-i" option, and "ed" can do it by default, but in either
    case you still have that pesky field identification issue. You can get
    round that by using an RE like this one which replaces the second field
    ("old") with the word "new":

    $ echo "pre old post" | sed
    's/\([^[:space:]]*[[:space:]]*\)[^[:space:]]*/\1new/'
    pre new post

    If you need it in some other field, you can specify a repetition count
    for the number of fields to skip, e.g.:

    $ echo "f1 f2 f3 f4 f5 f6" | sed
    's/\(\([^[:space:]]*[[:space:]]*\)\{2\}\)[^[:space:]]*/\1new/'
    f1 f2 new f4 f5 f6
    $ echo "f1 f2 f3 f4 f5 f6" | sed
    's/\(\([^[:space:]]*[[:space:]]*\)\{4\}\)[^[:space:]]*/\1new/'
    f1 f2 f3 f4 new f6

    You'll need to throw in an extra pattern if you want to accomodate white
    space at the start of a line.

    Regards,

    Ed.
    Ed Morton, Sep 26, 2005
    #13
  14. In comp.unix.solaris Logan Shaw <> wrote:
    >> Or even...
    >>
    >> perl -F, -ane '$F[5] =~ s/bob/sue/ if 6 .. 1876 ;print join(",",@F);'


    > If you want to get cryptic, you can save a few characters:


    > perl -F'/(,)/' -ane '$F[10] =~ s/bob/sue/ if 6 .. 1876; print @F'


    > That might even run faster because it avoids the join().


    Ah, that made me think that doing $,="," might help. It would probably
    aid clarity, but it doesn't seem to be actually shorter for any
    construction of mine. (Also, I'd have either do it in a BEGIN block or
    run it every time through the loop. The latter would work fine, but it
    would annoy me.) :)

    --
    Darren Dunham
    Senior Technical Consultant TAOS http://www.taos.com/
    Got some Dr Pepper? San Francisco, CA bay area
    < This line left intentionally blank to confuse you. >
    Darren Dunham, Sep 27, 2005
    #14
  15. Damian James Guest

    On Mon, 26 Sep 2005 23:57:53 GMT, Darren Dunham said:
    > In comp.unix.solaris Logan Shaw <> wrote:
    >>> Or even...
    >>>
    >>> perl -F, -ane '$F[5] =~ s/bob/sue/ if 6 .. 1876 ;print join(",",@F);'

    >
    >> If you want to get cryptic, you can save a few characters:

    >
    >> perl -F'/(,)/' -ane '$F[10] =~ s/bob/sue/ if 6 .. 1876; print @F'

    >
    >> That might even run faster because it avoids the join().

    >
    > Ah, that made me think that doing $,="," might help. It would probably
    > aid clarity, but it doesn't seem to be actually shorter for any
    > construction of mine. (Also, I'd have either do it in a BEGIN block or
    > run it every time through the loop. The latter would work fine, but it
    > would annoy me.) :)


    *scratches head*

    (I note that I introduced an off-by-one error cross thread by
    equating awk's $6 with $F[6] instead of $F[5]).

    Anyhow, with $, set:

    perl -F, -lane '$,=",";$F[5] = "new" if 2..1876; print @F'

    So yeah, it takes more characters, especially if you wanted a BEGIN block.
    Similarly with $".

    NB, the -l permits changing $F[-1] without needing to think about
    line endings, not that that was required, but it seemed like the OP
    wished to change arbitrary elements.

    Likewise, I understood the OP intended to replace the whole element,
    not a substring of it (the various awk solutions made this assumption).

    So how about:

    perl -F, -ape 's/$F[5]/new/ if 2..1876'

    That breaks if $F[5] is a substring of an element to its left, of course.

    --Damian
    Damian James, Sep 27, 2005
    #15
  16. wrote:
    > I have file which is separated by commas.
    > This is like csv file, actually a database which
    > has 10 to 15 fields separated by commas.
    >
    > I can get a print of field number 6 awk on a file which has fields
    > separated by commas
    > awk -F, '{print $6}' file
    > this prints number 6 field only.
    >
    > Now how do I substitute a certain field with another word/string on
    > certain line range.
    >
    > I have tried the vi editor
    > I have manage to append line #6 to #1876 to the end of lines
    > here is what I do
    > :2,1876s/$/stringtobeAppended/g and it works but
    >
    > now how do I replace in the middle like field number 6 separated by
    > commas
    > on certain line range.
    >
    > if done in PERL easily then I have to learn that too.
    >
    > thanks in advance
    > Rashid


    ruby -F, -pane'$F[5]="new" if 2..7;$_=$F*","'
    William James, Sep 27, 2005
    #16
  17. Guest

    thanks for the sed script.

    I have also tried the RUBY stuff in my cygwin and it worked.
    This RUBY must be new, never heard of it.

    How popular is it now as compared to Java, Perl or Python

    I have taken a class in Java, its good but ..

    But for my work working as telecom engr working on solaris system
    most of the time, I think Ruby fits.
    I dont know much about it.
    Just downloaded ruby editor from FORGE site today.

    thanks
    Rashid

    William James wrote:
    > wrote:
    > > I have file which is separated by commas.
    > > This is like csv file, actually a database which
    > > has 10 to 15 fields separated by commas.
    > >
    > > I can get a print of field number 6 awk on a file which has fields
    > > separated by commas
    > > awk -F, '{print $6}' file
    > > this prints number 6 field only.
    > >
    > > Now how do I substitute a certain field with another word/string on
    > > certain line range.
    > >
    > > I have tried the vi editor
    > > I have manage to append line #6 to #1876 to the end of lines
    > > here is what I do
    > > :2,1876s/$/stringtobeAppended/g and it works but
    > >
    > > now how do I replace in the middle like field number 6 separated by
    > > commas
    > > on certain line range.
    > >
    > > if done in PERL easily then I have to learn that too.
    > >
    > > thanks in advance
    > > Rashid

    >
    > ruby -F, -pane'$F[5]="new" if 2..7;$_=$F*","'
    , Sep 27, 2005
    #17
  18. On Tue, 27 Sep 2005 07:13:44 +0200, <> wrote:

    > thanks for the sed script.
    >
    > I have also tried the RUBY stuff in my cygwin and it worked.
    > This RUBY must be new, never heard of it.


    Ruby's inventor named it Ruby thinking that was a suitable name
    for a language that wanted to become the successor of Pe(a)rl.

    > How popular is it now as compared to Java, Perl or Python


    I have not statistics, but I believe it still lives pretty much
    in the shadow of these. When Perl appeared, it was like the
    answer to everybody's frustrations. It had everything that
    the day's scripting languges had been insisting on not having
    for many years. When Ruby appeared, Perl was there, and it worked.

    For a long time, Perl had CPAN, an enormous internet archive of
    modules that would install themselves (or that Perl would
    install) *correctly* with a single command that was (is) the same
    on all architectures.

    Only quite recently, I believe, has Ruby got that kind of
    infrastructure.

    I don't know why python got so much wind in its sails. I never
    had the occasion to learn Ruby or Python well enough to judge
    their relative merits. To my still rather superficial judgement
    they have much in common, and seem to have been inpired by
    much of the same theories. Both are very clean, uncluttered,
    and rich in similar ways. Yet Python does not seem to have the
    same advantage when it comes to one-liners.

    > I have taken a class in Java, its good but ..


    :)

    > But for my work working as telecom engr working on solaris system
    > most of the time, I think Ruby fits.
    > I dont know much about it.
    > Just downloaded ruby editor from FORGE site today.


    -Enrique
    Enrique Perez-Terron, Sep 27, 2005
    #18
  19. [follow-ups trimmed]

    wrote:
    > thanks for the sed script.
    >
    > I have also tried the RUBY stuff in my cygwin and it worked.
    > This RUBY must be new, never heard of it.


    OK - you have never heard of it. This puzzles me because later you say...

    >
    > How popular is it now as compared to Java, Perl or Python
    >
    > I have taken a class in Java, its good but ..
    >
    > But for my work working as telecom engr working on solaris system
    > most of the time, I think Ruby fits.


    You've never heard of it but you think it fits? Am not arguing for or
    against the utility of Ruby for your problem domain, but am interested
    in your logic.

    > I dont know much about it.


    :) But it's a good fit, right?

    puzzled,

    Mark
    Mark Clements, Sep 27, 2005
    #19
  20. wrote:
    > thanks for the sed script.
    >
    > I have also tried the RUBY stuff in my cygwin and it worked.
    > This RUBY must be new, never heard of it.
    >
    > How popular is it now as compared to Java, Perl or Python
    >
    > I have taken a class in Java, its good but ..
    >
    > But for my work working as telecom engr working on solaris system
    > most of the time, I think Ruby fits.
    > I dont know much about it.
    > Just downloaded ruby editor from FORGE site today.


    Go to comp.lang.ruby and see the action.

    By the way, my Ruby solution was 1 character too long; it could
    have been:

    ruby -F, -pae'$F[5]="new" if 2..7;$_=$F*","' file
    William James, Sep 27, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Miles
    Replies:
    1
    Views:
    593
    Jürgen Exner
    May 28, 2004
  2. Peter Rilling

    Array to a comma Separated String

    Peter Rilling, Jul 8, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    499
  3. DrLovely@.
    Replies:
    7
    Views:
    475
    DrLovely@.
    Mar 25, 2005
  4. RogerTBrick
    Replies:
    3
    Views:
    4,156
    RogerTBrick
    Mar 7, 2005
  5. =?Utf-8?B?Q2hyaXMgTGFuZQ==?=

    How to stream a comma separated string to the browser?

    =?Utf-8?B?Q2hyaXMgTGFuZQ==?=, Jul 21, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    339
    =?Utf-8?B?Q2hyaXMgTGFuZQ==?=
    Jul 21, 2006
Loading...

Share This Page