perl/sed -i removes link


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users perl/sed -i removes link
# 1  
Old 05-08-2012
perl/sed -i removes link

hello,

is it a behavior of
Quote:
perl -i
or
Quote:
sed -i
that "-i" removes unix link .

example :

i create a file "src_file" and link it to "link_file" and then i start "perl -i"

Quote:
echo "AAA" > src_file

ln -s src_file link_file

ll
lrwxr-x--- 1 user root 8 May 8 14:20 link_file -> src_file
-rw-r----- 1 user root 4 May 8 14:20 src_file

perl -i -pne 's/AAA/BBB/g' link_file

ll
-rw-r----- 1 user root 4 May 8 14:21 link_file
-rw-r----- 1 user root 4 May 8 14:20 src_file

cat link_file
BBB
the link is removed. does another option exists to change content of a file without temporary files ?

UNIX-Version: HP-UX and i also tested it with Linux

regards
# 2  
Old 05-08-2012
You can use ed

Code:
ls -dl link.txt
lrwxrwxr-x   1 root       sys             11 May  8 14:50 link.txt -> example.txt
cat link.txt
AAA
ed -s link.txt <<EOF
s/AAA/BBB
w
EOF
BBB
cat link.txt
BBB
ls -dl link.txt
lrwxrwxr-x   1 root       sys             11 May  8 14:50 link.txt -> example.txt

# 3  
Old 05-08-2012
I have not tested the following, but it is a viable theory:

when sed (and presumably perl too) modifies a file it has to create an intermediate file. This is because of its modus operandi: it reads from the source file, modifies the read part and writes this modified version to the target file (screen or new file). This is why you usually do:

Code:
sed '<something>' /path/to/source > /path/to/target
mv /path/to/target /path/to/source

although this looks cumbersome. Notice that

Code:
 sed '<something>' /path/to/source > /path/to/source

won't work, because sed would read the first line, modify it, then write it back to the file it just read from and further tries to read from it now won't be successful any more.

This is why Linux (/GNU-) versions of sed and perl extended the options of the original sed to the "-i" switch, which does just that: automatically replacing the input file and making the second command variant working.

Still this doesn't change the working of sed and the trick with which the "-i" switch is implemented is to create a (hidden) temporary output file and move that over the original file after the modification. In fact "-i" is just automating the "mv"-command in my first example. If you start a long-running sed job you will probably find the intermediate file somewhere in /tmp, /var/tmp or some similar place while the job runs.

It may well be that this doesn't honor soft links because a new file is created and then moved in place, replacing the inode of the softlink.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed or awk removes attachment in email

Hi We have a requirement to send email using shell script.email should have html body and pdf attachment. We used uuencode for attaching files and sendmail option to acheive and it is working fine. However custoemr wants to make body of email slightly dynamic. E.g dear customer in html file... (3 Replies)
Discussion started by: Harish7586
3 Replies

2. Shell Programming and Scripting

Rewrite sed to perl or run sed in perl

I am having trouble re-writing this sed code sed -nr 's/.*del(+)ins(+).*NC_0{4}(+).*g\.(+)_(+).*/\3\t\4\t\5\t\1\t\2/p' C:/Users/cmccabe/Desktop/Python27/out_position.txt > C:/Users/cmccabe/Desktop/Python27/out_parse.txt in perl Basically, what the code does is parse text from two fields... (12 Replies)
Discussion started by: cmccabe
12 Replies

3. Solaris

How to link sed from /usr/bin/sed to /usr/local/bin/sed?

Hi Guys, OS:- Solaris 10 64Bit I have a small query. On one server a user is facing sed command issue. He gets error regarding sed for this location /users/hoy/2999/batch5/bin/internal.sh: /usr/local/bin/sed: not found How ever the sed is actually present at this location on server:-... (13 Replies)
Discussion started by: manalisharmabe
13 Replies

4. Shell Programming and Scripting

Sed: removes \ from text which causes issues

Hi all, Hoping someone hoping someone might be able to help. i've got the following sed command which i'm using in a bash script that i'm trying to use to insert a new line into an already existing file so i don't have to manually enter it when setting stuff up. the existing script test2/3 are... (3 Replies)
Discussion started by: springs2
3 Replies

5. UNIX for Dummies Questions & Answers

Sed to remove only first line erroneously removes last line too

Hello everyone, This is my first posting. I have read the rules of this forum. I have searched many various threads and haven't found one that applies to my situation or suggestions to fix the issue. I do appreciate the help. I am trying to execute a basic UNIX script in a Solaris... (4 Replies)
Discussion started by: dqrgk0
4 Replies

6. Shell Programming and Scripting

SED - Create mailto: link

Help! I am using sed to convert text files into easily viewed html tables. I have managed all except converting the email addresses to mailto: links. Multiple email addresses exist within the files, either preceded by a space or > (as part of HTML tag), and followed by either space or < I've... (5 Replies)
Discussion started by: Nigel_R
5 Replies

7. Shell Programming and Scripting

sed command removes lines in file

Hi, I am modifying a file with sed command. i want to make SCORE= blank in the file whereever SCORE=somevalue. What will *$ do in the below command? cat $file | sed 's/SCORE=.*$/SCORE=\r/g' > newfile The last line is also missing in the new file. How to make SCORE='100' to SCORE=... (5 Replies)
Discussion started by: ashok.k
5 Replies

8. Shell Programming and Scripting

Search Files from Array and link to original location in Perl

Hello, Question is related to Perl: I need to search few of the files from the array of file names. And after grepping the file names from an array I need to link these files to original location. The original location in this case is ref_path as input from the user. ##$ref_path is... (3 Replies)
Discussion started by: aarora1
3 Replies

9. UNIX for Dummies Questions & Answers

Providing a Link to Perl

I was wondering how I can create a link to the perl interpreter. On my old machine perl is at: user/local/bin/perl and my new machine has it at: user/bin/perl I don't want to have to change all my cgi scripts when I move them to the new server. Can I just create a new directory... (4 Replies)
Discussion started by: pingdom
4 Replies
Login or Register to Ask a Question