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! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-19-2009, 02:14 AM
|
#1
|
Member
Registered: Oct 2009
Posts: 40
Rep:
|
BASH: how to substitute just one occurency of a pattern in a text file
I want to know if it's possible to use 'sed' to substitute just the first occurency of a searched pattern in the file, so if I have:
text
text PATTERN
text
PATTERN
text PATTERN text
text
is there a way of substituting the first occurency of PATTERN in the file? So that at the end I have:
text
text SUBSTITUTED PATTERN
text
PATTERN
text PATTERN text
text
|
|
|
11-19-2009, 02:37 AM
|
#2
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
If you want to do it using sed then it is a sed question not a bash question (so the thread title is misleading and I cannot help you).
If you are happy to do it in bash script then it is easy but will take longer to execute than when done by sed. That may or may not be a problem, depending on the size of the file.
|
|
|
11-19-2009, 02:47 AM
|
#3
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
|
|
|
11-19-2009, 03:09 AM
|
#4
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
@OP : awk,sed etc are just tools to help your work. What's more important is to get your algorithm and steps to solve the problem right. You only want to substitute the first occurrence. That means you can set a toggle flag to turn substitution "off" when the first pattern is found
Code:
#!/bin/bash
toggle=0
while read -r line
do
[ "$toggle" -eq 1 ] && echo $line && continue
if [ "$toggle" -eq 0 ] ; then
case $line in
*PATTERN*)
line=${line/PATTERN/SUBSTITUTED}
toggle=1
;;
esac
fi
echo $line
done <"file"
output
Code:
# ./shell.sh
text
text SUBSTITUTED
text
PATTERN
text PATTERN text
text
i think the code is self explanatory. read up more on shell scripting (see my sig).
|
|
|
11-19-2009, 03:32 AM
|
#5
|
Member
Registered: Oct 2009
Posts: 40
Original Poster
Rep:
|
Hi guys,
sorry for the confusion. The script I'm writing is a .sh file and I'm open to any solution. I just wrote 'with sed' because it is the only command I was using until now.
@David: Thanks for the link! I just tried to use:
sed '0,/RE/s//to_that/' file to change only the first match, but it didn't work.
I wrote in my script:
sed '0,/<declaration>/s//<declaration>\nint p_covg['$access_array_size'];/' file.xml
The pattern I want to find is <declaration> and it should be substituted by <declaration>\nint p_covg['$access_array_size'];, where access_array_size is an integer.
And the text in the file.xml is the following:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE nta PUBLIC '-//Uppaal Team//DTD Flat System 1.1//EN' 'http://www.it.uu.se/research/group/darts/uppaal/flat-1_1.dtd'>
<nta>
<declaration>
clock global;
broadcast chan dummybchannel;
...
Would you know why it doesn't work?
|
|
|
11-19-2009, 04:01 AM
|
#6
|
Member
Registered: Oct 2009
Posts: 40
Original Poster
Rep:
|
Hi Ghostdog,
nice that you are answering again, thanks. I used your idea but it doesn't save the changes in the original file. What do I need to do, so that the changes are saved?
|
|
|
11-19-2009, 04:05 AM
|
#7
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
It looks like this: sed '0,/<declaration>/s//<declaration>\nint p_covg['$access_array_size'];/' file.xml
is not correct, you are missing the part between the red slashes.
The <declaration> part should be there:
sed '0,/<declaration>/s/<declaration>/<declaration>\nint p_covg['$access_array_size'];/' file.xml
Hope this helps.
|
|
|
11-19-2009, 04:16 AM
|
#8
|
Member
Registered: Oct 2009
Posts: 40
Original Poster
Rep:
|
You were right druuna. It works now
Thanks a lot!
|
|
|
11-19-2009, 04:31 AM
|
#9
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by carolflb
doesn't save the changes in the original file. What do I need to do, so that the changes are saved?
|
redirect to a file and rename it back.
|
|
|
All times are GMT -5. The time now is 02:12 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|