To modify a file from the command line

Discussion in 'Perl Misc' started by clearguy02@yahoo.com, Feb 6, 2008.

  1. Guest

    Hi all,

    I have this file, test.txt and I want to replace a string "bsmith"
    with "brsmith" in that file from a command line itself (windows xp).
    how can i do it?

    Thanks
    J
     
    , Feb 6, 2008
    #1
    1. Advertising

  2. Guest

    On Feb 6, 2:59 pm, wrote:
    > Hi all,
    >
    > I have this file, test.txt and I want to replace a string "bsmith"
    > with "brsmith" in that file from a command line itself (windows xp).
    > how can i do it?
    >
    > Thanks
    > J


    Sorry.. I forgot to add the line.

    c:\perl -pe "s/bsmith/brsmith/g" <test.txt

    I meant to ask if there is any other powerful command line one liner?

    Thanks,
    J
     
    , Feb 6, 2008
    #2
    1. Advertising

  3. writes:

    > On Feb 6, 2:59 pm, wrote:
    >> Hi all,
    >>
    >> I have this file, test.txt and I want to replace a string "bsmith"
    >> with "brsmith" in that file from a command line itself (windows xp).
    >> how can i do it?
    >>
    >> Thanks
    >> J

    >
    > Sorry.. I forgot to add the line.
    >
    > c:\perl -pe "s/bsmith/brsmith/g" <test.txt
    >
    > I meant to ask if there is any other powerful command line one liner?


    Drop the < for one character less.

    that's about as good as it gets, without using sed

    Joost.
     
    Joost Diepenmaat, Feb 6, 2008
    #3
  4. Guest

    On Feb 6, 3:24 pm, Joost Diepenmaat <> wrote:
    > writes:
    > > On Feb 6, 2:59 pm, wrote:
    > >> Hi all,

    >
    > >> I have this file, test.txt and I want to replace a string "bsmith"
    > >> with "brsmith" in that file from a command line itself (windows xp).
    > >> how can i do it?

    >
    > >> Thanks
    > >> J

    >
    > > Sorry.. I forgot to add the line.

    >
    > > c:\perl -pe "s/bsmith/brsmith/g" <test.txt

    >
    > > I meant to ask if there is any other powerful command line one liner?

    >
    > Drop the < for one character less.
    >
    > that's about as good as it gets, without using sed
    >
    > Joost.- Hide quoted text -
    >
    > - Show quoted text -



    No it is not working. I just want to replace the word in the same
    file, test1.txt

    c:\perl -pe "s/bsmith/brsmith/g" test.txt

    doesn't work.

    Even, c:\perl -pe "s/bsmith/brsmith/g" <test.txt >test.txt
    not working :(

    Thanks
    J
     
    , Feb 7, 2008
    #4
  5. writes:

    > On Feb 6, 3:24 pm, Joost Diepenmaat <> wrote:
    > No it is not working. I just want to replace the word in the same
    > file, test1.txt


    Oh right, I missed that part.

    > c:\perl -pe "s/bsmith/brsmith/g" test.txt


    Try

    perl -i -pe "s/bsmith/brsmith/g" test.txt

    See perlrun for the -i switch.

    Joost.
     
    Joost Diepenmaat, Feb 7, 2008
    #5
  6. Ben Morrow Guest

    Quoth Joost Diepenmaat <>:
    > writes:
    >
    > > On Feb 6, 3:24 pm, Joost Diepenmaat <> wrote:
    > > No it is not working. I just want to replace the word in the same
    > > file, test1.txt

    >
    > Oh right, I missed that part.
    >
    > > c:\perl -pe "s/bsmith/brsmith/g" test.txt

    >
    > Try
    >
    > perl -i -pe "s/bsmith/brsmith/g" test.txt


    Or rather,

    perl -i~ -pe "s/bsmith/brsmith/g" test.txt

    since Win32 doesn't have inodes, so can't do inplace edit without a
    backup.

    Ben
     
    Ben Morrow, Feb 7, 2008
    #6
  7. Guest

    On Feb 6, 4:20 pm, Ben Morrow <> wrote:
    > Quoth Joost Diepenmaat <>:
    >
    > > writes:

    >
    > > > On Feb 6, 3:24 pm, Joost Diepenmaat <> wrote:
    > > > No it is not working. I just want to replace the word in the same
    > > > file, test1.txt

    >
    > > Oh right, I missed that part.

    >
    > > > c:\perl -pe "s/bsmith/brsmith/g" test.txt

    >
    > > Try

    >
    > > perl -i -pe "s/bsmith/brsmith/g" test.txt

    >
    > Or rather,
    >
    >     perl -i~ -pe "s/bsmith/brsmith/g" test.txt
    >
    > since Win32 doesn't have inodes, so can't do inplace edit without a
    > backup.
    >
    > Ben


    Thanks Ben..

    But I don't want to create another file, text1.txt~...
    perl -i~ -pe "s/bsmith/brsmith/g" test.txt

    Is there any way to do in-place editing with no backup file creations?

    Thanks,
    J
     
    , Feb 7, 2008
    #7
  8. Uri Guttman Guest

    >>>>> "BM" == Ben Morrow <> writes:

    BM> Quoth Joost Diepenmaat <>:
    >> writes:
    >>
    >> > On Feb 6, 3:24 pm, Joost Diepenmaat <> wrote:
    >> > No it is not working. I just want to replace the word in the same
    >> > file, test1.txt

    >>
    >> Oh right, I missed that part.
    >>
    >> > c:\perl -pe "s/bsmith/brsmith/g" test.txt

    >>
    >> Try
    >>
    >> perl -i -pe "s/bsmith/brsmith/g" test.txt


    BM> Or rather,

    BM> perl -i~ -pe "s/bsmith/brsmith/g" test.txt

    BM> since Win32 doesn't have inodes, so can't do inplace edit without a
    BM> backup.

    what do inodes have to do with it? perl doesn't do true inplace edits as
    it could screw it up with a large file. if you output more than you
    input, you could overwrite later input data in the same file. -i always
    writes to another file and renames it when the program is done. you can
    check this out by looking at the inode numbers. it wouldn't make sense
    to not use a new file.

    i use the same idea in File::Slurp in write_file with the atomic option
    on. it writes the whole file to a temp name and when done calls rename
    (which is atomic) so you always have a legit file around.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Architecture, Development, Training, Support, Code Review ------
    ----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Feb 7, 2008
    #8
  9. Guest

    On Feb 6, 4:56 pm, Uri Guttman <> wrote:
    > >>>>> "BM" == Ben Morrow <> writes:

    >
    >   BM> Quoth Joost Diepenmaat <>:
    >   >> writes:
    >   >>
    >   >> > On Feb 6, 3:24 pm, Joost Diepenmaat <> wrote:
    >   >> > No it is not working. I just want to replace the word in the same
    >   >> > file, test1.txt
    >   >>
    >   >> Oh right, I missed that part.
    >   >>
    >   >> > c:\perl -pe "s/bsmith/brsmith/g" test.txt
    >   >>
    >   >> Try
    >   >>
    >   >> perl -i -pe "s/bsmith/brsmith/g" test.txt
    >
    >   BM> Or rather,
    >
    >   BM>     perl -i~ -pe "s/bsmith/brsmith/g" test.txt
    >
    >   BM> since Win32 doesn't have inodes, so can't do inplace edit without a
    >   BM> backup.
    >
    > what do inodes have to do with it? perl doesn't do true inplace edits as
    > it could screw it up with a large file. if you output more than you
    > input, you could overwrite later input data in the same file. -i always
    > writes to another file and renames it when the program is done. you can
    > check this out by looking at the inode numbers. it wouldn't make sense
    > to not use a new file.
    >
    > i use the same idea in File::Slurp in write_file with the atomic option
    > on. it writes the whole file to a temp name and when done calls rename
    > (which is atomic) so you always have a legit file around.
    >
    > uri
    >
    > --
    > Uri Guttman  ------    --------  http://www.sysarch.com--
    > -----  Perl Architecture, Development, Training, Support, Code Review  ------
    > -----------  Search or Offer Perl Jobs  -----http://jobs.perl.org ---------
    > ---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com---------


    Thanks Uri,

    Can you pl. give an example? :)

    J
     
    , Feb 7, 2008
    #9
  10. Ben Morrow Guest

    Quoth Uri Guttman <>:
    > >>>>> "BM" == Ben Morrow <> writes:

    >
    > BM> perl -i~ -pe "s/bsmith/brsmith/g" test.txt
    >
    > BM> since Win32 doesn't have inodes, so can't do inplace edit without a
    > BM> backup.
    >
    > what do inodes have to do with it? perl doesn't do true inplace edits as
    > it could screw it up with a large file. if you output more than you
    > input, you could overwrite later input data in the same file. -i always
    > writes to another file and renames it when the program is done. you can
    > check this out by looking at the inode numbers. it wouldn't make sense
    > to not use a new file.


    Under Unix, -i with no backup unlinks the old file (holding it open) and
    then creates the new file in its place, so there's no need for another
    filename. Under systems that don't have close-behind semantics (because
    they don't have an inode layer), this is impossible: under Win32, you
    cannot delete a file while it is open. Therefore, under such systems you
    have to provide a backup filename so that both input and output files
    get to have a name. (In principle perl could work around this by making
    up a temporary filename in the same directory to use instead, but at
    least under Win32 it doesn't.)

    > i use the same idea in File::Slurp in write_file with the atomic option
    > on. it writes the whole file to a temp name and when done calls rename
    > (which is atomic) so you always have a legit file around.


    This is actually somewhat safer than inplace edit, of course, as the
    file is replaced atomically. Even with a backup name, -i does things in
    the wrong order: first it renames the old file to the backup, then it
    creates a new file by the original name for the output, so if the
    process is interrupted the file is left corrupted. I guess the perl core
    doesn't want to get involved with the issue of safely creating a
    temporary filename, and just avoids the issue.

    I have often thought Unix could do with flink(2) and mkstemp(2)
    syscalls, that create a new link to an open fd and create a new file
    with no name respectively. It would save a whole lot of mucking about
    with temporary names.

    Ben
     
    Ben Morrow, Feb 7, 2008
    #10
  11. Uri Guttman Guest

    >>>>> "BM" == Ben Morrow <> writes:

    BM> Under Unix, -i with no backup unlinks the old file (holding it open) and
    BM> then creates the new file in its place, so there's no need for another
    BM> filename. Under systems that don't have close-behind semantics (because
    BM> they don't have an inode layer), this is impossible: under Win32, you
    BM> cannot delete a file while it is open. Therefore, under such systems you
    BM> have to provide a backup filename so that both input and output files
    BM> get to have a name. (In principle perl could work around this by making
    BM> up a temporary filename in the same directory to use instead, but at
    BM> least under Win32 it doesn't.)

    interesting. i didn't think about that way of doing things.

    >> i use the same idea in File::Slurp in write_file with the atomic option
    >> on. it writes the whole file to a temp name and when done calls rename
    >> (which is atomic) so you always have a legit file around.


    BM> This is actually somewhat safer than inplace edit, of course, as the
    BM> file is replaced atomically. Even with a backup name, -i does things in
    BM> the wrong order: first it renames the old file to the backup, then it
    BM> creates a new file by the original name for the output, so if the
    BM> process is interrupted the file is left corrupted. I guess the perl core
    BM> doesn't want to get involved with the issue of safely creating a
    BM> temporary filename, and just avoids the issue.

    well, that is why i call that option atomic. the idea is to always have
    a proper file around. i have plans for inplace edit functions in
    file::slurp (line by line or whole file) and those will call write_file
    with the atomic option. then you can do inplace editing from a program
    with just a single sub call (passing it a code ref to do the editing).

    BM> I have often thought Unix could do with flink(2) and mkstemp(2)
    BM> syscalls, that create a new link to an open fd and create a new file
    BM> with no name respectively. It would save a whole lot of mucking about
    BM> with temporary names.

    open can create temp files now (i think it uses file::temp
    underneath). in slurp i take the old file name and append the pid to it
    for the temp file. maybe i need to make that part more robust but no one
    has mentioned it. one reason i don't use the open temp feature is that
    slurp is backwards compatible to 5.005.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Architecture, Development, Training, Support, Code Review ------
    ----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Feb 7, 2008
    #11
  12. Guest

    On Feb 6, 6:08 pm, Uri Guttman <> wrote:
    > >>>>> "BM" == Ben Morrow <> writes:

    >
    >   BM> Under Unix, -i with no backup unlinks the old file (holding it open) and
    >   BM> then creates the new file in its place, so there's no need for another
    >   BM> filename. Under systems that don't have close-behind semantics (because
    >   BM> they don't have an inode layer), this is impossible: under Win32, you
    >   BM> cannot delete a file while it is open. Therefore, under such systems you
    >   BM> have to provide a backup filename so that both input and output files
    >   BM> get to have a name. (In principle perl could work around this by making
    >   BM> up a temporary filename in the same directory to use instead, but at
    >   BM> least under Win32 it doesn't.)
    >
    > interesting. i didn't think about that way of doing things.
    >
    >   >> i use the same idea in File::Slurp in write_file with the atomic option
    >   >> on. it writes the whole file to a temp name and when done calls rename
    >   >> (which is atomic) so you always have a legit file around.
    >
    >   BM> This is actually somewhat safer than inplace edit, of course, as the
    >   BM> file is replaced atomically. Even with a backup name, -i does things in
    >   BM> the wrong order: first it renames the old file to the backup, thenit
    >   BM> creates a new file by the original name for the output, so if the
    >   BM> process is interrupted the file is left corrupted. I guess the perl core
    >   BM> doesn't want to get involved with the issue of safely creating a
    >   BM> temporary filename, and just avoids the issue.
    >
    > well, that is why i call that option atomic. the idea is to always have
    > a proper file around. i have plans for inplace edit functions in
    > file::slurp (line by line or whole file) and those will call write_file
    > with the atomic option. then you can do inplace editing from a program
    > with just a single sub call (passing it a code ref to do the editing).
    >
    >   BM> I have often thought Unix could do with flink(2) and mkstemp(2)
    >   BM> syscalls, that create a new link to an open fd and create a new file
    >   BM> with no name respectively. It would save a whole lot of mucking about
    >   BM> with temporary names.
    >
    > open can create temp files now (i think it uses file::temp
    > underneath). in slurp i take the old file name and append the pid to it
    > for the temp file. maybe i need to make that part more robust but no one
    > has mentioned it. one reason i don't use the open temp feature is that
    > slurp is backwards compatible to 5.005.
    >
    > uri
    >
    > --
    > Uri Guttman  ------    --------  http://www.sysarch.com--
    > -----  Perl Architecture, Development, Training, Support, Code Review  ------
    > -----------  Search or Offer Perl Jobs  -----http://jobs.perl.org ---------
    > ---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com---------


    Can some one tell the command line perl one-liner (windows xp) to
    replace a word with another with in the same file, with out backing up
    the file?

    C:\> perl -i~ -pe "s/bsmith/brsmith/g" test.txt
    is backing up the file.

    Thanks,
    J
     
    , Feb 7, 2008
    #12
  13. Ben Morrow Guest

    Quoth :
    >
    > Can some one tell the command line perl one-liner (windows xp) to
    > replace a word with another with in the same file, with out backing up
    > the file?
    >
    > C:\> perl -i~ -pe "s/bsmith/brsmith/g" test.txt
    > is backing up the file.


    Apply a small amount of imagination.

    perl -i~ -pe "s/bsmith/brsmith/g; END { unlink 'test.txt~' }'
    test.txt

    or more generally

    perl -i~ -pe "s/bsmith/brsmith/g; END { unlink map qq/$_$^I/, @ARGV }"
    test.txt

    Ben
     
    Ben Morrow, Feb 7, 2008
    #13
  14. Uri Guttman Guest

    >>>>> "JS" == Joe Smith <> writes:

    JS> wrote:
    >> Can some one tell the command line perl one-liner (windows xp) to
    >> replace a word with another with in the same file, with out backing up
    >> the file?


    JS> For arbitrary-length files on Windows, the answer is No.

    >> C:\> perl -i~ -pe "s/bsmith/brsmith/g" test.txt
    >> is backing up the file.


    JS> For files that can fit into memory:

    JS> C:\> perl -0le "$f=$ARGV[0];$_=<>;s/bsmith/brsmith/g;open F,'>',$f;print F $_" test.txt

    untested

    perl -MFile::Slurp -e '$f = shift ; $t = read_file $f; $t =~
    s/bsmith/brsmith/g; write_file $f, $t' test.txt

    when edit_file is added to file::slurp you will be able to do:

    perl -MFile::Slurp=edit_file -e 'edit_file shift, sub{ s/bsmith/brsmith/g }'

    maybe i will also add an edit_file script that takes a file name and
    code ref arg:

    edit_file 'sub{ s/bsmith/brsmith/g }' test.txt

    that looks kinda nice!

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Architecture, Development, Training, Support, Code Review ------
    ----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Feb 7, 2008
    #14
    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. Hugo
    Replies:
    10
    Views:
    1,320
    Matt Humphrey
    Oct 18, 2004
  2. kaushikshome
    Replies:
    4
    Views:
    775
    kaushikshome
    Sep 10, 2006
  3. scad
    Replies:
    23
    Views:
    1,174
    Alf P. Steinbach
    May 17, 2009
  4. Markus Dehmann

    Modify string on command line

    Markus Dehmann, Nov 22, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    87
    Lazrado Allan \(IFIN CSW WS MS\)
    Feb 3, 2006
  5. Ting Wang
    Replies:
    3
    Views:
    197
    Paul Lalli
    Dec 13, 2005
Loading...

Share This Page