[SOLVED] BASH: how to substitute just one occurency of a pattern in a text file
Linux - NewbieThis 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.
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.
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.
@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).
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;
...
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?
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.