To modify a file from the command line

C

clearguy02

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
 
C

clearguy02

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
 
J

Joost Diepenmaat

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.
 
C

clearguy02

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
 
J

Joost Diepenmaat

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.
 
B

Ben Morrow

Quoth Joost Diepenmaat said:
Oh right, I missed that part.


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
 
C

clearguy02

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
 
U

Uri Guttman

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
 
C

clearguy02

  BM> Quoth Joost Diepenmaat <[email protected]>:
  >> (e-mail address removed) writes:
  >>
  >> > 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  ------  (e-mail address removed)  --------  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
 
B

Ben Morrow

Quoth Uri Guttman said:
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
 
U

Uri Guttman

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.

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
 
C

clearguy02

  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  ------  (e-mail address removed)  --------  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
 
B

Ben Morrow

Quoth (e-mail address removed):
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
 
U

Uri Guttman

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

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,076
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top