Re: OT: Which tool, and how, to get partial string from file?
In article <[🔎] 412699A2.8030407@acu.edu>,
Kent West <westk@acu.edu> wrote:
>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
That's the complete wrong way to use '[' !
You want something like:
WORD=`sed -ne 's/^.*\(icewm\).*$/\1/p' < /home.local/snert/.xinitrc`
if [ -n "$WORD" ]
then
....
For a primer on how to use '[', do "man test".
>>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"
I wrote
if grep -q word file
Why change it to
if [ `grep -q icewm /home.local/snert/.xinitrc` ]
? That means something completely different.
"sh" scripts are a programming language. They do what you say,
not what you mean ;)
After 'if' comes a command. The exit status of the command is used
as return value: 0 is true, 1 (or any non-zero value) is false.
The '[' thing is just a command. Look:
$ ls -l /usr/bin/[
-rwxr-xr-x 1 root root 23928 Jul 16 13:37 /usr/bin/[*
It's also known as 'test'. See "man test".
But you can put other commands there, like grep. "grep -q" exits
with exit-status 0 (true) on a match. So that's why I said
if grep -q word file
and that's why it's completely different from if [ grep ...
Mike.
--
The question is, what is a "manamanap".
The question is, who cares ?
Reply to: