LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 04-28-2011, 02:41 PM   #1
boumphreyfr
LQ Newbie
 
Registered: Apr 2011
Distribution: lubuntu
Posts: 14

Rep: Reputation: 0
using sed and grep with a text string


Q: Is there any way to use grep and sed with a string variable rather than with a file?

The problem: Im running through a LARGE (about 10,000 lines) xhtml file and need to replace every instance of lines beginning <p>~

The following code works but takes a long time mainlly because an in/out operation needs to be carried out on each line. If I could read from a string rather than a file it would take a much shorter time!

Code:
#!/bin/bash 
count=$((0))
>tpf
echo "waiting....."

while read line; do
	echo $line>tpl
	echo $line>tpl
	if grep "<p>~" tpl >/dev/null 2>&1; then
		count=$((count+1))
		sed s/"<p>"/""/ tpl >tpl2;cp tpl2 tpl
		echo "<p style='font-size:1.4em; text-align: center; margin-top: 2em;' id='ch$count'>"$line >> tpf
		#echo $count$line
	else
		echo $line >> tpf
	fi
		
done<testbook

rm tpl
echo "done!! $count lines were changed"
I'm new to bash, and am trying to convert php and c programs to bash.

Any suggestions would be appreciated

Thanks,
Frank
 
Old 04-28-2011, 03:42 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by boumphreyfr View Post
Q: Is there any way to use grep and sed with a string variable rather than with a file?

The problem: Im running through a LARGE (about 10,000 lines) xhtml file and need to replace every instance of lines beginning <p>~

The following code works but takes a long time mainlly because an in/out operation needs to be carried out on each line. If I could read from a string rather than a file it would take a much shorter time!

Code:
#!/bin/bash 
count=$((0))
>tpf
echo "waiting....."

while read line; do
	echo $line>tpl
	echo $line>tpl
	if grep "<p>~" tpl >/dev/null 2>&1; then
		count=$((count+1))
		sed s/"<p>"/""/ tpl >tpl2;cp tpl2 tpl
		echo "<p style='font-size:1.4em; text-align: center; margin-top: 2em;' id='ch$count'>"$line >> tpf
		#echo $count$line
	else
		echo $line >> tpf
	fi
		
done<testbook

rm tpl
echo "done!! $count lines were changed"
I'm new to bash, and am trying to convert php and c programs to bash.

Any suggestions would be appreciated

Thanks,
Frank
Hi,

first of all, you are checking for "<p>~" but your sed only removes "<p>". Do you really want to keep the "~"? Anyway, try this while-loop
Code:
while read line; do
	if [[ $line =~ ^\<p\>~ ]]; then
		echo "${line/<p>/<p style='font-size:1.4em; text-align: center; margin-top: 2em;' id='ch$count'>}" >> tpf
		#echo $count$line
	else
		echo $line >> tpf
	fi
		
done<testbook
Or how about just:
Code:
sed "/<p>~/ s/<p>/<p style='font-size:1.4em; text-align: center; margin-top: 2em;' id='ch$count'> " testbook > tpf
It might help if you post a small sample file and what you expect as output. I am having difficulties to understand what exactly you are trying to do.
 
Old 04-28-2011, 03:57 PM   #3
cmnorton
Member
 
Registered: Feb 2005
Distribution: Ubuntu, CentOS
Posts: 585

Rep: Reputation: 35
The Answer Might Be Pipes

I believe you are asking how grep and sed can work on the variable's contents. Here is a simple example of using grep with a test string. If you back tick your grep expression and assign it to a variable, you can test its contents.

Code:
TEST_STRING="This is a test."

RC=`echo $TEST_STRING | grep This`

if [ ! -z $? ]; then
    echo "This is contained in the test string."
fi
 
Old 04-29-2011, 09:07 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by boumphreyfr View Post
use grep and sed with a string variable rather than with a file?
Code:
echo "$variable" | sed '...'
Or if you want to save the output to another variable:

Code:
output=$(echo "$variable" | sed '...')
 
Old 04-29-2011, 04:26 PM   #5
boumphreyfr
LQ Newbie
 
Registered: Apr 2011
Distribution: lubuntu
Posts: 14

Original Poster
Rep: Reputation: 0
Thank you for all your replies, they taught me more than any book could!
When I entered:
Code:
#!/bin/bash 
	myvariable="Black Hat"
		echo $myvariable
		echo "$myvariable" | sed 's/Black/White/'
#stout should be 'White hat'

#enter result to new var and test
	mynewvariable=$(echo "$myvariable" | sed 's/Black/Red/')
	echo $mynewvariable
#stout should be 'Red hat'
exit
I got exactly the result I wanted!
 
  


Reply

Tags
bash, grep, loop, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 11:05 AM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 01:34 PM
grep, sed, awk or tr - searching words in a string hal8000b Programming 2 03-06-2009 08:04 PM
sed/grep new lines in text hk20 Linux - Newbie 2 09-13-2008 06:47 PM
awk/sed to grep the text ahpin Linux - Software 3 10-17-2007 12:34 AM


All times are GMT -5. The time now is 10:58 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration