Shell line command Sorting command [duplicate] - shell

This question already has answers here:
find difference between two text files with one item per line [duplicate]
(11 answers)
Closed 9 years ago.
I have a Masters.txt (all records) and a New.txt file. I want to process New.txt against Masters.txt and output all the lines from New.txt that do not exist in Masters.txt
i'm not sure if this is something the sort -u command can do.

Sort both files first using sort and then use the comm command to list the lines that exist only in new.txt and not in masters.txt. Something like:
sort masters.txt >masters_sorted.txt
sort new.txt >new_sorted.txt
comm -2 -3 new_sorted.txt masters_sorted.txt
comm produces three columns in its output by default; column 1 contains lines unique to the first file, column 2 contains lines unique to the second file; column 3 contains lines common to both files. The -2 -3 switches suppress the second and third columns.

see the linux comm command:
http://unstableme.blogspot.com/2009/08/linux-comm-command-brief-tutorial.html

Related

bash: sort applied to a file returns right results as terminal output, but does change the file itself [duplicate]

This question already has answers here:
How can I use a file in a command and redirect output to the same file without truncating it?
(14 answers)
Closed 8 months ago.
I am using this command
sort -k1 -n source-g5.txt
to sort the content of file tmp-source-g5.txt (n rows, 2 columns) according to the numerical value of the first column.
When I run that line, the terminal prints out the desired result, but as I try to save the result into the same file,
sort -k1 -n source-g5.txt > source-g5.txt
the file shows no difference from before.
What am I doing wrong?
SOLVED
From this thread it turns out that redirecting the output of sort into the same file from which sort reads as source will not work since
the shell is makes the redirections (not the sort(1) program) and the
input file (as being the output also) will be erased just before
giving the sort(1) program the opportunity of reading it.
So I have split my command into two
sort -k1 -n source-g5.txt > tmp-source-g5.txt
mv tmp-source-g5.txt > source-g5.txt

Cut columns two and three using bash [duplicate]

This question already has answers here:
How to extract one column of a csv file
(18 answers)
Closed 1 year ago.
I have a .csv file with three columns. I want to keep the first column only. I have been trying to work with a command similar to the one below.
cut -f 1,4 output.csv > output.txt
No matter what I do, my output remains the same- giving me all three columns. Can anyone give me some insight?
Thanks!
read file one line at a time, trim everything right of that first comma:
while read -r line; do echo ${line%%,*}; done < output.csv > output.txt

Sort tow file using comm and sort function [duplicate]

This question already has answers here:
Fast way of finding lines in one file that are not in another?
(11 answers)
Difference between sh and Bash
(11 answers)
Closed 4 years ago.
I want to compare with online users inside freeradius database and users inside mikrotik devices , so I retrieved online users from freeradius and insert them inside file1, get online users from mikrotik and insert them inside file2, now I want to get the users that are found in file2 but not found inside file1
so I used this code
I am trying to get users that are found in file2 but not found in file 1.
comm -23 <(sort < file2) <(sort file1)
but I had this error
Syntax error: "(" unexpected.
So where is my error?
actually I don't know much things about bash and shell , so please help me
Assuming your files contain 1 username per line in file1, a simple one liner like the below should do what you need, without having to sort the files :
while read line; do if grep -q "$line" file2; then echo "user in both :" "$line"; fi; done < file1

Keep text file rows by line number in bash [duplicate]

This question already has answers here:
Print lines indexed by a second file
(4 answers)
Closed 8 years ago.
Have two files. the first file (called k.txt) looks like this
lineTTY
lineRTU
lineERT
.....furtherline like this...
The other file (called w.txt) contains indices of rows which shall be kept. It looks like:
2
9
12
The indices in the latter file are sorted. Is there a way to do that in bash quickly as my file is large over 1 million rows?
Every line is the row of a matrix in a text file and only specific rows specified in the other file should be in the matrix.
I think you need here is:
cat w.txt | xargs -i{} sed -n '{}p' k.txt
if you must also sort a file, then
sort -g w.txt | xargs -i{} sed -n '{}p' k.txt

Shell scripts: diff command

I have a small question about the diff command. I am comparing two ascii files to check if there's a difference between them and print out the output to another ascii. However my problem is that the order of the contents of the files shouldn't matter, for example let's say we have:
file1.txt with
1
2
3
4
5
6
7
file2.txt with
1
3
2
4
so that when i do a "diff" on them the output should just be:
5
6
7
i.e the order of the two files shouldn't matter, it should just print out the whatever it is that is different between the two files.
How about:
comm -3 <(sort file1.txt) <(sort file2.txt)
First you have to sort both the files and then input the files into diff command or comm command. There are many options for doing so.
Instead of providing a command that would do your job. I can provide you a link that can make you understand how it works.
Here it is Click here

Resources