How to store result of diff in Linux - linux

How to get the result on another file after applying diff to file A.txt and B.txt.
Suppose File A.txt has:
a
b
c
File B.txt has:
a
b
on running
diff A.txt B.txt
It gives result as c, but how to store it in a file C.txt?

The diff utility produces its output on standard output (usually the console). Like any UNIX utility that does this, its output may very simply be redirected into a file like this:
diff A.txt B.txt >C.txt
This means "execute the command diff with two arguments (the files A.txt and B.txt) and put everything that would otherwise be displayed on the console into the file C.txt". Error messages will still go to the console.
To save the output of diff to a file and also send it to the terminal, use tee like so:
diff A.txt B.txt | tee C.txt
tee will duplicate the data to all named files (only C.txt here) and also to standard output (most likely the terminal).

Using > you can redirect output to a file. Eg:
diff A.txt B.txt > C.txt
This will result in the output from the diff command being saved in a file called C.txt.

Use Output Redirection.
diff file1 file2 > output
will store the diff of file1 and file2 to output

There are some files that diff may not do well with the output, like block special files, character special files, and broken links. The output due to differences with these may go to standard error.
Interestingly, when I redirected standard error, I still missed some things:
diff -qr <DirA> <DirB> 2>&1 > mydiff.txt
The only way to see the results of everything was to:
diff -qr <DirA> <DirB> |tee mydiff.txt
I was comparing the results of a live-cd mounted directory after copying to an external HD

Related

In linux, how to save only the command into file without output

I just want to save only the command into file, without the long output. For example, i type ls, terminal output a.txt b.txt, if i type ls > command.txt, the content of command.txt will be
a.txt
b.txt
command.txt
But what i want is :
ls
Can we achieve this ?
All shells store the history of commands run in dotfiles in the home directory.
Assuming you're using bash, i think you should be looking at the ~/.bash_history file

Is it possible to use a P4 command to `Mark for Delete` every file that is "missing"?

Say I had checked in the files:
a.txt
b.txt
c.txt
I checked them all out so that I could mass edit them by dumping files from another folder:
cp -R folder-one/* folder-two/
The result is:
a.txt (updated)
b.txt (deleted)
c.txt (no changes)
Now I Revert Unchanged Files and intend to submit:
a.txt (updated)
b.txt (deleted)
But I can't because now b.txt is "missing".
Is it possible to use a P4 command to Mark for Delete every file that is "missing"?
Easiest fix is just to revert all of the files that you've opened (with the -k flag so you keep your local changes) and let reconcile figure it out from square one:
p4 revert -k ...
p4 reconcile ...

Creating a file by merging two files

I would like to merge two files and create a new file using Linux command.
I have the two files named as a1b.txt and a1c.txt
Content of a1b.txt
Hi,Hi,Hi
How,are,you
Content of a1c.txt
Hadoop|are|world
Data|Big|God
And I need a new file called merged.txt with the below content(expected output)
Hi,Hi,Hi
How,are,you
Hadoop|are|world
Data|Big|God
To achieve that in terminal I am running the below command,but it gives me output like below
Hi,Hi,Hi
How,are,youHadoop|are|world
Data|Big|God
cat /home/cloudera/inputfiles/a1* > merged.txt
Could somebody help on getting the expected ouput
Probably your files do not have newline characters. Here is how to put the newline character to them.
$ sed -i -e '$a\' /home/cloudera/inputfiles/a1*
$ cat /home/cloudera/inputfiles/a1* > merged.txt
If you are allowed to be destructive (not have to keep the original two files unmodified) then:
robert#debian:/tmp$ cat fileB.txt >> fileA.txt
robert#debian:/tmp$ cat fileA.txt
this is file A
This is file B.

Comparing 2 files in linux for different word

I have two files like below
file1 has the below words
word23.cs
test.cs
only12.cs
file 2 has the below words
word231.cs
test.cs
only12.cs
The above words might change, And now i need to compare the two files using script or linux command to get the different word , i need to compare the file2 with file1 and need to get the output as word23.cs
Thank you
Use the "diff" command to compare 2 files:
$ diff a.txt b.txt
Or, for a unified diff:
$ diff -u a.txt b.txt
Use -u0 for a unified diff without context.
You can use comm, diff or cmp command to find different word from files.
Also this trick can work with a grep command as follows
grep -Fwf file1 file2

Bash Sorting Redirection

What are the differences between sort file1 -o file2 and sort file1 > file2 ? So far from what I have done they do the same thing but perhaps I'm missing something.
Following two commands are similar as long as file1 and file2 are different.
sort file1 -o file2 # Output redirection within sort command
sort file1 > file2 # Output redirection via shell
Let's see what happens when input and output files are same file i.e. you try to sort in-place
sort file -o file # Works perfectly fine and does in-place sorting
sort file > file # Surprise! Generates empty file. Data is lost :(
In summary, above two redirection methods are similar but not the same
Test
$ cat file
2
5
1
4
3
$ sort file -o file
$ cat file
1
2
3
4
5
$ sort file > file
$ cat file
$ ls -s file
0 file
The result is the same but in the case of -o file2 the resulting file is created by sort directly while in the other case, it is created by bash and filled with the standard output of sort. The xfopen defined in line 450 of sort.c in coreutils treats both cases (stdout and -o filename) equally.
Redirecting the standard output of sort is more generic as it could be redirected to another program with a | in place of a >, which the -o option makes more difficult to do (but not impossible)
The -o option is handy for in place sorting as the redirection to the same file will lead to a truncated file because it is created (and truncated) by the shell prior to the invocation of sort.
There is not much difference > is a standard unix output redirection function. That is to say 'write your output that you would otherwise display on the terminal to the given file' The -o option is more specific to the sort function. It is a way to again say 'write the output to this given file'
The > can be used where a tool does not specifically have a write to file argument or option.

Resources