2

I've come across some weird problem that I can't solve by myself. I've got two files, content as follows:

File1.txt

Butterscotch;lkt+4S7Yhzf/g
Sox;WrWMzN6HD
Table;K4yPIK+SFfTFs
tram;YaY3opP9ie
number;e/WwiF5aihUY
Buttler;Nsadlkt+4S7Yhz
wzahwahwah;PYaY3opP9ieH

File2.txt

Butterscotch;lkt+4S7Yhzf/g
Sox;WrWMzN6HD
Table;K4yPIK+SFfTFs
tram;YaY3opP9ie
number;e/WwiF5aihUY

I need to find differences between those two files but only for entries that are before semicolon, so I first use awk to get those entries:

awk 'BEGIN {FS=";"}; {print $1}' File1.txt > Output1.txt
awk 'BEGIN {FS=";"}; {print $1}' File2.txt > Output2.txt

New files contain entries as follows:

Output1.txt

Butterscotch
Sox
Table
tram
number
Buttler
wzahwahwah

Output2.txt

Butterscotch
Sox
Table
tram
number

Then I use grep -Fxvf Output2.txt Output1.txt > Diff.txt to print out the differences between Output2.txt and Output1.txt so my file looks like this:

Diff.txt

Buttler
wzahwahwah

Now the tricky part. I need to find and print out to another file whole lines from File1.txt that start with entries from Diff.txt. I'm declaring a variable and do something like this:

variable=$(cat Diff.txt)
grep $variable Output1.txt > Delete.txt

So for this example I should get something like this:

Delete.txt

Buttler;Nsadlkt+4S7Yhz
wzahwahwah;PYaY3opP9ieH

But unfortunately I get an error:

grep: wzahwahwah: No such file or directory

File Delete.txt instead of desired data contains:

/home/working/File1.txt:Buttler;Nsadlkt+4S7Yhz

So it's far from what I need to get. What I've noticed is that when I remove from File1.txt

wzahwahwah;PYaY3opP9ieH

I get no errors and my desired file looks like:

Buttler;Nsadlkt+4S7Yhz

Any ideas why this isn't working?

2 Answers 2

4

Instead of your cat and subsequent grep use this grep:

grep -Ff Diff.txt File1.txt
Buttler;Nsadlkt+4S7Yhz
wzahwahwah;PYaY3opP9ieH
Sign up to request clarification or add additional context in comments.

4 Comments

I believe problem is reading file content in a variable and using that variable unquoted afterwards.
Only thing that bothers me now is why did grep work for other entries, but didn't work for wzahwahwah;PYaY3opP9ieH. Nevertheless once again - thank you.
Hi, It's me again ;) Everything was working great 'till today. I've noticed that when inside my Diff.txt I have, for example, only one letter t, my Delete.txt file would have all entries that have that letter (so there would be Butterscotch, tram, Buttler). Is there a way to secure this, so grep would get only whole words?
Sure you can use: grep -wFf Diff.txt File1.txt Here -w for whole word match.
2

When you do this

variable=$(cat Diff.txt)
grep $variable Output1.txt > Delete.txt

After the variable is expanded, since it is unquoted the grep command turns into

grep Buttler wzahwahwah Output1.txt > Delete.txt

Grep only uses the first argument as the pattern, and all following args are filenames. Grep found "Buttler" in Output1.txt, but did not find a file named "wzahwahwah"

1 Comment

Thank You for explanation. I've checked all my other scripts where I use grep as I mentioned above, and I'm using it only on files that contain one variable (eg. 0 or 1 to check if script is running or not). So I've made wrong assumption that this will also work with other variables :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.