Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Fri 22 Aug 2014, 04:01
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
grep -n "$frustration" = "$true"
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [15 Posts]  
Author Message
H4LF82


Joined: 02 Oct 2012
Posts: 124

PostPosted: Tue 28 May 2013, 14:53    Post subject:  grep -n "$frustration" = "$true"  

Okay so I am TRYING to wrap my brain around this...its now lunch time and im a little closer to my days goal, but not entirely there, yet....

data1.txt file contains one line of string text; "Hello".
data2.txt file contains multiple lines of string text, including "Hi, Hello, Greetings," and "Salutations".

I want to compare the single line from the first file to the multiple lines of the second file and return a true false value for a matching string.

but I seem to be missing something...

Code:

set -eu
matched=$false
string=./Data1/data1.txt
   if grep -n "$string" ./Data1/data2.txt;
    then $matched=$true
    ./messagebox;;
   else
   echo "failure"
fi


Would someone be so kind as to tell me the error of my ways, please and thank you?! Very Happy
Back to top
View user's profile Send private message 
musher0


Joined: 04 Jan 2009
Posts: 4230
Location: Gatineau (Qc), Canada

PostPosted: Tue 28 May 2013, 20:00    Post subject:
Subject description: One-liner Solution
 

Hi, H4LF82 !

In the form of a one-liner:
Code:
A="data1.txt";B="data2.txt";C="`cat $A`";D="`grep -n "$C" $B | cut -d: -f1`";echo;echo "Your word \"$C\" in file \"$A\" is also on line $D of file \"$B\".";echo


where file data1.txt contains
Hello

and

file data2.txt contains

Greetings
Hello
Hi
Salutations

I hope that fits the bill.

Regards.

musher0

_________________
"Logical entities must not be multiplied needlessly." / "Il ne faut pas multiplier les êtres logiques inutilement." (Ockham)
Back to top
View user's profile Send private message Visit poster's website 
H4LF82


Joined: 02 Oct 2012
Posts: 124

PostPosted: Tue 28 May 2013, 20:22    Post subject:  

THANK YOU musher0!!

I had already tried umpteen different ways to do that--all of them failing miserably! I will be deciphering that one liner for the rest of the evening in an effort to understand it. Very Happy

You have my gratitude!!
Cheers!
Back to top
View user's profile Send private message 
musher0


Joined: 04 Jan 2009
Posts: 4230
Location: Gatineau (Qc), Canada

PostPosted: Wed 29 May 2013, 01:09    Post subject:  

My pleasure! Smile
_________________
"Logical entities must not be multiplied needlessly." / "Il ne faut pas multiplier les êtres logiques inutilement." (Ockham)
Back to top
View user's profile Send private message Visit poster's website 
L18L

Joined: 19 Jun 2010
Posts: 2505
Location: Burghaslach, Germany somewhere also known as "Hosla"

PostPosted: Wed 29 May 2013, 03:22    Post subject: Re: grep -n "$frustration" = "$true"  

H4LF82 wrote:
Would someone be so kind as to tell me the error of my ways, please and thank you?! Very Happy

As most one liners are for advanced users here a multi line version of your script.
Some of your lines commented #
Hope that helps
Code:
#!/bin/sh
#matched=$false
matched=false
#string=./Data1/data1.txt
string="`cat ./Data1/data1.txt`"
   if grep -n "$string" ./Data1/data2.txt;
    #then $matched=$true
    then matched=true
    #./messagebox;;
    echo "match"
   else
   #echo "failure"
   echo "no match"
fi


Syntax error was ;;

"$frustration" = "$true"
is true because the variables frustration and true both are ""' Wink

http://murga-linux.com/puppy/viewtopic.php?t=44123
Back to top
View user's profile Send private message 
H4LF82


Joined: 02 Oct 2012
Posts: 124

PostPosted: Wed 29 May 2013, 14:13    Post subject:  

Oh, i see. Thank you L18L! That makes much more sense...

frustration=true

not $frustration=$true
not $frustration=true
not frustration =$true

what I still fail to understand though, is why the need for cat? And if I am going to run a script such as ./messagebox (instead of echo "match") is the ;; not necessary?

why concatenate 1 file with 1 line? Common sense says there is nothing to "join together, as in a chain", as cat would suggest...

To what would we be cat'n it to, exactly? itself? the shell? I obviously do not understand the subtleties of concatenation with regard to bash...

I guess I know what I am looking up this afternoon! Very Happy

THANK YOU AGAIN GUYS! You always point me in the right direction Smile
Back to top
View user's profile Send private message 
H4LF82


Joined: 02 Oct 2012
Posts: 124

PostPosted: Wed 29 May 2013, 15:13    Post subject:  

it needs cat for the line number!

now I feel foolish...

Very Happy
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 5016
Location: Arizona, U.S.A.

PostPosted: Thu 30 May 2013, 00:02    Post subject:  

I think the command: "comm" will do the same.

