Simple Bash Script

The only bash scripting I’ve done is just executing basic commands more like a Windows batch file. I’m not really familiar with actual bash scripting, and the conventions of opening files, parsing text, comparing it, and outputing text.

I need a really basic script that will open two text files. Each has a list of numbers with line breaks. I’d like the script to output a list of numbers that are in the first file and aren’t in the second file.

So if I have two files, with one that says:

1
2
3
4
5

and the second file says:

1
3
5
7
9

The output would be:

2
4

If the lines are sorted in both files, just use the comm command:

comm -23 file1 file2

If not sorted, bash is not the appropriate tool as you have to keep track of which numbers were already seen and an associative array available in language like Perl, Python or Ruby is what you need. You could fake an associative array in bash, but it would be ugly.

diff file1 file2|awk ‘{print $2}’|sed /^$/d

This may not the best way to get this done but it works.

enderandrew wrote:

>
> The only bash scripting I’ve done is just executing basic commands more
> like a Windows batch file. I’m not really familiar with actual bash
> scripting, and the conventions of opening files, parsing text, comparing
> it, and outputing text.
>
> I need a really basic script that will open two text files. Each has a
> list of numbers with line breaks. I’d like the script to output a list
> of numbers that are in the first file and aren’t in the second file.
>
> So if I have two files, with one that says:
>
> 1
> 2
> 3
> 4
> 5
>
> and the second file says:
>
> 1
> 3
> 5
> 7
> 9
>
> The output would be:
>
> 2
> 4

So… put the first set of numbers into f1.txt and the second into f2.txt
and then do:

fgrep -v -f f2.txt f1.txt

Does that work for your real case?

using just one awk command. No need to use too many.


$ awk 'FNR==NR{f$1];next}(!($1 in f)) ' file2 file1
2
4


Thats done quite easy:


cat file1 file2 file2| sort | uniq -u

This will always show you the entries from file1 which are not in the second file!

It just doubles the entries from the second file, so that these doubles are gone after “uniq -u” -> and that will take the same entries from the first file right away with it.
Whats leftover are the entries from the first file which are not in the second file!

:slight_smile:

diff file1 file2 | awk '/</ { print $2 }'

I didn’t test it, but grep could do the job, too, even if the files aren’t
sorted. However you have to make sure the file2 does not contain any empty
lines.


grep -F -x -v -e file2 file1