Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
Hi, Dieter5.
Apologies for the long post.
I think that we often see a tool and a few features, and we think it will fit for a solution to a problem. It's possible that we see, for example, that sed has a range selection for lines:
or
and we think that we can perform some task based on that. However,we may be over-looking how a stream editor works.
The sed command, in normal operation, reads a single line, and applies all the operations on that line that are appropriate. So, if an operation has a range selection that includes the current line, then the operation proceeds. When all the operations are completed, the line is written out, and the next line is read in, operated upon, etc. So there are constraints on what can be done, and certainly on what can be done easily.
On the other hand, awk is flexible tool that may seem like it takes longer to master, but most people who take that time find it to be well spent.
The one advantage that sed has over awk is in the area of efficiency -- sed is 1/6 the size of awk (machine efficiency), and simple substitutions are easily coded (human efficiency).
I don't know if the code from ygemici is the best sed solution, but it is certainly better than I could do without a lot of extra work.
A few simple awk rules in the core of the solution below, seem simple and straight-forward. I'm sure that the awk can be improved upon, but this solution works as it is.
produces, by inserting file data2 into data1 between the markers:
Quote:
"Different tools may rely on different assumptions about their
context--e.g., surrounding infrastructure, control model, data
model, communication protocols, etc.--which can lead to an
architectural mismatch between the application and the tools.
Such a mismatch leads to hacks and workarounds that will make the
code more complex than necessary."
-- 97 Things Every Programmer Should Know
"Sed has a few rudimentary programming constructs that can be
used to build more complicated scripts. It also has a limited
ability to work on more than one line at a time."
-- sed & awk, 2nd Edition
cheers, drl
Last edited by drl; 05-12-2010 at 01:23 PM..
Reason: ( Edit 1: typo, clarify )
I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Hello,
I'm an starter in Bash scripting. I would like to write a script in Bash shell that replaces a specific text block (a function) by another text block in a file:
for example in my file --> $HOME/myFile.js
replacing following function between other functions in the file:
function ABC()... (6 Replies)
I have a "main" file which has blocks of data for each user defined by tags BEGIN and END.
BEGIN
ID_NUM:24879
USER:abc123
HOW:47M
CMD1:xyz1
CMD2:arp2
STATE:active
PROCESS:id60
END
BEGIN
ID_NUM:24880
USER:def123
HOW:4M
CMD1:xyz1
CMD2:xyz2
STATE:running
PROCESS:id64
END (7 Replies)
My apologies if this has been answered in a previous post. I've been doing a lot of searching, but I haven't been able to find what I was looking for. Specifically, I am wondering if I can utilize sed and/or awk to locate two strings in a file, and replace everything between those two strings... (12 Replies)
Hello,
file1:
not to be changed
not to be changed
<start>
old stuff
old stuff
old stuff
<end>
not to be changed
not to be changed
file2:
new text
new text
desired output: (3 Replies)
I need to replace (delete) a text block in a bunch of files, its a html table, almost at the end of pages but the location varies.
In Windows I used Filemonkey, but nothing like that in Unix?
There is replace from mysql, but how does it deal with newlines?
sed only works with single lines,... (6 Replies)
I would perfer to use cut and paste to do this but I can't find a GUI to do this with. What I want to do is to find a multiple line block of code like
Exit Sub
Log_Handler:
then replace it with
GoTo RSLogRtn
Exit Sub
Log_Handler:
Basically it is just an insert, but I may want to... (8 Replies)
(1) Yes but how is this block different from the other 24? You will need this information in order to identify and replace this block correctly (out of the 25).
Ans: The 1st line and last line of this block are unique from other block.
The 1st line is “rem Subset Rows (&&tempName.*) and
The... (1 Reply)
Hello,
I really would appreciate some help with a bash script for some string manipulation on an SQL dump:
I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump.
This is quite easy with sed:
sed -e... (1 Reply)
Hi, how would we replace a few patterns on the same line with a change of it's own..using visual block on vim editor.
ie a file contains lines such as the following:
abccss (dfrss)) emailkk
abdcss (dfrss)) dmailkk
Using visual block once, replacement is indeed to get the following output:... (0 Replies)