LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-21-2010, 07:26 AM   #1
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Rep: Reputation: 43
sed (?) insert string between 2 others


I have a file like this:

Code:
/opt/local/some/dir/
blablablabiugfyugf
"/opt/local/some/dir/file.txt"
"/opt/local/someother/dir/file.bin"
/opt/local/some/dir/file.txt
plaplaplllalalalal
/opt/local/someother/dir/file.bin
MYBEST_DIR="/opt/local/some/dir/"
blablabla
sometimes my favorite path is in the middle of the line /opt/local/somedifrent/dir/file.txt
mmmmrtatatatata
/opt/local/someotherX/dir sometimes is at beginning or...
"opt/local/someother/dir" in this funny sign " <- which i dont know how to call it ;)
What I need to do, is to insert between /opt/ and local/ string NEW/Path/
I was tring with "sed" but it didnt work to me :/
Can anyone help me with this please ?

Last edited by czezz; 05-21-2010 at 07:29 AM.
 
Old 05-21-2010, 07:28 AM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I don't understand what you want to do.
 
Old 05-21-2010, 07:33 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Maybe if you showed us a before and after picture and what you have tried we can show you where the problem is?
 
Old 05-21-2010, 07:34 AM   #4
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Code:
echo '/opt/local/some/local/dir/' | sed 's#.*?\(/local.*\)#/opt/path\1#'

Last edited by PMP; 05-21-2010 at 07:42 AM. Reason: First match
 
Old 05-21-2010, 07:59 AM   #5
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
PMP its almost this what I need but when I replace path with ur code it also erase beginning of the line.
Eg. MYBEST_DIR= will be erased. Same as every sign "
 
Old 05-21-2010, 08:03 AM   #6
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
My Mistake !! Modified Verison.

Code:
echo '/opt/local/some/local/dir/' | sed 's#/opt\(/local.*\)#/opt/path\1#'

Last edited by PMP; 05-21-2010 at 08:08 AM.
 
Old 05-21-2010, 08:04 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Even easier, just rewrite the entire substring:
Code:
sed 's|/opt/local/|/opt/new/path/local/|'
Note that you can use any character in sed's substitution pattern, not just the traditional s///, allowing you to work on file paths. In this case I used s||| (and PMP above used s###).
 
Old 05-21-2010, 08:12 AM   #8
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Quote:
Originally Posted by David the H. View Post
Even easier, just rewrite the entire substring:
Code:
sed 's|/opt/local/|/opt/new/path/local/|'
Note that you can use any character in sed's substitution pattern, not just the traditional s///, allowing you to work on file paths. In this case I used s||| (and PMP above used s###).
Right !! I made it complicated !!
 
Old 05-21-2010, 10:46 AM   #9
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
Awesome! )))
Many thanks for help.

btw. I have one more... lets say "half-problem":
What im going to do now, is to use this command on all files on /opt.
So I have written a simple loop:
Code:
#!/bin/bash

files="/opt/*"
for i in $files
do
  echo "parsing file $i"
  sed 's|/opt/local|/opt/NEW_PATH/local|' $i > $i.out
  mv  $i.out $i
done
This works fine but I need to re-run script in each dir on /opt file system.

Is there any trick I can use to execute this loop on every sub-dir on /opt file system ?

Of course I can always set: files="/opt/*/*/*" but I must know exactelly count of sub-dirs.
 
Old 05-21-2010, 11:01 AM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
So you want to recursively get all the dirs in /opt?

Do you want to include normal files in the results or just directories?
 
Old 05-21-2010, 11:13 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Firstly, just use 'sed -i' then the 'mv' becomes redundant.

Then use a while/read loop:
Code:
while read file
do
    if grep -q "/opt/local" $file
    then
        sed -i 's|/opt/local|/opt/NEW_PATH/local|' $file
    fi
done< <(find /opt -type f)
 
  


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
Insert using sed ZAMO Linux - General 6 01-15-2009 11:05 PM
Trying to change String using sed with a string \/home\/user\/Desktop icecoolcorey Programming 10 06-12-2008 11:32 PM
unable to insert string on particular lines using sed livetoday Linux - Newbie 5 03-30-2008 11:28 PM
insert string with sed greg108 Programming 7 02-18-2005 01:11 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:19 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
Open Source Consulting | Domain Registration