Sponsored Content
Full Discussion: sed help - nested pattern
Top Forums Shell Programming and Scripting sed help - nested pattern Post 302550438 by planetary12 on Thursday 25th of August 2011 09:39:52 PM
Old 08-25-2011
sed help - nested pattern

Hello,

I am very new to Unix and using sed so I'm struggling a little bit with this command and was hoping someone could help me out.

I want to find a nested pattern in a file and replace some text in that file with text from another file.

For example, in file one I have something like this:
Code:
def bikeModels(myBike = getBike())
if myBike == "Huffy":
return ["H1","H2","H3"]
elif myBike == "Mongoose":
return ["M1","M2","M3"]
***Blank line separates these two sections*** def bikeColors(myBike = getBike())
if myBike == "Huffy":
return ["Black","Blue"]
elif myBike == "Mongoose":
return ["Red","White"]

In my file two all I have is a new set of Huffy models:
Code:
return ["H4","H5","H6"]

which I want to put into file one, replacing what was originally being returned if myBike == "Huffy" under bikeModels to get a file that looks like this:

Code:
def bikeModels(myBike = getBike())
if myBike == "Huffy":
return ["H4","H5","H6"]
elif myBike == "Mongoose":
return ["M1","M2","M3"]
def bikeColors(myBike = getBike())
if myBike == "Huffy":
return ["Black","Blue"]
elif myBike == "Mongoose":
return ["Red","White"]

I can use sed to find the chunk of the file I want with a command like:
sed -n '/bikeModels/,/^$/ {myBike == "Huffy"/,/]/p}; file1

but I have no idea how to replace that text with the text from file two.

I did see I could use:
sed -e '/regex/{r insert.file' -e 'd;}' source
but, again, I have no idea how to combine everything I want to do and have it work.

Thanks to anyone that can help. Let me know if I need to go into more detail or explain anything better.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

2. UNIX for Dummies Questions & Answers

Bourne-sh (not bash) question about nested loops and sed

Here's the input: alpha, numeric or alphanumeric string ("line 1 string") numeric string ("line 2 string") numeric string ("line 3 string") numeric string ("line 4 string") ... where - each numeric string is in a pattern that can be matched with RE but - there can be any number of... (2 Replies)
Discussion started by: uiop44
2 Replies

3. Shell Programming and Scripting

Complex bash/sed, variables and nested quotes

Ok, this one isn't for everybody, it's pretty tough and I've spent a good deal of time on it without figuring it out yet. Can anybody get this script to work: #!/bin/bash cq_fname="%let outputfile="/user/cq_"$1".csv";" sed "29s/.*/\"$cq_fname\"/" file1.sas >... (3 Replies)
Discussion started by: nocloud
3 Replies

4. Homework & Coursework Questions

Sed, matching nested brackets and deleting

1. The problem statement, all variables and given/known data: I have to write a script using sed, which delete everything between curly brackets and the brackets themself. The brackets might be nested. The input-file is: aaa { bbb ccc { ddd eee } fff { ... (2 Replies)
Discussion started by: FuzzyGnome
2 Replies

5. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

6. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

7. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

8. Shell Programming and Scripting

Nested sed commands

Hi, I get the following response by gphoto2 and I would like to substract the index number of the current item. In this case 3. gphoto2 --get-config /main/imgsettings/iso Label: ISO Speed Type: RADIO Current: 200 Choice: 0 100 Choice: 1 125 Choice: 2 160 Choice: 3 200 Choice: 4 250 ..... (11 Replies)
Discussion started by: Nic2015
11 Replies

9. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

10. Shell Programming and Scripting

[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below. 2018.07.01, Sunday 09:27 some text 123456789 0 21 0.06 0.07 0.00 2018.07.02, Monday 09:31 some text 123456789 1 41 0.26 0.32 0.00 09:39 some text 456789012 1 0.07 0.09 0.09 09:45 some text 932469494 1 55 0.29 0.36 0.00 16:49 some text 123456789 0 48 0.12 0.15 0.00... (9 Replies)
Discussion started by: father_7
9 Replies
dump(n) 																   dump(n)

__________________________________________________________________________________________________________________________________________________

NAME
dump - Dump information about Tcl interpreter in TkCon SYNOPSIS
dump method ?-nocomplain? ?-filter pattern? ?--? pattern ?pattern ...? _________________________________________________________________ DESCRIPTION
The dump command provides a way for the user to spit out state information about the interpreter in a Tcl readable (and human readable) form. It takes the general form: dump method ?-nocomplain? ?-filter pattern? ?--? pattern ?pattern ...? The patterns represent glob-style patterns (as in string match pattern $str). -nocomplain will prevent dump from throwing an error if no items matched the pattern. -filter is interpreted as appropriate for the method. The various methods are: dump command args Outputs one or more commands. dump procedure args Outputs one or more procs in sourceable form. dump variable args Outputs the values of variables in sourceable form. Recognizes nested arrays. The -filter pattern is used as to filter array ele- ment names and is interepreted as a glob pattern (defaults to {*}). It is passed down for nested arrays. dump widget args Outputs one or more widgets by giving their configuration options. The -filter pattern is used as to filter the config options and is interpreted as a case insensitive regexp pattern (defaults to {.*}). SEE ALSO
idebug(n), observe(n), tkcon(1), tkcon(n), tkconrc(5) KEYWORDS
Tk, console, dump COPYRIGHT
Copyright (c) Jeffrey Hobbs <jeff at hobbs.org> TkCon 2.5 dump(n)
All times are GMT -4. The time now is 07:25 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy