Possible to restrict SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Possible to restrict SED
# 1  
Old 12-11-2006
Possible to restrict SED ?

Hi,

I guess by default SED looks for any occurences anywhere in the file or text. Is it possible to restrict the search to replace only for the complete pattern .

I have data file like

record 1->"abc abc123"
record 2->"123 aaaaa"
record 3->"./abc acacac"

i have a replace file

"abc"
"123"

I wanna replace only the record 1 and 2 as it matched the patern(abc & 123 respectively), for record 3 sed shouldn't do anything as the replace file do not have match for ./abc ? .

Is this possible ?

PS : Iam not stringent to do it in sed, even if 'tr' or anything works i would be ok.

I really appreciate any input.

Thanks

Last edited by braindrain; 12-11-2006 at 12:07 PM..
# 2  
Old 12-11-2006
why dont u use "^" to check the beginning of the line
like
sed 's/^abc/tr/' file1
it will search lines starting with abc only

--Manish
# 3  
Old 12-11-2006
Question

Hi,

Unfortunately it may not always at the begining of line and if iam not wrong in sed ^ refers to non-matching.

Anyway, i could figure out something, but fiddling around to finid out a way

In vi we can use sed to replace by bracing the < and >, which works fine.

However I am not able to replicate the same in shell.

I tried to have the replace file withthe braces and escape chars
'\/\<CELL\>'

my data file : 1~hello /CELL

Iam suppose to replace /CELL in data file to 'X'

my code looks like :

Code:
while read dat_file;do
while read replace_file;do
sed -e s,$replace_file,X,g
done < replace.txt
done < datfile.txt

However whenever I read the replace file , it's reading as
Code:
/<CELL>

and never will find a match so no replace happens.
Even I tried with single quote, but in vain.

'/<CELL>'

So Iam kind of lost how to pass the text with / from the file ??

Also, the sed statement %s,\/\<CELL\>,X,g works in vi but not in shell Smilie

Last edited by braindrain; 12-11-2006 at 01:40 PM..
# 4  
Old 12-11-2006
as far as my understanding goes ^ refers to same beginning of the line in sed also.. it doesnt refer to non matching...
# 5  
Old 12-11-2006
Okay I almost nailed out , " double Quotes" with appropriate escape chars does the trick, but missing one and important thing towards the last step.

while read d;do
for l in `cat $2`
do
HTML Code:
echo $d"| sed -e s,"$l",X,g" # for display
echo $d | sed -e s,$l,X,g
....
For the above code I could see the echo is displaying is the following
echo 1~hello /CELL| sed -e s,"\/\<CELL\>",X,g
above is correct statement when I execute from the command line. Ouput is
HTML Code:
1~hello X
However when it is executed from shell script. It doesn't replace.
it simply outputs
HTML Code:
hello /CELL
Anybody can guess a reason ?
FYI, I tried on ksh,sh and bash.

Last edited by braindrain; 12-11-2006 at 07:09 PM..
# 6  
Old 12-12-2006
try this
Code:
echo $d | sed -e "s,/CELL,X,g"

# 7  
Old 12-12-2006
Hi Anbu,
thanks, it will work, but it will also search and replace as wildcard "searchsting*", but my requirement is to confine the search to the string,

i.e

d=/CEL
echo $d | sed -e "s,/CEL,X,g"
X ->fine and expected

d=/CELLU
echo $d | sed -e "s,/CEL,X,g"
XLU -> not expected,

I would like to replace only the if the match exist as complete word and not part of word.

thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Restrict sudo -i

Hi, I wanted add a group to the sudoers file so they can run sudo commands and blocked su command but it seems they can just run sudo -i to switch to root which defeats my purpose. Is it possible to block sudo -i with the help of sudoers file or any other way. Please advise. The below... (1 Reply)
Discussion started by: Jartan
1 Replies

2. UNIX for Dummies Questions & Answers

Restrict access

I'm trying to use squid to restrict elinks' access to certain websites(only http traffic). I have tried some configs in squid.conf but no luck. Hope someone has a bit of time to explain me how can you make these config's :) ---------- Post updated at 05:40 PM ---------- Previous update was at... (1 Reply)
Discussion started by: Birnbacher
1 Replies

3. UNIX for Dummies Questions & Answers

Restrict user access.

Hi All, How can we restrict a particular user access to a particular shell in solaris 10. Thanks in Advance. (5 Replies)
Discussion started by: rama krishna
5 Replies

4. Shell Programming and Scripting

how can i restrict commands

hi all, i want to restrict commands to run from a specifc directory..and to make that only some specific commands to run ,,not all. please help me ...how can i make this happen. (3 Replies)
Discussion started by: tprayush
3 Replies

5. UNIX for Advanced & Expert Users

Restrict Access to the folder

Hi I have requirement to create 3 new users on my server but to restrict their access to a set of particular folders. /export/home/kapil/shared, /export/home/kapil/shared/Folder1 /export/home/kapil/shared/Folder2 These folders should be accessible to all the 3 users and to me too.... (1 Reply)
Discussion started by: kapilk
1 Replies

6. Shell Programming and Scripting

restrict USB

Hi, How can i restrict use of USB in redhat. & also is it possible to allow USB devices for only specific devices or only few known penDrives......? Thank you (4 Replies)
Discussion started by: shivarajM
4 Replies

7. UNIX for Dummies Questions & Answers

restrict in file creation

Hi, how to restrict directories with one type of file creation. thanks and regards. (4 Replies)
Discussion started by: nk_bastia
4 Replies

8. Solaris

restrict commands

Dears, how to determine the commands that could be used by certain user..like I want to prevent some users from running pwd command???? Thanx (4 Replies)
Discussion started by: mm00123
4 Replies

9. Solaris

restrict a user to certain command

Hi all, I am using Sun OS 5.10. I am new to Unix. Is there some way to restrict a specific user to certain command say "/usr/bin/more" ?? for example: I want that user1 can execute more command & user2 can't. Can we somehow edit .profile file in the home directory of user to achieve... (1 Reply)
Discussion started by: vikas027
1 Replies

10. UNIX for Dummies Questions & Answers

How to restrict account to one log-in?

Our users have the tendency to use only one login account, to do their jobs. Obvious itīs a matter of training our users. But our internal audit team insists on restrictions from our system. So is there an option to restrict an account to only login once into the system? We use HP-UX 11.0. ... (0 Replies)
Discussion started by: Egroman
0 Replies
Login or Register to Ask a Question