LinuxQuestions.org
Help answer threads with 0 replies.
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 11-18-2009, 11:01 AM   #1
carolflb
Member
 
Registered: Oct 2009
Posts: 40

Rep: Reputation: 15
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?
 
Old 11-18-2009, 11:22 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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 11:44 AM.
 
Old 11-18-2009, 11:35 AM   #3
carolflb
Member
 
Registered: Oct 2009
Posts: 40

Original Poster
Rep: Reputation: 15
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?
 
Old 11-18-2009, 12:04 PM   #4
carolflb
Member
 
Registered: Oct 2009
Posts: 40

Original Poster
Rep: Reputation: 15
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 12:26 PM
giving search-string(s) from a file to linux "find" command Fond_of_Opensource Linux - Newbie 3 02-02-2009 07:14 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 11:18 PM
Bash: "after" or "waitfor" command enemorales Programming 3 06-15-2005 01:04 AM
GOOD P2P client (search video, movies) in "COMMAND MODE" swmok Linux - Software 4 01-21-2004 09:03 AM


All times are GMT -5. The time now is 09:58 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration