using sed/awk to replace a block of text in a file? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

using sed/awk to replace a block of text in a file?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-29-2011
kiddsupreme kiddsupreme is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 12 December 2011, 2:50 PM EST
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
using sed/awk to replace a block of text in a file?

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 (including the strings themselves) with new text.

For example, lets say I have a file with the following text:


Code:
"The quick brown fox jumps over the lazy dog" is an English-language program, that is, a phrase that contains all of the letters of the alphabet. It has been used to test typewriters and computer keyboards.

And I want it to read like this:


Code:
"Then the quick onyx goblin jumps over the lazy dwarf" is an English-language program, that is, a phrase that contains all of the letters of the alphabet. It has been used to test typewriters and computer keyboards.

How do I go about replacing
Code:
"The quick brown fox jumps over the lazy dog"

with
Code:
"Then the quick onyx goblin jumps over the lazy dwarf"

? Any assistance would be appreciated. Thanks in advance.
Sponsored Links
    #2  
Old 11-29-2011
gowtham.varma gowtham.varma is offline
Registered User
 
Join Date: May 2011
Last Activity: 15 April 2014, 8:23 AM EDT
Posts: 25
Thanks: 3
Thanked 5 Times in 5 Posts
this can help you..
Quote:
sed 's/The quick brown fox/Then the quick onyx goblin/g' <filename
Sponsored Links
    #3  
Old 11-29-2011
ahamed101's Avatar
ahamed101 ahamed101 is offline Forum Advisor  
root is god!!!
 
Join Date: Sep 2008
Last Activity: 17 April 2014, 8:24 AM EDT
Location: San Jose, CA
Posts: 1,906
Thanks: 54
Thanked 485 Times in 478 Posts
Like this?

Code:
sed 's/The/Then the/;s/brown fox/onyx goblin/;s/dog/dwarf/' input_file

--ahamed
    #4  
Old 11-29-2011
kiddsupreme kiddsupreme is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 12 December 2011, 2:50 PM EST
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Thank you for the replies. There was one thing though that I may have not been clear about. There would be times where everything in the middle of the two strings could be different. For example:


Code:
The quick brown fox.... dogs

or
Code:
The time to feed the .... dogs

or
Code:
The man ran for his life away from the dogs

In each case, what was between the words "The" and "dogs" is fluid. It could possibly be anything, and I wouldn't know beforehand. I need a mechanism that would look for the word "The" and the word "dog" and replace everything in between (including the word The and dog) with something else.

Does that make sense? If need be, I can give you a more specific example of what I am working with (a CSV) and what I am trying to do. Thanks again.
Sponsored Links
    #5  
Old 11-29-2011
CarloM CarloM is offline
Registered User
 
Join Date: Oct 2011
Last Activity: 16 April 2014, 8:57 AM EDT
Location: London, UK
Posts: 929
Thanks: 72
Thanked 213 Times in 200 Posts
Try

Code:
sed 's/The.*dog/whatever/' inputfile

EDIT: Note that .* will match the longest available string though, e.g.

Code:
# echo "The quick brown fox fell over the dog" | sed 's/The.*dog/whatever/'
whatever
# echo "The quick brown fox fell over the dog and some other dogs and cats" | sed 's/The.*dog/whatever/'
whatevers and cats

The Following User Says Thank You to CarloM For This Useful Post:
kiddsupreme (11-30-2011)
Sponsored Links
    #6  
Old 11-29-2011
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 16 April 2014, 10:12 PM EDT
Location: India
Posts: 1,796
Thanks: 23
Thanked 516 Times in 495 Posts
Try the following to replace whatever you want in between "The" and the first occurrence of "dog":

Code:
# echo "The quick brown fox fell over the dog and some other dogs and cats" | perl -e '$x=<>;$x=~s/The.+?dog/whatever/;print $x'
whatever and some other dogs and cats

The Following User Says Thank You to balajesuri For This Useful Post:
kiddsupreme (11-30-2011)
Sponsored Links
    #7  
Old 11-30-2011
kiddsupreme kiddsupreme is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 12 December 2011, 2:50 PM EST
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
I've tried some suggestions with the real case I have, but I haven't had any success. Perhaps if I show you what I am working with it will make more sense. The following is a CSV file that I am working on:


Code:
 eDir Top N Report , logoRpt 
 All LAN Interface for ACCESS 
 Shown: Errors above 1.0 K or BW Util above 70.0 

  , Errors , BW Util 
  ,   , % 
  , Above , Above 
  ,  ,  
 Element , 1.0 K , 70.0 
 cat65-acomp-d,FastEthernet4/18 ,974787.00000000,0.03142596
 ios6-zdc-g2,TenGigabitEthernet6/4 ,887644.00000000,0.42693967
 ios6-dc-a,GigabitEthernet2/12 ,71172.00000000,12.59653282

What I am trying to make it look at is the following:


Code:
 Hostname,Interface,Number of Errors,Bandwidth Utilization
 cat65-acomp-d,FastEthernet4/18 ,974787.00000000,0.03142596
 ios6-zdc-g2,TenGigabitEthernet6/4 ,887644.00000000,0.42693967
 ios6-dc-a,GigabitEthernet2/12 ,71172.00000000,12.59653282

The bold parts are the items that are actually changing. So in this case, the report I run will always start with "eDir" and end with "70.0" (The second occurrence for those eagle eyed). I want that replaced with what is in the second example. Any chance on getting that to work? Thank you again for all your help, I do appreciate it.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Deleting Block of Text from a File rakeshkumar UNIX for Dummies Questions & Answers 3 11-28-2011 07:44 AM
replace block of text with content of another file ripat Shell Programming and Scripting 3 05-30-2010 05:11 PM
Replace text block in multiple files eiland Shell Programming and Scripting 6 02-23-2010 07:07 AM
finding a block in a file and replace with another file block. Zaheer.mic Shell Programming and Scripting 1 09-28-2009 02:13 PM
Deleting text block in file andre123 Shell Programming and Scripting 1 02-27-2007 12:17 PM



All times are GMT -4. The time now is 08:58 AM.