Is there anyway (perferably in one command line) that you can manipulate the file itself rather than outputting to a temporary file and then renaming it??
Is there anyway (perferably in one command line) that you can manipulate the file itself rather than outputting to a temporary file and then renaming it??
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
Hi.
Note than "-i" really refers to the result, not the operational characteristics; a temporary file is still created. Think of it as a convenience, not as an option to save space:
Quote:
`-i[SUFFIX]'
`--in-place[=SUFFIX]'
This option specifies that files are to be edited in-place. GNU
`sed' does this by creating a temporary file and sending output to
this file rather than to the standard output.(1).
This option implies `-s'.
When the end of the file is reached, the temporary file is renamed
to the output file's original name.
-- excerpt from info sed
(I think I would have written that last part as "... to the input file's original name".)
Thanks very much for all the replies. That -i instruction is very useful.
My task has grown another leg. This is what I have now
sed -i '/<script>function v470/,/<\/script>/ s/<script>.*/<\/body>/' filename
this will replace a line that starts with
<script>function v470......and ends with.....</script></body>
and replace it with just
</body>
BUT SOMETIMES the line I want to replace DOES NOT end with </body>
and therefore I just want to blank/delete the line and not insert an unnecessary </body>
sed -i '/<script>function v470/,/<\/script>/ s/<script>.*//'
This will work but can I do it all together in one line for both cases?
i.e. if a line starts with <script> can I delete it as far as </script> so that it doesn't erase the (occasional) trailing </body> if it exists??
sed -i '/<script>function v470/,/<\/script>/ s/<script>.*//'
To explain, what you'll need to do I'll start by explaining what your script really does. Your code, reformatted, is like:
sed does the following: it searches the file, line by line, until it finds a line containing "start". Now it applies to this and every following line the commands between "{" and "}", a so-called "rule". In this case this is a single command: "s/this/that/", which changes the first occurence of "this" in a line to "that" . sed does this until it encounters a line containing "end", where it will stop applying the rule until it again finds a line containing "start", where it will again apply the rule to every line until finding one containing "end", and so on.
So much for the general case, back to your problem: your sed-line states that you search for a line containing "<script> function v470" (your "start"-clause) and from there on up to a line containing "</script>" (your "end"-clause) you apply the rule to replace "<script>.*", which means "everything including '<script>' to the end of the line" with nothing - in effect deleting "<script>" and everything following it on the same line.
Is this really what you want?
If you want to delete all your "<script> function v470" up to where you encounter "</script> you will have to work differently, because in the way you stated it you will only achieve what you ant to achieve when the <script>- and the </script>-clause appear on the same line:
On such a line your script will work. But on the following text fragments (i mark blue what i presume you would like to cut out):
it will fail.
In this case you have three different types of lines:
1) Lines which contain the start-clause and (maybe the end-clause)
2) Lines between type-1-lines and type-3-lines
3) Lines which only contain the end-clause (and maybe the start-clause)
Type-2-lines are the easiest: they can be deleted. Type-1-lines will have to deal with the special case where start- and end-clause are on the same line and hence Type-3-lines are reduced to a simple solution: cut everything out up to the end-clause. The type-1-lines we will split because we can match our special case (<script> and </script> on the same line) pretty easily.
(in the following i omit the sed-call, only giving the sed-script itself):
In detail: the first clause will make sure we work only on lines containing "<script> function v470" and all following lines up to a line containing "</script>".
On lines containing our start-clause AND end-clause we will snip out everything beginning with the <script>-tag and the </script>-tag. This has to be the first rule to make sure the next rule only has to deal with lines only containing the start-clause. On lines containing only the start-clause we will snip out everything from the start-clause to the end of line. On lines containing only the end-clause we will snip out everything from the beginning of line up to the end-clause. All the lines dealt with this way we will mark with four "@"-chars at the beginning to tell ourselves later on that we have already dealt with this line. Other lines we encounter will be lines between a start- and an end-clause but containing neither of then themselves. We simply delete them. This is where we need the markers: otherwise we would delete the lines we have already worked on here.
At last we remove the markers again and are finished.
I am trying to write a script that will take an input text file in the format
person: place: phonenumber;
person: place: phonenumber;
person: place: phonenumber;
...
and output it using sed too:
Name ######## Location ######### Phone Number... (1 Reply)
I need to replace the numbers with a new string.
How can I give a wildcard for the different # of numbers
sed '/abcdef/s/abcdef=*/abcdef=999999/'<foo>foo1
From: To:
abcdef=1234 abcdef=999999
abcdef=12345 abcdef=999999
abcdef=123456... (10 Replies)
hi
i have a file with this line:
variable=/export/home/oracle
I want to change the file so that the path is replaced with the value of another variable
var2=/tmp/anything.
how to do this in sed?
thx (4 Replies)
Write a sed script to extract the year, rank, and stock for the most recent 10 years available in the file top10_mktval.csv, and output in the following format:
------------------------------
YEAR |RANK| STOCK
------------------------------
2007 | 1 | Exxon... (1 Reply)
How would I use sed to print everything on the line after the regular expresion?
I have a configuration file setting several variables.
cfg.dat
DDB = cpptest
SUDBNAME = sucpptestdb
host = cpptest
Example
I want to search for the regular expresion 'SUDBNAME =' and print everything on... (3 Replies)
I have a file that conatins following info
Policy1=U|guestRoom=test1idCode=5(1):!:Amenity2=U|RoomId=testrma=4(1):!:|
GuestRoomAmenity1=U|guestRoomId=testguest1id^rmaCode=5(1):!:|
I need it to look like this
Policy1=U|guestRoom=test1idCode
Amenity2=U|RoomId=testrmaCode... (2 Replies)
Hi,
When deleting lines using sed, as i understand the lines are redirected to the standard output. What i'm unclear about is how to actually modify the file?
If I write the command sed '1,2d' test it will display lines one and 2 onto the screen however the file is not modified? I think my... (5 Replies)
hello there, I have a sed question.
I have a file (temp.srv), in it it has
v1_host1
v2_host2
And I have another file (temp2.srv), in it is has
v1_host3_date
v1_host1
v2_host2
v2_host4_date
v3_host5_date
I had used a script to remove the name from temp2.srv base on the name inside... (3 Replies)
Hi,
Is there any way to traverse the file once and look for the following conditions in one sweep instead of going over the file 3 times with different search criteria......
sed -n '/^ORA-07445/ p' /tmp/t$$ > ${OERRFILE}
sed -n '/^ORA-00600/ p' /tmp/t$$ >> ${OERRFILE}
... (1 Reply)