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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
11-18-2009, 10:01 AM
|
#1
|
Member
Registered: Oct 2009
Posts: 40
Rep:
|
BASH: is it possible to use "\n" in a search command?
Hi all,
I need to find a way to insert a string in a text after a pattern. The problem is that the pattern can not be found in one line, so I would need to tell the command in bash that the second part of the pattern comes in the next line after the first part of the pattern was found. So I tried:
sed -i '/[^ ]*<nta>'\n'<declaration>[^ ]*/ a\string' file.txt
But sadly the sed command like this can't find in the text what I want him to find...
Here is the text (the pattern I'm looking for is highlighted):
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE nta (View Source for full doctype...)>
<nta>
<declaration>// 971204, Arne, Fredrik, Kim & Paul. // Fairmont Hotel, San Francisco.</declaration>
<template>
<name x="32" y="16">Door</name>
Does somebody knows a solution for this?
|
|
|
11-18-2009, 10:22 AM
|
#2
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
gawk
Code:
gawk -vRS="<declaration>" -vFS="\n" ' # set record separator as "<declaration>", field separator as "\n"
$(NF-1) == "<nta>"{ f=1 } # if last 2nd field is <nta> , toggle flag
f {
# print the string for toggled flag
printf $0 RT "string"
f=0 # reset flag
next
}f==0' file
output
Code:
# ./shell.sh
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE nta (View Source for full doctype...)>
<nta>
<declaration>string// 971204, Arne, Fredrik, Kim & Paul. // Fairmont Hotel, San Francisco.</declaration>
<template>
<name x="32" y="16">Door</name>
Last edited by ghostdog74; 11-18-2009 at 10:44 AM.
|
|
|
11-18-2009, 10:35 AM
|
#3
|
Member
Registered: Oct 2009
Posts: 40
Original Poster
Rep:
|
Thanks for another solution Ghostdog!
This time it also works, but I didn't get how the code works, could you explain it a little?
|
|
|
11-18-2009, 11:04 AM
|
#4
|
Member
Registered: Oct 2009
Posts: 40
Original Poster
Rep:
|
Hey Ghostdog, yesterday you also replied to another thread I posted:
http://www.linuxquestions.org/questi...w-file-769456/
But sadly I didn't get the answer I wanted...I think I didn't write my question the right way. Would you take a look at the thread again? I made a comment there. I need a command that searches a pattern in a text file, but that just substitutes the first occurency of it and not all of them.
|
|
|
All times are GMT -5. The time now is 02:11 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|