Re: OT: Which tool, and how, to get partial string from file?
Miquel van Smoorenburg wrote:
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".
I think I must have some mental block against understanding the syntax
of "regular expressions" and/or "test" and/or "conditionals" and/or
single-quotes-vs-double-vs-backtics and/or something. It seems every
time I try to do some sort of scripting over the past several years
(only 3 or 5 times a year, so not often), I have to google and "man" and
study and ask, to essentially figure out the same sorts of things. So I
sure am glad there's folks like you with whom it all "clicks" who
volunteer their expertise.
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.
Because like I say above, I google around trying to find my own way
before I come ask, and that just leaves me confused. I thought the "if
grep -q word file" was sort of a cross between psuedo-code and actual
syntactical use, leaving the exact syntax for me to figure out, based on
my previous efforts to find my own answers. Apparently I should have
accepted the command as more literal than I did. I'm a goofball ;-)
"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 "Conditional Expressions" section of "man bash" wasn't explanatory
enough for me to figure out if-then, so I went a' googling, and found
things like this, which is from the "BASH Programming - Introduction
HOW-TO" that I found at
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html:
A word about syntax:
The base for the 'if' constructions in bash is this:
if [expression];
then
code if 'expression' is true.
fi
followed immediately by several examples that look like this:
#!/bin/bash
if [ "foo" = "foo" ]; then
Each of these examples has the brackets.
And then in all but one of the examples in the "BASH Guide for
Beginners" at http://tille.soti.org/training/bash/ch07.html#sect_07_01,
there are brackets. The one case that didn't have brackets I assumed was
some sort of special case.
So I got it in my head that the brackets are part of the if-then
statement. If I understand what you're saying, they're not. Whew. My
head is spinning.
Thanks for the input!
--
Kent
Reply to: