Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sed editing files: better way? 2

Status
Not open for further replies.

Ghodmode

Programmer
Feb 17, 2004
177
NZ
Is there a better way to do this? ...

Code:
for FILE in *.pl; do
    echo $FILE
    sed $FILE -e 's!c:\\xampp\\xampp\\kqsb!/opt/lampp/kqsb!g' > $FILE.tmp
    mv $FILE.tmp $FILE
    chmod 755 $FILE
done

What I'm hoping to find is a way to not create that temporary file and possibly retain the permissions before it was changed.

Thanks,

--
-- Ghodmode
 
You may be lucky and have the GNU version of sed that supports the -i (edit-in-place) option (check the man page).

An alternative is to use perl -spi.bak 's/water/wine/g' *.pl, for example.

Annihilannic.
 
Hi

Newer GNU [tt]sed[/tt]s, for example version 4.1.2 used by me can do it :
man sed said:
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)

In fact, with the -i option [tt]sed[/tt] will do the same thing of using a temporary file. The problem is, that if the input file is a symlink, then will be replaced with a file.

Feherke.
 
possibly retain the permissions
for FILE in *.pl; do
echo $FILE
cp $FILE $FILE.tmp &&
sed $FILE.tmp -e 's!c:\\xampp\\xampp\\kqsb!/opt/lampp/kqsb!g' > $FILE &&
rm $FILE.tmp
done

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I checked the man page and I do have the -i option [smile].

I'll use that.

However, I may eventually do a Perl script that does changes like this as part of an installation process.

I'm curious about the Perl method. The posted command doesn't do anything but generate an error message, but that may be due to a typo. Getting into a discussion about this Perl method is outside the scope of this forum, so I'll post another question to the Perl forum in the future.

perl -spi.bak 's/water/wine/g' *.pl
Can't open perl script "s/water/wine/g": No such file or directory

--
-- Ghodmode
 
Stars to both feherke and PHV. I forgot about the permissions thing myself [surprise], but I understand why PHV's method works: creating a new file (with [tt]mv[/tt]) uses the umask for it's permissions, but overwriting an existing file keeps the permissions that the file already had.

PHV's method is also safer because each step is only performed if the previous step was successful.

I've experimented with this and [tt]sed -i[/tt] also modifies the existing file rather than creating a new file.

--
-- Ghodmode
 
Ghodmode, it's my understanding that mv preserves the original permissions, is this incorrect?
 
The mv step *does* retain the permissions, however when a new file is created using sed script file.moved > file it is effectively creating a new one with default permissions.

I should probably have mentioned that the perl -i.bak option makes it keeps a backup copy of the file with the extension .bak. You can of course choose another extension, or leave it blank in which case no backup copy will be kept.

Annihilannic.
 
Hi

Ghodmode said:
I've experimented with this and sed -i also modifies the existing file rather than creating a new file.
No, [tt]sed[/tt] creates a new file.
Code:
[blue]master #[/blue] date > testfile
[blue]master #[/blue] ls -i testfile 
1214246 testfile
[blue]master #[/blue] sed -i 's/[[:digit:]]//g' testfile 
[blue]master #[/blue] ls -i testfile 
1214247 testfile

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top