LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 10-17-2012, 02:33 AM   #1
vin_2
LQ Newbie
 
Registered: Oct 2012
Posts: 1

Rep: Reputation: Disabled
Replace a line in file used sed command


Hi all,

I want to replace a line in a file using sed or equivalent,

File snip:
#######################################################
Command file "Format"
#######################################################
$value
Solve1
History 3
.....
......
solver name "c:\acro\past\71_p_5_1_1 ncpu=8"
solver input file "c:\pt\bs\case3\CAT_base_1200_B.y"

..................
......................


I want to replace below two from the file
solver name "c:\acro\past\71_p_5_1_1 ncpu=8"
solver input file "c:\pt\bs\case3\CAT_base_1200_B.y"

as like this:...
solver name "/opt/acro/71_p_5_1_1 ncpu=8"
solver input file "CAT_base_1200_B.y"


could some please help ..
 
Old 10-17-2012, 03:04 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Basically you want to change windows path name with your linux path, don't you? Here we go:
Code:
sed '/solver name/s@c:.*\\\([^\\]*\)$@/opt/acro/\1@' file
Here the regexp (in blue) matches c: and any string to the last backslash. Note that you have to escape (that is double) the backslashes inside the regular expression. The remaining part till the end of the line is retained (see the escaped parentheses) and used in the substitution string as \1.

You can do the same for the second line:
Code:
sed '/input file/s@c:.*\\\([^\\]*\)$@\1@' file
and combining them together using multiple -e options:
Code:
sed -e '/solver name/s@c:.*\\\([^\\]*\)$@/opt/acro/\1@' -e '/input file/s@c:.*\\\([^\\]*\)$@\1@' file
Finally, check the option -i to edit the file in place (see man sed for details).
 
  


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
SED replace line command throwing parsing error when run through SSH uniami Linux - Newbie 3 03-09-2012 03:46 AM
sed command to replace line in text file gengwei89 Linux - Newbie 6 11-06-2011 09:10 AM
how do you replace text between two words in a whole file not just 1 line w/ sed/awk lityit Programming 5 11-04-2011 01:04 AM
[SOLVED] Need sed help: s/ command won't replace two occurrences of pattern on same line GrapefruiTgirl Programming 7 12-16-2009 03:08 AM
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 08:01 PM

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

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

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