[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: Which tool, and how, to get partial string from file?



Miquel van Smoorenburg wrote:

In article <[🔎] 41265EB6.2050202@acu.edu>,
Kent West  <westk@acu.edu> wrote:
A programmer would know this . . , but not me  ;-)

I'm using a script to build a file by concatenating portions of two other files. Then end result needs to be checked to make sure a certain word shows up in a line.

I know that "grep programm <this email message>" would return the line:

A programmer would know this . . , but not me  ;-)

but what I want returned is just the word "programmer".

sed -ne 's/^.*\(word_here\).*$/\1/p' < file
Here's my test script:

#!/bin/bash

if [ sed -ne 's/^.*\(icewm\).*$/\1/p' < /home.local/snert/.xinitrc ] ; then
        echo "Yep"
else
        echo "Nope"
fi

and the result:

snert[@macs51]:/home.local/snert> ./test.sh
./test.sh: [: sed: integer expression expected
Nope


BTW, why not simply

	if grep -q word file
	then
		# word was present in file
		bla
		bla
	fi

My test script:

#!/bin/bash

if [ `grep -q icewm /home.local/snert/.xinitrc` ] ; then
        echo "Yep"
else
        echo "Nope"
fi

and the result:

snert[@macs51]:/home.local/snert> ./test.sh
Nope

and if I remove the backtics:

#!/bin/bash

if [ grep -q icewm /home.local/snert/.xinitrc ] ; then
        echo "Yep"
else
        echo "Nope"
fi

which results in:

snert[@macs51]:/home.local/snert> ./test.sh
./test.sh: [: too many arguments
Nope


and my /home.local/snert/.xinitrc file:

snert[@macs51]:/home.local/snert> cat .xinitrc
icewm
#startkde
#gnome
#fluxbox


Wow! I never would have thought this to be such a difficult question. But thanks everyone, for the suggestions. I appreciate you trying.

--
Kent




Reply to: