[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

shell script: how to find a string within the line

Posted on 2009-02-23
9
Medium Priority
?
2,278 Views
Last Modified: 2013-12-26
Hi,

I want to write this script, that looks for a string within a line (the line is read from a file), while he does this he writes each line into a file, and the line containing this string is copied again into the file, with a replaced string.

This is how far I have come:

#!/bin/bash
exec<config.xml
string="*Standard*"
while read line
do
echo $line >> file.xml
case $string in $line) echo $line >> file.xml ;;
*) ;;
esac
done

But this is clearly not working.


For example if config.xml contains:

bla bla bla  BBB
blablabla AAA
bla la la BBB

and my script is looking for the string AAA.
After my script runs, file.xml should contain:

bla bla bla  BBB
blablabla AAA
blablabla AAAnew  // matches string AAA and is copied again replacing AAA to AAAnew
bla la la BBB


Does anybody know how to do this?

Thank you in advance.


0
Comment
Question by:Endelm
9 Comments
 
LVL 23

Expert Comment

by:Maciej S
ID: 23710355
If you have GNU sed:
sed 's/\(.*\)\(AAA\)\(.*\)/\1\2\3\n\1AAAnew\3/' config.xml > file.xml
0
 
LVL 23

Accepted Solution

by:
Maciej S earned 2000 total points
ID: 23710390
If you don't have GNU version of sed, you may use below script (it can be probably much simpler, but it works).
#!/bin/sh
 
FILE=config.xml
NEW_FILE=file.xml
 
:> ${NEW_FILE}
 
IFS="
"
 
for line in `cat ${FILE}`; do
   echo ${line} >> ${NEW_FILE}
   if [ -n "`echo ${line} | grep AAA`" ]; then 
      echo ${line} | sed 's/AAA/AAAnew/g' >> ${NEW_FILE}
   fi   
done

Open in new window

0
 
LVL 10

Expert Comment

by:mahome
ID: 23710397
Another version with sed:

sed -n -e 'p' -e 's/AAA/AAAnew/p' config.xml > file.xml

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Expert Comment

by:DrCalmo
ID: 23710446
With sed and grep:

#!/bin/bash
set -x
exec<config.xml
string="AAA"
newstring="AAAnew"
while read line
do
	echo $line >> file.xml
	echo $line | grep $string
	if [ $? -eq 0 ]; then
		echo `echo $line | sed "s/${string}/${newstring}/"` >> file.xml
	fi
done

Open in new window

0
 
LVL 1

Expert Comment

by:rohitttt
ID: 23710452
using bash
#!/bin/bash
 
string=AAA
 
while read line
do
	if [[ $line == *$string* ]]
	then
		echo -e "$line\n${line}BBB"
	else
		echo $line
	fi
done < config.xml > file.xml

Open in new window

0
 
LVL 23

Expert Comment

by:Maciej S
ID: 23710465
Well, mahome's oneliner is better - works regardless of sed version, so - use his one :)
I would just shorten it a little (but it's still _the_ _same_ command): sed -n 'p;s/AAA/AAAnew/p' config.xml > file.xml
0
 
LVL 23

Expert Comment

by:Maciej S
ID: 23710505
I was too slow with my last comment.
Anyway - mahome - I think your is much better (and if author wants to change his choice of answer - I won't object of course).
0
 
LVL 10

Expert Comment

by:mahome
ID: 23710587
Off-Topic
Never mind, just points. It's interesting to see how much possibilities there are. And I'm learning with each one (I didn't know, that it's possible to seperate the commands with semicolon and that replacement is only in GNU version possible).
0
 
LVL 23

Expert Comment

by:Maciej S
ID: 23710645
I mentioned about GNU sed, because of my use of \n.
One of the best features of GNU sed for me is -i option (edit in place, with possibility of creating backup).
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month20 days, 11 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question