Except it can only use files for input ( in this case it`s good...).
Back to top
View user's profile Send private message 
L18L

Joined: 19 Jun 2010
Posts: 2505
Location: Burghaslach, Germany somewhere also known as "Hosla"

PostPosted: Thu 30 May 2013, 03:33    Post subject: ;;  

H4LF82 wrote:
...is the ;; not necessary?


The only case I know where to use ;; is with
case
esac

Code:
string="`cat ./Data1/data1.txt`"
while read LINE; do
 case $LINE in
  $string) matched=true
           break 
           ;;
  *)       matched=false ;;
 esac
done < ./Data1/data2.txt
echo "matched=$matched"
Back to top
View user's profile Send private message 
H4LF82


Joined: 02 Oct 2012
Posts: 124

PostPosted: Thu 30 May 2013, 12:13    Post subject:  

;; is straight from Xdialog examples...

http://xdialog.free.fr/

Code:

#!/bin/sh

result=`Xdialog --title "XDIALOG BUILDLIST" --backtitle "A user-built list" \
      --stdout --separator "|" \
      --buildlist "hello, this is a --buildlist..." 0 0 6 \
             "1" "Item n° 1" "on" \
             "2" "Item n° 2" "off" \
             "3" "Item n° 3" "on" \
             "4" "Item n° 4" "on" \
             "5" "Item n° 5" "off" \
             "6" "Item n° 6" "on"`

retval=$?
case $retval in
  0)
    echo "The user-built list is '$result'.";;
  1)
    echo "Cancel pressed.";;
  255)
    echo "Box closed.";;
esac


I figured that if Xdialog was using it, it probably knew more than I did.

I am still not sure why tho...

What I AM sure of is if I am going to add to Xdialog, I have to use ;; and &&, as in...

Code:
retval=$?
case $retval in
  0)
    echo "The user-built list is '$result'."&&
    echo "some other stuff."&&
    ./newmessagebox;;
  1)
    echo "Cancel pressed.";;
  255)
    echo "Box closed.";;
esac


and if I do not add &&, it wont work.

THAT is where ;; came from....I cannot say why or if it is necessary, only that if I do not add it, or rather add && in place of ;; when adding to Xdialog after the case line, it wont work.

Since I am not using case here, i suppose it is not necessary then? ;; is specific to case?
Back to top
View user's profile Send private message 
L18L

Joined: 19 Jun 2010
Posts: 2505
Location: Burghaslach, Germany somewhere also known as "Hosla"

PostPosted: Thu 30 May 2013, 13:41    Post subject:  

H4LF82 wrote:
....and if I do not add &&, it wont work...


With me it was working with and without && Wink
Back to top
View user's profile Send private message 
musher0


Joined: 04 Jan 2009
Posts: 4230
Location: Gatineau (Qc), Canada

PostPosted: Thu 30 May 2013, 13:45    Post subject:  

H4LF82 wrote:
;; is straight from Xdialog examples...

http://xdialog.free.fr/


Hum... That xdialog utility is from mid-April 2000. Many programming usages may have changed since then.

H4LF82 wrote:

(...)
;; is specific to case?


As far as I know.

Best regards.

musher0

_________________
"Logical entities must not be multiplied needlessly." / "Il ne faut pas multiplier les êtres logiques inutilement." (Ockham)
Back to top
View user's profile Send private message Visit poster's website 
H4LF82


Joined: 02 Oct 2012
Posts: 124

PostPosted: Thu 30 May 2013, 14:51    Post subject:  

April 2000!!??

Well that just goes to show you what happens when the blind guy tries to go bash programming on his own!

evidently I am going to require more "hand holding" from you guys than the average bear. Apologies for that. is it any wonder that every time I add a new line of code, my script stops working...maybe because I am using 13 year old examples?

Geesh---thanks Google. I thought you were my friend...

So at the end of the day, after making what I thought was much progress, I learn that it was progress 13 years ago---today not so much. Typical.

For those of you that care to know what I am doing....I am trying to code an Intelligent AI as a "back end" for a STT engine "front end" to help decode "spoken input"....so the computer can tell the difference between a greeting and a command.

http://www.murga-linux.com/puppy/viewtopic.php?t=86471

Evidently I am failing miserably!

Any thoughts?
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 886

PostPosted: Thu 30 May 2013, 17:51    Post subject:  

H4LF82,

There is nothing wrong with that example (except for some non-UTF-8 encoding)

If you wish to add more commands to case statements -
Code:
 retval=$?
case $retval in
  0)
    echo "The user-built list is $result" ; echo hello
    echo "some other stuff."
    ls ;;
  1)
    echo "Cancel pressed.";;
  255)
    echo "Box closed." ; echo why? ; echo "why not?" ;;
esac


Commands on the same line are separated by a ;
Every case statement is terminated by a double ;;

Cheers,
s
Back to top
View user's profile Send private message 
H4LF82


Joined: 02 Oct 2012
Posts: 124

PostPosted: Thu 30 May 2013, 18:14    Post subject:  

thank you for clearing that up for me seaside!

i was staggeringly confused by the whole thing.

that makes much more sense.

Cheers!
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [15 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0865s ][ Queries: 12 (0.0059s) ][ GZIP on ]