sed help - nested pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed help - nested pattern
# 1  
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.
# 2  
Old 08-26-2011
Try:
Code:
awk -vs="$(cat file2)" '
/myBike == "Huffy"/ { change = 1 }
/return/ && change { change = 0; sub(/return.*$/, s) }1' INPUTFILE

# 3  
Old 08-26-2011
Thanks, it's close but it's replacing the Huffy information in both bikeModels and bikeColors. I just want to replace that information in whichever one I specify, in this case bikeModels. I've only used awk for very basic stuff but I'll play around with it for a little while and see if I have better luck than I have had with sed.
# 4  
Old 08-30-2011
Sorry to bump this up but the hurricane meant I wasn't able to check the forums for a few days. Just wondering if anyone else maybe had some insight as to how to go about solving my problem whether it's through sed or awk. Thanks.
# 5  
Old 08-30-2011
Just add one more check:
Code:
awk -vs="$(cat file2)" '                                                  
/bikeModels/ { change1 = 1}
/myBike == "Huffy"/ { change2 = 1 }
/return/ && change1 && change2 {
  change1 = change2 = 0
  sub(/return.*$/, s)
}
1' INPUTFILE

# 6  
Old 08-30-2011
Here's how it is done with sed...
Code:
sed '/def bikeModels/ {
   N
   /Huffy/ {
      H
      n
      /return/ {
         x
         r file2
         d
      }
   }
}' file1

# 7  
Old 08-30-2011
Quote:
Originally Posted by shamrock
Here's how it is done with sed...
Code:
sed '/def bikeModels/ {
   N
   /Huffy/ {
      H
      n
      /return/ {
         x
         r file2
         d
      }
   }
}' file1

Thanks so much for replying everyone.
I'm trying to use this command as one line without much luck (I keep getting the message "sed: -e expression #1, char 22: extra characters after command" no matter how I try to reformat the command).

I am entering the command as one line as follows:
sed '/def bikeModels/ { N /Huffy/ { H n /return/ { x r file2 d} } }' file1

Like I said in my post, I am EXTREMELY new to this and kind of got thrown into having to write this command so I'm trying to figure things out on the fly. Should I be able to one line it like that or am I totally doing something wrong? I have tried to move things around/remove spaces in the command but keep getting the same message but with different locations i.e. character 27.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question