LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   using sed and grep with a text string (http://www.linuxquestions.org/questions/linux-newbie-8/using-sed-and-grep-with-a-text-string-877684/)

boumphreyfr 04-28-2011 03:41 PM

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

crts 04-28-2011 04:42 PM

Quote:

Originally Posted by boumphreyfr (Post 4339755)
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.

cmnorton 04-28-2011 04:57 PM

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


MTK358 04-29-2011 10:07 AM

Quote:

Originally Posted by boumphreyfr (Post 4339755)
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 '...')

boumphreyfr 04-29-2011 05:26 PM

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!


All times are GMT -5. The time now is 06:19 PM.