Using Sed to duplicate a section of a file....


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using Sed to duplicate a section of a file....
# 1  
Old 04-29-2008
Using Sed to duplicate a section of a file....

hello all,
I have a file like this:
Code:
section 1
blah1
blah2
section 2
blah1
blah2
section 3
blah1
blah2

and I want to use sed to duplicate section 2, like this:
Code:
section 1
blah1
blah2
section 2
blah1
blah2
section 2
blah1
blah2
section 3
blah1
blah2

I've figured out that I can do this:
Code:
sed -n '/section 2/,/section/{ p }' input_file

and it will return:
Code:
section 2
blah1
blah2
section 3

but I can't get it to drop the last line, and I can't get my regexp to say grab UNTIL you get to the word section, since thats marking the line. Help please, I've been reading the sed tutorials and man pages endlessly and now I'm just going in circles.

Last edited by nick26; 04-29-2008 at 09:23 AM.. Reason: spacing
# 2  
Old 04-29-2008
The real hurdle is in expressing more complex logic than "from here through to here". But sed is a programming language, just rather primitive, and very terse (or "succinct", or "unreadable", depending on how you look at it).

So to paraphrase, between section 2 and section 3, you want to print, but not if it's section 3. If expressed this way, it's easy (just not very elegant):

Code:
sed -n '/section 2/,/section 3/{;/section 3/!p;}' input_file

Incidentally, in your original script, the braces were not necessary, but now they are, because there's a block of multiple things we want to happen when the first condition matches.

Just to complete it, why don't you collect section 2 into the hold space, and print it out a second time when you see the section 3 heading:

Code:
sed '/section 2/,/section 3/{;/section 3/!{;H;b;};x;D;p;x;}' input_file

So if we are between section 2 and section 3 (inclusive); then if we are not looking at section 3, append to hold space, and skip to the end of the current script (where the line is implicitly printed the first time); else, recall what's in the hold space, remove the first item (which is empty, because we have been appending to the hold space, and that's what it originally contained), print, and recall back the earlier pattern space (which is the section 3 heading; again, at the end of the script, that line is printed.

This would be way more readable and maintainable in awk or Perl, mind.
# 3  
Old 05-01-2008
Code:
awk '
/section 2/ {f=1}
/section 3/{
  f=0; 
  for(i=1;i<=c;i++) print a[i]  
}
f{ print; a[++c]=$0 }
!f{ print}
' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Section Removal With sed; and With a Twist . . .

Hello folks! Raised a bump on my head trying to figure this one out ;) I have an xml file which needs to be edited, removing an entire property section in the work. Here's what the target section layout looks like: <property name="something"> {any number of lines go here} </property>... (7 Replies)
Discussion started by: LinQ
7 Replies

2. UNIX for Advanced & Expert Users

How to identify particular section using sed?

Hi, I have following data in a file. Not all but most of the lines start with letter 'T' has 8 SPACES from column 121 to 128 and I want to replace that portion with some dummy value. Is it possible through sed? Input File:- T1111111111111A 20140310000000005076358416369283 AAAAA ... (6 Replies)
Discussion started by: jnrohit2k
6 Replies

3. Shell Programming and Scripting

sed - String substitution within specified section in ini type file

Hello. I am trying to modify a config file which is in windows *.ini type file. I have found a piece of code here :linux - Edit file in unix using SED - Stack Overflow As I can't make it doing the job , I am trying to find a solution step by step. here a modified sample file : my_sample.ini... (1 Reply)
Discussion started by: jcdole
1 Replies

4. Shell Programming and Scripting

Fetch a section from a file

Hi, I have a file like... $cat file1 +++++++++++++++++++ client1 +++++++++++++++++++++++++++++ col1 col2 col3 ------ ----- ----- (0 rows affected) ========================================================= +++++++++++++++++++ client1 +++++++++++++++++++++++++++++ col1 col2 col3... (6 Replies)
Discussion started by: sam05121988
6 Replies

5. Shell Programming and Scripting

Delete a section of a file if...

i have a file as below that has n section : 2006 0101 1236 49.3 L 37.902 48.482 0.0 Teh 5 0.2 2.7LTeh 1 GAP=238 E Iranian Seismological Center, Institute of Geophysics, University of Tehran 6 ... (5 Replies)
Discussion started by: oreka18
5 Replies

6. Shell Programming and Scripting

Extract section of file based on word in section

I have a list of Servers in no particular order as follows: virtualMachines="IIBSBS IIBVICDMS01 IIBVICMA01"And I am generating some output from a pre-existing script that gives me the following (this is a sample output selection). 9/17/2010 8:00:05 PM: Normal backup using VDRBACKUPS... (2 Replies)
Discussion started by: jelloir
2 Replies

7. Shell Programming and Scripting

Duplicate Line Report per Section

I've been working on a script (/bin/sh) in which I have requested and received help here (in which I am very grateful for!). The client has modified their requirements (a tad), so without messing up the script to much, I come once again for assistance. Here are the file.dat contents: ABC1... (4 Replies)
Discussion started by: petersf
4 Replies

8. Shell Programming and Scripting

Placing Duplicate Lines per section into another file

Hello, I need help in putting duplicate lines within a section into another file. Here is what I'm struggling with: Using this file “data.txt”: ABC1 012345 header ABC2 7890-000 ABC3 012345 Header Table ABC4 ABC5 593.0000 587.4800 ABC5 593.5000 587.6580 <= dup need to remove ABC5... (4 Replies)
Discussion started by: petersf
4 Replies

9. Shell Programming and Scripting

Removing Duplicate Lines per Section

Hello, I am in need of removing duplicate lines from within a file per section. File: ABC1 012345 header ABC2 7890-000 ABC3 012345 Header Table ABC4 ABC5 593.0000 587.4800 ABC5 593.5000 587.6580 <= dup need to remove ABC5 593.5000 ... (5 Replies)
Discussion started by: petersf
5 Replies

10. Shell Programming and Scripting

sed & awk--get section of file based 2 params

I need to get a section of a file based on 2 params. I want the part of the file between param 1 & 2. I have tried a bunch of ways and just can't seem to get it right. Can someone please help me out.....its much appreciated. Here is what I have found that looks like what I want....but doesn't... (12 Replies)
Discussion started by: Andy Cook
12 Replies
Login or Register to Ask a Question