LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-03-2009, 03:38 AM   #1
scriptblues
LQ Newbie
 
Registered: Jun 2009
Posts: 13

Rep: Reputation: 0
sed script help


Hi

I need sed or awk script which basically takes the file containing the input as below

Original file
apple
bat
elephant
cat
dog
elephant
fox
goat
elephant
cat
dog
fox
elephant
fox

output should be like
apple
bat
elephant
cat
giraffe
dog
fox
goat
elephant
cat
giraffe
dog
fox
fox

Basically, First I want to search for line containing "cat" and from there on search for "elephant" and delete the line containing elephant and add line "girrafe" after cat.

Request for speedy answer if possible
 
Old 06-03-2009, 03:48 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Sounds like homework.
 
Old 06-03-2009, 03:51 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
put in some effort....show us what you have got.
 
Old 06-03-2009, 04:06 AM   #4
scriptblues
LQ Newbie
 
Registered: Jun 2009
Posts: 13

Original Poster
Rep: Reputation: 0
Okay ..
I tried to solve this by breaking the problem into two steps
1 st step - Add Giraffe after cat blindly
2nd step - search between cat and ) (ofcourse its non existing here) and delete elephant.
I was able to get that, but I am missing the

parent bash script contains..says parent.sh
>>>
sed '/cat/a\Giraffe' hyd1 > hyd2
sedfile3 hyd2 > hyd3
<<<

sedfile3 contains ..(sedfile3)
>>>
sed -n '
/cat/,/)/ {
s/Ele.*//
s/[ ^I]*$//
/^$/ d
p
}
' $1
<<<

And If I run ./parent.sh I get output as
cat
giraffe
dog
fox
goat
elephant
cat
giraffe
dog
fox
fox

not as intended .. I am missing the portion before "cat"
 
Old 06-03-2009, 04:09 AM   #5
scriptblues
LQ Newbie
 
Registered: Jun 2009
Posts: 13

Original Poster
Rep: Reputation: 0
Some typo in my previous message

1 st step - Add Giraffe after cat blindly
2nd step - search between cat and ) (ofcourse its non existing here) and delete elephant.
I was able to get that, but I am missing something.


hyd1 file contains. ..
apple
bat
elephant
cat
dog
elephant
fox
goat
elephant
cat
dog
fox
elephant
fox

My parent bash script contains (name parent.sh)
>>>
sed '/cat/a\Giraffe' hyd1 > hyd2
sedfile3 hyd2 > hyd3
<<<



sedfile3 contains ..(sedfile3)
>>>
sed -n '
/cat/,/)/ {
s/Ele.*//
s/[ ^I]*$//
/^$/ d
p
}
' $1
<<<

And If I run ./parent.sh I get output as
cat
giraffe
dog
fox
goat
elephant
cat
giraffe
dog
fox
fox

not as intended .. I am missing the portion before "cat"
 
Old 06-03-2009, 06:21 AM   #6
scriptblues
LQ Newbie
 
Registered: Jun 2009
Posts: 13

Original Poster
Rep: Reputation: 0
I have gone through the backup questions, but unable to find on how to search the second pattern elephant ( and delete), between pattern cat (and say fox)
 
Old 06-03-2009, 06:53 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Request for speedy answer if possible
The US is not awake yet, and Europe is already at work. You may have to be patient.

I find this a bit hard to follow.....two things:

1. You use "sed -n" which suppresses printing until a "p" is found. Is that why you are missing all before "cat".

2. To search the second pattern, you may need this general form:
sed -e '<some code>' -e '<some more code>' oldfile > newfile

Quote:
search the second pattern elephant ( and delete), between pattern cat (and say fox)
I don't understand this.


Finally, what references are you using for SED?
 
Old 06-03-2009, 07:17 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
i don't really understand your output, it seems like you only want to delete the first instance of "elephant" after cat, right?
 
Old 06-03-2009, 11:42 PM   #9
scriptblues
LQ Newbie
 
