Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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"
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)
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.
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.......
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.