Sed Range Issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed Range Issue
# 1  
Old 06-05-2008
Sed Range Issue

OK, so for a grand overview of what I'm trying to do:

I've got 2 files that are mostly like.
The file format is:

[descriptor1]
data
data
data
data

[descriptor2]
data
data
data
data

[descriptor3]
data
data

[descriptor4]
data
data

OK, so what I need to do is take all data from [descriptor2] and [descriptor3] sections from file1, remove like sections from file2, and then insert the stream from file1 in the right spot in file2. Fun, right?

Well, I'm new w/ sed, but that seems the best way to do this (correct me if I'm wrong). I've got some code that's working atm, however it's giving me the actual line [descriptor4] as that's the end of the range I'm using and I don't want it to include that in the section delete, how do I not make it do that?

sed '/\[descriptor2/,/\[descriptor4/!d' file1

Basically, I want the output to be:

[descriptor2]
data
data
data
data

[descriptor3]
data
data

And it's giving me all that, plus a [descriptor4] as that's where the range ends, but I'm not sure how else to define it.

Thank you for any light you can shed!
# 2  
Old 06-05-2008
Try this:

Code:
awk ' 
FNR==NR && /descriptor2/{f=1} 
FNR==NR && /descriptor4/{f=0} 
FNR==NR && f{arr[++i]=$0}
FNR==NR{next}
/descriptor2/{for(j=1;j<=i;j++){print arr[j]};f=1} 
/descriptor4/{f=0} 
!f{print}
' "file1" "file2"

Regards
# 3  
Old 06-17-2008
Code:
sed -e '/\[descriptor2/,/\[descriptor4/!d' -e '/\[descriptor4/d' file1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed to grab first instance of a range

Hi all, I'm new to the forum and also relatively new to sed and other such wonderfully epic tools. I'm attempting to grab a section of text between two words, but it seems to match all instances of the range instead of stopping at just the first. This occurs when I use: sed -n... (7 Replies)
Discussion started by: Lazarix
7 Replies

2. UNIX for Dummies Questions & Answers

Using sed to replace a range of number

Trying to use SED to replace numbers that fall into a range but can't seem to get the logic to work and am wondering if SED will do this. I have a file with the following numbers 3 26 20 5. For the numbers that are greater than zero and less than 25, SED would add the word range after the... (7 Replies)
Discussion started by: jimmyf
7 Replies

3. Shell Programming and Scripting

sed/awk date range?

Hi, I am trying to grep out a date range in an access log file. I defined the date like so; DATE1=$(date --date '1 hour ago' '+%m/%d/%y:%H:%M:%S') DATE2=$(date '+%m/%d/%y:%H:%M:%S') Then I just used cat to get the hits to the url into a results.txt; touch /tmp/results.txt cat... (7 Replies)
Discussion started by: Epx998
7 Replies

4. Shell Programming and Scripting

sed delete range

Hi I would like to delete ranges of text from an html file; In the sentence; aqua>Stroomprobleem in Hengelo verholpen <a href="107-01.html"><font color=yellow>107</a> With several sentences like this in that file, where the text between <a href a> varies, so it needs to be deleted in the... (2 Replies)
Discussion started by: mdop
2 Replies

5. Shell Programming and Scripting

how to use sed to get 2nd range?

Hi all, Is there a way to use the sed command to get the second range? For instance: sample.dat: ------------- 11111 22222 33333 44444 ------------- using "sed -n '/^ping/,/^$/p' sample.dat can get the first 2 lines. But is there a way to get the first 4 lines, from... (4 Replies)
Discussion started by: sleepy_11
4 Replies

6. Shell Programming and Scripting

sed pattern range

Hi guys, trying to replace a '#' with a ' ' (space) but only between the brackets '(' and ')' N="text1#text2#text3(var1#var2#var3)" N=`echo $N |sed '/(/,/) s/#. //'` echo $N Looking for an output of "text1#text2#text3(var1 var2 var3)" Any ideas? (15 Replies)
Discussion started by: mikepegg
15 Replies

7. Shell Programming and Scripting

Using REGEX within SED range search

test data: Code: sed -n '/^**$*/,/;/{/;/G;p;}' What i'm trying to do with the above regex (in bleu) identify upper/lower case select only when select is at the beginning of the line OR preceded by a space select is followed by a space or is at the end of the line. ... (13 Replies)
Discussion started by: danmauer
13 Replies

8. Shell Programming and Scripting

SED Newb - Range Rage!

Hi all First time poster long time lurker. I've been trying to get my head around SED but I think my beginner status is starting to prove too great a hindrence. I have nearly 100 CSS files that I need to modify in such a way that label, b, p .text{ some style stuff } would become b ,p... (9 Replies)
Discussion started by: GoneShootin
9 Replies

9. Shell Programming and Scripting

Sed Range command

Hi, I am trying to get all the log entries in a log file based on start and end times, I have tried to use the following but couldnt get the desired output: sed -n '/start/,/end/ p' file & newtime=`echo "$time" | cut -d -start | cut -d" " -start` Kindly help me, Thanks (0 Replies)
Discussion started by: openspark
0 Replies

10. Shell Programming and Scripting

Help needed in sed range pattern

Hi all, I am using sed for extracting the lines that occurs between the 2 patterns using the following command: sed -n '/pattern1/,/pattern2/' filename The above command has no problem and works fine. But I was wondering if there is a way to quit sed when it has extracted the range at... (3 Replies)
Discussion started by: sank
3 Replies
Login or Register to Ask a Question