Sed question


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Sed question
# 8  
Old 10-15-2007
Quote:
Originally Posted by bhargav
Code:
sed 's/\".*//g' test

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??
# 9  
Old 10-15-2007
Quote:
Originally Posted by cronjob78
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??
in-file editing

"-i" option of sed ( only with GNU sed )
# 10  
Old 10-15-2007
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".)

cheers, drl
# 11  
Old 10-15-2007
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??
# 12  
Old 10-17-2007
Quote:
Originally Posted by cronjob78
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:

Code:
sed '/start/,/end/ {
    s/this/that/
    }'

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:

Code:
blabla <script> function v470 blabla </script>

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):

Code:
blabla <script> function v470 blabla </script> blabla

blabla <script> function v470
blabla
blabla
bla </script> blabla

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):

Code:
/<script> function v470/,/<\/script>/ {
       /<script> function v470.*<\/script>/ {
               s/<script> function v470*.<\/script>//
               s/^/@@@@/
       }
       /<script> function v470/ {
               s/<script> function v470*.//
               s/^/@@@@/
       }
              /<\/script>/ {
               s/.*<\/script>//
               s/^/@@@@/
       }
              /@@@@/ ! {
               d
       }
       s/^@@@@//
}

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.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

SED question

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)
Discussion started by: jmack56
1 Replies

2. Shell Programming and Scripting

Sed question

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)
Discussion started by: beppler
10 Replies

3. Shell Programming and Scripting

sed question

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)
Discussion started by: melanie_pfefer
4 Replies

4. Shell Programming and Scripting

Sed Question 1. (Don't quite know how to use sed! Thanks)

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)
Discussion started by: beibeiatNY
1 Replies

5. UNIX for Dummies Questions & Answers

sed question

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)
Discussion started by: orahi001
3 Replies

6. Shell Programming and Scripting

sed question

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)
Discussion started by: arushunter
2 Replies

7. Shell Programming and Scripting

sed question

Hi, :) can any body explain the following statement sed 's/\(\)- ]//g' cheers RRK (3 Replies)
Discussion started by: ravi raj kumar
3 Replies

8. Shell Programming and Scripting

sed question

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)
Discussion started by: c19h28O2
5 Replies

9. Shell Programming and Scripting

sed question (again)

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)
Discussion started by: ahtat99
3 Replies

10. Shell Programming and Scripting

Sed Question

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)
Discussion started by: YS2002
1 Replies
Login or Register to Ask a Question