grep -n "$frustration" = "$true"

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
H4LF82
Posts: 123
Joined: Tue 02 Oct 2012, 04:22

grep -n "$frustration" = "$true"

#1 Post by H4LF82 »

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: Select all

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?! :D

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#2 Post by musher0 »

Hi, H4LF82 !

In the form of a one-liner:

Code: Select all

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
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
H4LF82
Posts: 123
Joined: Tue 02 Oct 2012, 04:22

#3 Post by H4LF82 »

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. :D

You have my gratitude!!
Cheers!

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#4 Post by musher0 »

My pleasure! :)
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

Re: grep -n "$frustration" = "$true"

#5 Post by L18L »

H4LF82 wrote:Would someone be so kind as to tell me the error of my ways, please and thank you?! :D
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: Select all

#!/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

User avatar
H4LF82
Posts: 123
Joined: Tue 02 Oct 2012, 04:22

#6 Post by H4LF82 »

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! :D

THANK YOU AGAIN GUYS! You always point me in the right direction :)

User avatar
H4LF82
Posts: 123
Joined: Tue 02 Oct 2012, 04:22

#7 Post by H4LF82 »

it needs cat for the line number!

now I feel foolish...

:D

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#8 Post by sunburnt »

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

Except it can only use files for input ( in this case it`s good...).

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

;;

#9 Post by L18L »

H4LF82 wrote:...is the ;; not necessary?
The only case I know where to use ;; is with
case
esac

Code: Select all

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"

User avatar
H4LF82
Posts: 123
Joined: Tue 02 Oct 2012, 04:22

#10 Post by H4LF82 »

;; is straight from Xdialog examples...

http://xdialog.free.fr/

Code: Select all

#!/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: Select all

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?

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#11 Post by L18L »

H4LF82 wrote:....and if I do not add &&, it wont work...
With me it was working with and without && :wink:

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#12 Post by musher0 »

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
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
H4LF82
Posts: 123
Joined: Tue 02 Oct 2012, 04:22

#13 Post by H4LF82 »

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?

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#14 Post by seaside »

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: Select all

 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

User avatar
H4LF82
Posts: 123
Joined: Tue 02 Oct 2012, 04:22

#15 Post by H4LF82 »

thank you for clearing that up for me seaside!

i was staggeringly confused by the whole thing.

that makes much more sense.

Cheers!

Post Reply