sed not working as I expected. What can be the possible reason?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users sed not working as I expected. What can be the possible reason?
# 1  
Old 04-12-2012
MySQL sed not working as I expected. What can be the possible reason?

Code:
# cat test
<abc>
</abc>
<abc>

</abc>
# sed 's/abc/checker/' test>test1
# cat test1
<checker>
</checker>
<checker>
</checker>
#

I want the first instance of abc to be changed to checker. But all(rest 3 also) are changed. I din't used global (g) option too..

What can be the possible reason for this... Am i doing anything incorrectly here...

Thanks
Ugenther

Moderator's Comments:
Mod Comment Please use code tags, thanks!

Last edited by Ugenther; 04-12-2012 at 08:38 AM.. Reason: code tags
# 2  
Old 04-12-2012
sed processes files line-by-line - the 'g' option refers to instances of the pattern on the addressed line(s), not the whole file.

If you want to restrict the substitution to parts of the input file you'll need to supply an address, e.g.:
Code:
sed '0,/abc/{s/abc/checker/}' test>test1

(addresses from line 0 to the first instance of abc)
This User Gave Thanks to CarloM For This Post:
# 3  
Old 04-12-2012
Code:
#sed '0,/abc/{s/abc/checker/}' test>test1

sed: command garbled: 0,/abc/{s/abc/checker/}


Am I mentioning the address incorrectly??
What can be the address for "abc" in the file test. #doubt

Any other alternate ways to replace the word(first instance) to another.
# 4  
Old 04-12-2012
I was using GNU sed 4.1.5, the syntax may be slightly different on whatever implementation your system has (it might insist on whitespace between the address & command, for instance) - check man sed.

Also, if you're on SunOS then try /usr/xpg4/bin/sed.

Last edited by CarloM; 04-12-2012 at 09:03 AM..
# 5  
Old 04-27-2012
you can try
Code:
sed '1s/abc/checker/' test > test1

# 6  
Old 04-27-2012
Try:
Code:
awk '! found && sub(from,to){found=1}1' from=abc to=checker infile

Code:
perl -0777pe 's/abc/checker/' infile


@CarloM:0,.. is GNU sed only , other seds know only 1,.. (and there needs to be a ";" before the closing "}" )

Last edited by Scrutinizer; 04-27-2012 at 06:01 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Test -e not working as expected (by me)

I ran into the following and still do not understand entirely the rationale behind this. If someone could explain why things are as they are I'd be thankful. The following was tested on AIX 7.1 with ksh88, but i suspect that to be ubiquitous. In an installation routine i had to create a set of... (6 Replies)
Discussion started by: bakunin
6 Replies

2. Shell Programming and Scripting

Script not working as expected

Hi, I have prepared a script and trying to execute it but not getting expected output. Could you please help and advise what is going wrong. "If else" part in below script is not working basically. I am running it on HP-UX. for i in slpd puma sfmdb do echo "******\t$i\t*******" echo... (10 Replies)
Discussion started by: sv0081493
10 Replies

3. UNIX for Dummies Questions & Answers

Nohup not working as expected

Hi. I am trying to start a script on my router that will execute even if i log off. To execute the script I write: nohup ./dslconnection > dslstat.out 2>&1 & It starts the job: 21968 admin 1604 S /bin/ash ./dslconnection The problem is that when I log back in the job has been... (6 Replies)
Discussion started by: sebcou
6 Replies

4. UNIX for Dummies Questions & Answers

-atime not working as expected

I need to sort through a volume that contains video files by access time and delete files that have not been accessed over x days. I have to use the access time as video files are originals that do not get modified, just read Testing commands on a local test folder... $ date Wed Sep 28... (10 Replies)
Discussion started by: canon273
10 Replies

5. Shell Programming and Scripting

bash variable (set via awk+sed) not working as expected

Hi! Been working on a script and I've been having a problem. I've finally narrowed it down to this variable I'm setting: servername=$(awk -v FS=\/ '{ print $7 } blah.txt | sed 's\/./-/g' | awk -v FS=\- '{print $1}')" This will essentially pare down a line like this: ... (7 Replies)
Discussion started by: creativedynamo
7 Replies

6. Shell Programming and Scripting

Why this is not working in expected way?

total=0 seq 1 5 | while read i ; do total=$(($total+$i)) echo $total done echo $totalThis outputs: 1 3 6 10 15 0whereas I am expecting: 1 3 6 10 15 15My bash version: (4 Replies)
Discussion started by: meharo
4 Replies

7. UNIX for Dummies Questions & Answers

For some reason, my grep doesn't work as expected

I am trying to find only those entries where 7018 and another number appear in the end of the line. 7018 2828 1423 2351 7018 2828 14887 2828 7018 1222 123 7018 1487 I am looking for a way to generate only the last two lines. I was trying to do just "grep '7018{1,5}" but it does not... (5 Replies)
Discussion started by: Legend986
5 Replies

8. UNIX for Dummies Questions & Answers

Redirection not working as expected

Portion of my script below : if ; then NUMBEROFFEILDS=`cat ${BASE_SCRIPT_LOC}/standardfilecleanup.lst|grep -w ${db_file_path}|awk -F: '{print NF}'` COUNT=4 while ; do awk_var="$"`echo $COUNT` file_name1=`cat ${BASE_SCRIPT_LOC}/standardfilecleanup.lst|grep -w... (1 Reply)
Discussion started by: findprakash
1 Replies

9. Shell Programming and Scripting

ls not working as expected within ksh

Hi, I use the command ls a\b\c\*.txt from the command line on HP UNIX and it works fine - It lists all files matching *.txt in the a\b\c directory When embeded in a ksh script `ls a\b\c\*.txt` it does not work - I get *.txt not found (even though there are files) I tried... (10 Replies)
Discussion started by: GNMIKE
10 Replies

10. Shell Programming and Scripting

which not working as expected

Hello. Consider the following magic words: # ls `which adduser` ls: /usr/sbin/adduser: No such file or directory # Hmmm... Then: # ls /usr/sbin/adduser /usr/sbin/adduser # Now what? Unforunately this little sniippet is used in my debian woody server's mysql pre install script.... (2 Replies)
Discussion started by: osee
2 Replies
Login or Register to Ask a Question