Comparing Output of Two Files in Unix/Bash - unix

I'm just starting to learn unix and want to create a system that checks homework assignments. Each assignment consists of uploading a file consisting of multiple one-line commands that address the homework question. In essence, this system needs to compare two files, and compare the output of each command (or each line in the file), and check if respective lines have the same output.
I've been trying to do this with a while loop:
answer=$(cat Answer_HW1)
for student in $(cat roster2)
do
cat HW1\_$student | while read input
do
if [[ $($input = $($answer) ]]
then
echo correct
else
echo incorrect
fi
done
done
The above code isn't very comprehensive and complete (and doesn't do exactly what I intended it to), but it is able to loop through each student's assignment (in the roster file in the same directory) and loop through each line of code–how would I be able to compare each line with another file?

Related

unix compare lists of file names

I believe similar questions have been answered on SO before. I cant find any that seem to match to my particular situation, though I am sure many others have faced this scenario.
In an FTP session on Red Hat I have produced a list of file names that reside on the server currently. The list contains the file names and only the file names. Call this file1. Perhaps it contains something like:
513569430_EDIP000754535900_MFC_20190618032554.txt
blah.txt
duh.txt
Then I have downloaded the files and produced a list of successfully downloaded files. As well, this list contains the file names and only the file names. Call this file2. Perhaps it contains something like:
loadFile.dat
513569430_EDIP000754535900_MFC_20190618032554.txt
localoutfile.log
Now I want to loop through the names in file1 and check if they exist in file2. If exists I will go back to FTP server and delete the file from server.
I have looked at while loops and comm and test command, but I just cant seem to crack the code. I expect there are many ways to achieve this task. Any suggestions out there or working references?
My area of trouble is really not the looping itself but rather the comparing of contents between 2 files.
comm -1 -2 file1 file2 returns just the lines that are identical in both files. This can be used as the basis of a batch command file for sftp.
From the comments to the question, it seems that line-endings differ for the two files. This can be fixed in various ways, simplest probably being with tr. comm understands - as a filename to mean "read from stdin".
For example:
tr -d '\r` file1 | comm -1 -2 - file2
If file1 or file2 are not sorted, this must be corrected for comm to operate properly. With bash, this could be:
comm -1 -2 <( sort file1 | tr -d '\r' ) <( sort file2 )
With shells that don't understand the <( ... ) syntax, temporary files may be used explicitly.
Thank you for the advice #jhnc.
After giving this some deeper consideration and conversation, I realized that I don't even need to do this comparison. After I download the files I just need to produce the list of successful downloads. Then I can go and delete from server based on list of successful downloads.
However, I am still interested to know how to compare with the '\r \n' vs '\n' line ending situation

Multiple Value for command line option in R

I am writing a code in R and I want to input values through the command line.
I am using get opt and I intend to read the values as such:
ScriptName -n File1 File2 -w 2...
I need an option which can read more than one value, in this case more than one file name, but I discovered that getopt doesn't allow this.
Any idea or any other package that will allow this?

Finding the differences of two variables containing strings unix

How i use diff for variables instead of files.
All tutorials have examples with files but not with variables.
I want it to print just the differences.
for example:
TEXTA=abcdefghijklmnopqrstuvxyz; TEXTB=abcdefghijklmnopqrstuvxyr
diff is a utility to compare two files. If you really want to compare two variables, and you are using bash for your shell, you can "fake it" this way:
diff <(echo ${TEXTA}) <(echo ${TEXTB})
Otherwise, you can just write your variables to two temporary files and compare them.
However, note that in your example, since each variable is a single line, it'll just tell you that they're different, unless you use a version of diff that will show you the specific positions in the line where they differ.
I would use sdiff.
sdiff <(echo $TEXTA) <(echo $TEXTB)
sdiff points out just the differences between the two strings and shows them side-by-side separated by |.
abcdefghijklmnopqrstuvxyz | abcdefghijklmnopqrstuvxyr
This can be useful when your string is too long. sdiff would highlight only the part of the string that is different.

Split files linux and then grep

I'd like to split a file and grep each piece without writing them to indvidual files.
I've attempted a couple variations of split and grep and no such luck; any suggestions?
Something along the lines of:
split -b SIZE filename | grep "string"
I've attempted grep/fgrep to find the string but my shell complains that the files are too large. See: use fgrep instead
There is no point in splitting the file if you plan to [linearly] search each of the pieces anyway (assuming that's the only thing you are doing with it). Consider running grep on the entire file.
If however you plan to utilize the fact that the file is split later on, then the typical way would be:
Create a temporary directory and step into it
Run split/csplit on the original file
Use for loop over written fragment to do your processing.

In what order does cat choose files to display?

I have the following line in a bash script:
find . -name "paramsFile.*" | xargs -n131072 cat > parameters.txt
I need to make sure the order the files are concatenated in does not change when I use this command. For example, if I run this command twice on the same set of paramsFile.*, parameters.txt should be the same both times. My question is, is this the case? And if it isn't, how can I make sure it is?
Thanks!
Edit: the same question goes for xargs: would that change how the files are fed to cat?
Edit2: as William Pursell pointed out, this question is actually about find. Does find always return files in the same order?
From description in man cat:
The cat utility reads files sequentially, writing them to the standard
output. The file operands are processed in command-line order.
If file is a single dash (`-') or absent, cat reads from the standard input. If file is a UNIX domain socket, cat connects to it
and
then reads it until EOF. This complements the UNIX domain binding capability available in inetd(8).
So yes as long as you pass the files to cat in the same order every time you'll be ok.

Resources