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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
Due to network maintenance being performed by our provider, LQ will be down starting at 05:01 AM UTC. The exact duration of the downtime isn't currently known. We apologize for the inconvenience.
|
 |
11-19-2009, 01: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, 01:37 AM
|
#2
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
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, 01:47 AM
|
#3
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,577
|
|
|
|
|
11-19-2009, 02:09 AM
|
#4
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
@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, 02: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, 03: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, 03:05 AM
|
#7
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,703
|
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, 03: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, 03:31 AM
|
#9
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:18 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
|
|