Registered: Jun 2009
Posts: 13

Original Poster
Rep: Reputation: 0
Let me explain,
I wanted is to search for "elephant" between patterns "cat" and "fox" and delete the line containing "elephant"


If original file contains:
apple
bat
elephant --- Ignore,this elephant as this is notwithin the patterns "cat" and "fox"
cat
dog
elephant -- I have to remove this line as is it is between "cat" and "fox"
fox
goat
elephant - Ignore
cat
dog
fox
elephant - Delete
fox


Output should be

apple
bat
elephant
cat
dog
fox
goat
elephant
cat
dog
fox
fox

HTH in better understanding of my problem.

Last edited by scriptblues; 06-03-2009 at 11:44 PM.
 
Old 06-04-2009, 04:15 AM   #10
scriptblues
LQ Newbie
 
Registered: Jun 2009
Posts: 13

Original Poster
Rep: Reputation: 0
Smile

Okay guys, I was able to solve the problem by following the below mentioned approach

parent.sh contains
>>>.
sed '/cat/a\Giraffe' hyd1 > hyd2
sedfile3 hyd2 > hyd3
<<<<

sedfile3 contains
>>>
sed '
/cat/,/fox/ {
s/ele.*//
s/[ ^I]*$//
/^$/ d
}
' $1
<<<<<

It is giving me the desired output.
But do let me know, if there is any easier approach.
This was a really learning experience on sed.
 
Old 06-04-2009, 07:27 AM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
This almost works:
Code:
sed  '/cat/,/fox/{/elephant/d}' filename
The problem is the last instance where you have:
Quote:
cat
dog
fox
elephant
fox
The problem is that the SED addressing range goes from "cat" to the next instance of "fox"---thus it never sees the last instance of "elephant".

The real issue in this kind of problem is to unambiguously define the logic to be used. SED addressing goes from <address1> to the next instance of <address2>. What you need is logic that goes from <address1> to the last instance of <address2> (before finding <address1> again). But the latter logic might not work with some other pattern in the target file.......

I'll post back if I think of something....

Last edited by pixellany; 06-04-2009 at 07:28 AM.
 
Old 06-17-2009, 05:22 AM   #12
scriptblues
LQ Newbie
 
Registered: Jun 2009
Posts: 13

Original Poster
Rep: Reputation: 0
Is there a way, where in I want to search within the pattern (say here between "cat" and "fox" , modify "elephant" to "dragon", but only in the first range.
For eg:
If input is like
cat
dog
elephant
fox
cat
dog
elephant
fox

I want output like
cat
dog
dragon << only elephant in first range block of cat and fox is modified"
fox
cat
dog
elephant << not the second one in the range of "cat and fox"
fox
 
Old 06-17-2009, 06:33 AM   #13
scriptblues
LQ Newbie
 
Registered: Jun 2009
Posts: 13

Original Poster
Rep: Reputation: 0
Find and replace between regex (Only first occurance)

I want to find and replace only first occurance in a block.
How to do that using sed.
Say,

Word1
a
b
c
Word2
Word1
a
b
c
Word2

is there in my input file.

My script
sed '/Word1/,/Word2/{s/a/d}' file

will give the output replacing all a's to d's between this word range.
But I want to restrict in only first block.
My output should be like

Word1
d
b
c
Word2
Word1
a
b
c
Word2

Any suggestions ..
 
Old 06-17-2009, 06:52 AM   #14
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
sed '1,/Word2/{/Word1/,/Word2/s/a/d/}' file
 
Old 06-17-2009, 06:55 AM   #15
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Looks like homework to me. Why don't you try eg. explicitly specifying lines 2-4 for replacement. Would it work in your case?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script with sed emetib Linux - General 3 05-04-2009 01:58 PM
Sed script help genderbender Programming 4 05-04-2008 04:38 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
sed script cranium2004 Programming 4 06-21-2006 09:33 AM
sed script glam1 Programming 2 08-31-2004 11:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration