Created a shell script to invoke sed to edit multiple files, but am missing something.
Here's the shell script:
As you can see, I'm using a script file 'script.sed' to supply the actual editing commands:
And for testing purposes, I have one .sh script to be edited:
But when I execute edit_scripts.sh, sed_sample.sh is not modified:
Created a shell script to invoke sed to edit multiple files, but am missing something.
Here's the shell script:
The problem is in this line:
First, sed doesn't need a redirection because it can work on files. Notice that i quoted "$i" because otherwise files with blanks in their names would break your script. It is generally a good idea to religiously quote whatever is quotable in shell scripts - it just comes with the impulse to convert the infidels, like i am doing to you right now. ;-)
Second, sed puts it output to <stdout> as per default. Probably it did do the changes but they were shown on your screen, not your file. To make the changes lasting you need to redirect this output into a (new!) file and only then move that file over the old one. As an additional measure of caution only do so if sed returned 0. This guards against i.e. disk-full-conditions where the output cannot be written (fully) to the disk and you move then a corrupt script over a perfectly fine template:
The first example worked for me, even though the man page for sed on my system does not list the '-i' switch. My system is running Oracle Linux, which is derived from RHEL.
The first example worked for me, even though the man page for sed on my system does not list the '-i' switch. My system is running Oracle Linux, which is derived from RHEL.
It does so for a reason because it is not advisable to use the "-i" switch at all, even in the few versions of sed not adhering to the standards which have it, because the the standards also do NOT have the switch at all.
The reason is that "behind the curtain" the "-i" switch does the same as i said: creating a temporary file and then copying it over the original. It does NOT really do "in-place-editing" as the documentation says, it does in fact the same every script programmer does. This in itself is not bad but: if you do it yourself you have control over where the temporary file goes and that it is removed afterwards. With the "-i" switch you don't have this control and in case of an error it may well be that it is left stuck somewhere and such a leftover chunk of junk is filling up valuable disk space.
Furthermore it changes file permissions and ownership, which a real in-place-editing wouldn't do. Consider the following example:
That means you can edit script.original because you are in the group "users" which has write privilege.
Now run this - and guess, what:
Did you notice? The ownership is changed! Now, that happens when you create a temporary file too, but there you are aware that you are dealing with a new file. Here you have no reason to suspect because you have been (wrongly) told it will be the same file plus your changes. It isn't. It will have a new inode number, it will be located somewhere else on the disk physically, etc.. This is why you should stay away from this switch. Have i mentioned that it doesn't conform to the standard either? If you want to create portable script which will not work somewhere but mysteriously not so somewhere else you better adhere to the standards slavishly. They define what you can rightfully expect from a system. If you expect more than chances are you will be disappointed sooner or later.
I hope this helps.
bakunin
These 3 Users Gave Thanks to bakunin For This Post:
I have 4000 files like
$cat clus_grp_seq10_g.phy
18 1002
anig_OJJ65951_1 ATGGTTTCGCAGCGTGATAGAGAATTGTTTAGGGATGATATTCGCTCGCGAGGAACGAAGCTCAATGCTGCCGAGCGCGAGAGTCTGCTAAGGCCATATCTGCCAGATCCGTCTGACCTTCCACGCAGGCCACTTCAGCGGCGCAAGAAGGTTCCTCG
aver_OOF92921_1 ... (1 Reply)
I have 6 different pipe delimiter files. My loads failing due to missing company code.
File1: 31 st field is company code.
402660076310|2014-12-10 17:22:39|2280361|MRYKI|1||CA|92507|US||1|1|0|0|0||N|A1|ONT|1001891771660009250700402660076310|WM|0201|RALA |2014-12-12|5|2014-12-12|5||FRI - 12... (4 Replies)
What is the syntax to use multiple input files in a SED command. i.e. substitute a word with a phrase in every file in a directory.
for every file in /usr/include that has the word "date" in the file
grep -l '\<date\>' /usr/include/*.h
find each occurrence of the word "time" in the file &... (3 Replies)
Hi guys,
say I have a few files in a directory (58 text files or somthing)
each one contains mulitple strings that I wish to replace with other strings
so in these 58 files I'm looking for say the following strings:
JAM (replace with BUTTER)
BREAD (replace with CRACKER)
SCOOP (replace... (19 Replies)
Hi all,
I would like to simply read a file which lists a number of pathnames and files, then search and replace key strings using a few vi commands:
:1,$s/search_str/replace_str/g<return>
but I am not sure how to automate the <return> of these vis commands when I am putting this in a... (8 Replies)
Hello all,
Search & replace works fine using sed on a single file.
Ex:
sed -i 's/day/night/g' test1.sh
There are many *.sh files in my current directory that I would like use sed on. I tried running the sed command using wild card but it did not work.
sed -i 's/day/night/g' *.sh
... (7 Replies)
Hi,
I want to do a search and replace on multiple text files. Can I use sed to do this?
i.e. I want to do something like:
$ sed *.html -e 's/<\/body>/<\!-- blah -->\n<\/body>/' | grep -1 body
... then pipe/ channel the results back into the same files that were searched.
how would... (2 Replies)
i have a command like :
sed -n 's/^* /&/w even' <file
if i want to write to multiple files like
sed -n 's/^* /&/w zero two three' < file
its not working it is taking "zero two three" as a single file i want to write to 3 seperate files . pls can anyone help me (2 Replies)
Here's what I have...
$ vi foo1
- open foo1 and work around for a while. I yank a few lines into a buffer and then :w to save.
Next I :e foo2 to open foo2 and paste my buffer. I :w to save, but I would like to then be able to go directly back into foo1 where I was before I opened foo2. ... (4 Replies)
I've got a bunch of files (40 or so) and in each there is a substitution I need to perform.
I execuet the following sed command but it just make the changes to the screen without affecting the original file.
sed "s/, LA/,LA/g" *
(All files in the directory need this update).
So I tried... (4 Replies)