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

From: Miquel van Smoorenburg (miquels_at_cistron.nl)
Date: 08/21/04

  • Next message: Hans Oluf Hagen: "Re: Evolution in Norwegian?"
    Date: Sat, 21 Aug 2004 12:36:48 +0000 (UTC)
    To: debian-user@lists.debian.org
    
    

    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 ?
    -- 
    To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org 
    with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
    

  • Next message: Hans Oluf Hagen: "Re: Evolution in Norwegian?"

    Relevant Pages

    • Re: difference between the -o and || conditionals.
      ... you are running a first "[" command with ... equal to the number whose string representation is in its third ... a non-0 exit status otherwise. ... the cat and grep commands are run and its (possibly ...
      (comp.unix.shell)
    • Re: MacTeX 2007 and documentation
      ... may be the most useful command available when doing searches at the ... will find every file on your system that has the text "memoir" on it, ... If locate generates 30 lines of text, where each line is a file, grep ... I need a Unix guru to chat to for half an hour or so to put me straight. ...
      (comp.text.tex)
    • Re: piping multiple selections to mv or cp and exclusion
      ... pipe to shell to execute each command statement ... Unix is indeed lifelong learning :-) ... and the regexp. ... back into the grep statement. ...
      (comp.unix.shell)
    • Re: MacTeX 2007 and documentation
      ... means you need expertise in your host operating system as well. ... locate memoir | grep pdf ... I'm not someone who lives at the command line, ...
      (comp.text.tex)
    • Re: Program to scan directories
      ... Is there a UNIX command which scans directories and execute any other ... the regex you specify here is /not/ given to grep to ... entries (files and subdirectories). ... tell grep to recurse through the kpathsea directory (the *.h regex ...
      (comp.unix.programmer)