LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 01-09-2013, 10:06 AM   #1
j-me
Member
 
Registered: Jan 2003
Location: des moines, ia
Distribution: suse RH
Posts: 123

Rep: Reputation: 16
search a directory and add file contents when find string


I have a requirement to insert multiple lines in file(s) that exists in multiple directories. I am thinking that sed or awk would be the best application for this but am stumped on how to accomplish effectively. I prefer sed right now but am open to awk.

requirement:
/DIR1/[multiple directories]
search for <string1>information</string1> when find insert file contents after line.

File contents:
Code:
 <properties>
      <Property1>        
      <permission>permission_A</permission>
        <permission>permission_B</permission>
        <permission>permission_C</permission>
  </properties>
What I have tried but failed:
in simplest form:
Code:
sed -i '#<string1>information</string1># -r insert_contents.txt ' config.xml
in a find command
Code:
find /DIR1/Sub_Dir2/config.xml -type f -exec sed -i '#<string1>information</string1># -r insert_contents.txt' {} \;
I recv no errors but no data is inserted.
Thank you.
 
Old 01-11-2013, 01:56 AM   #2
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,001

Rep: Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003
sorry guy, your find is useless:
find /DIR1/Sub_Dir2/config.xml ..... will find only /DIR1/Sub_Dir2/config.xml (if it was a file), so -exec will work on that single file, which is the same as your previous code (without find).
Also that sed will not insert anything, actually it will do nothing because it is not a valid sed script (contains no sed commands at all).
 
Old 01-11-2013, 03:29 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
As pan64 pointed out your sed command is incomplete. Here is a working (?) version:
Code:
find /DIR1 -name config.xml -exec sed -i.bck -r '\@<string1>information</string1>@r insert_contents.txt' {} \;
The sed command to insert the content of a file is r file. Please notice that in order to use a character different than slash in the sed address, you have to put a backslash in front of the address itself (see the part highlighted in red in my example).

Also notice that I used -i.bck to make sed keep a backup copy of the original files. Useful for testing purposes until you're satisfied with the results. Hope this helps.

Last edited by colucix; 01-11-2013 at 03:31 AM.
 
1 members found this post helpful.
Old 01-11-2013, 08:54 AM   #4
j-me
Member
 
Registered: Jan 2003
Location: des moines, ia
Distribution: suse RH
Posts: 123

Original Poster
Rep: Reputation: 16
Thank you both. pan64 ~ I realized after I posted that my find did not perform what I had stated earlier but it was using my isolated test command to prove the sed would work. not exemplifying my entire intent. thank you for pointing that out.
colucix ~ thank you as that was very helpful and proved to work very well.
 
  


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 On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Find Text After String Search in Text File redir Linux - Newbie 12 08-02-2011 03:57 PM
PHP: Find a string in a text file and add something to that line aocferreira Programming 1 05-26-2011 01:52 PM
giving search-string(s) from a file to linux "find" command Fond_of_Opensource Linux - Newbie 3 02-02-2009 06:14 PM
Search for a string within a directory hammertime1983 Linux - Newbie 2 11-01-2007 08:36 PM
recursive search of a string in a directory ?! realos Linux - Software 4 11-27-2002 04:49 AM


All times are GMT -5. The time now is 12:00 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration