Go Job Hunting at the LQ Job Marketplace
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 02-13-2010, 06:35 PM   #16
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 657Reputation: 657Reputation: 657Reputation: 657Reputation: 657Reputation: 657

If you use
sed "s#pattern#replacepattern#", then the pattern and replacement can have slashes in them. In my solution, I did this as well.

The exclamation point can be a problem if you are working interactively. It is used for recalling commands from the history buffer. It needs to be escaped then typing commands on the command line interface but not if it's in a script. This can sometimes make debugging or designing a program more difficult.

Looking closer at one of your solutions,
sed "#$line#s#$line\/#!$line\/#g" file2
The first two octothorpes won't work. You need to use slashes for pattern matching before the sed command.
Since $line matches the entire line, you don't need to match a pattern to locate the line. The LHS contains the entire line.
Also, since the entire line is in the LHS, you don't need "g" at the end.

You can use have a pattern test in the beginning determine whether to execute the following
sed command.
/pattern/s ...
/pattern/!s ...,

The second one is run if the pattern isn't found.


By the way, your new information about the data causes the particular sed command I used not to work. Working with sed, awk & grep, the pattern of the input can be very important. Special conditions need to be tested for as well.

Last edited by jschiwal; 02-13-2010 at 06:54 PM.
Old 02-14-2010, 05:32 AM   #17
LQ Newbie
Registered: Feb 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Hi All,

Thanks jschiwal for your great input really helpful.
I have managed to solve the problem with this script finally
Soon will try to adapt your method of creating sed command using sed itself

I am posting this because it may be useful for others and as i mentioned before in my previous post, my
input file contains meta-characters in various places. Hence the solution I used:


while read line; do
sed "s#$search#!$search#" file2 >tempfile.tmp
mv tempfile.tmp file2
done < file1

Input file1

a+b=c+d 1e105
g+h=o+p abcdefg
rev/ 0.35 / h 35/
h2 / 20 / he / ar

file2 (going to be changed file)

a+b=c+d 1e105
x+y=z+s 5e105
g+h=o+p abcdefg
t+r=w+q xvyderf
rev/ 0.35 / h 35/
h2 / 20 / he / ar

file2 (after changes made)

!a+b=c+d 1e105
x+y=z+s 5e105
!g+h=o+p abcdefg
t+r=w+q xvyderf
!rev/ 0.35 / h 35/
!h2 / 20 / he / ar

So with this script the problem with regards to meta-characters is solved
Old 02-26-2010, 10:06 AM   #18
LQ Newbie
Registered: Feb 2010
Posts: 16

Original Poster
Rep: Reputation: 0
cant get the last part

Originally Posted by jschiwal View Post
Your program will run sed for each line of input in file1.

Each time you run it you are running it on the same input file instead of saving what was done so far.

My idea is to use sed to process file1 to create a sed program.

sed '...' file1 >process.sed

This should produce these lines given your specific example:

Then you would run:
sed -f process.sed file2 >file3

There will be as many lines in process.sed as in your file1 file. If some lines in file1 are repeated, you could use "sort file1 | uniq" to eliminate redundancy.

I have left out the sed command I used to process your input file "file1".

I have used this technique at work to generate a script to delete files from a list of names (without the extension). There are sometimes up to 2000 files I need to delete from about 60 devices. Doing this using a graphical interface would take forever.

One script I make is run in Cygwin/X to delete files in a shared directory on a Windows server. Another is a script I upload to a number of devices. These devices have a custom OS so the command lines are different. I just need to produce the list and then convert it to each style of script.
So your example isn't a far fetched one. You may find it useful in the future.
Hi jschiwal,

I have been trying your suggestion for some time, but I keep get through the "wall"

At the moment i could get up to:

Using these commands:
sed 's/^/s#/' <file1 >file2
sed 's/$/#!/' <file2 >file3

But I really dont know how to make it get the 1st part back, g+h=o+p/ back at the end of the line to finally get like you have mentioned:


Thanks in advance


awk, find, replace, sed

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
Using sed/awk to replace a string at a given position in anoopvraj Linux - Newbie 6 05-30-2009 07:59 AM
replace a pattern with a line using sed/awk lokeshn05 Linux - Newbie 3 05-06-2009 03:01 PM
URGENT: Help for Find and Replace with AWK onacorpuscle Linux - Newbie 3 12-07-2007 11:22 AM
SED;find and replace;help required gd13 Programming 3 12-21-2004 06:33 AM
sed or awk question - replace caps with small letters computera Linux - General 1 12-30-2003 04:39 AM

All times are GMT -5. The time now is 12:34 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration