using sort command to sort by colums [duplicate] - bash

This question already has answers here:
Linux shell sort file according to the second column?
(4 answers)
Closed 8 years ago.
I have a simple data set. example:
IP address,number of times it appears
192.168.0.10,11
192.168.0.1,15
192.168.0.120,9
I want to use the sort command to sort these by the largest number of times the IP has been seen. the output should look like:
192.168.0.1,15
192.168.0.10,11
192.168.0.120,9

Use -k to specify the column. Don't forget about -n to use numeric ordering.
sort -n -k2 file.txt

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

storing result of awk in shell script [duplicate]

This question already has answers here:
How to split a string into an array in Bash?
(24 answers)
How do I split a string on a delimiter in Bash?
(37 answers)
Closed 3 years ago.
I am new to shell script and I have started it today itself. Can anyone tell me how to store and retrieve the output of this command:
cat /proc/cpuinfo | grep "MHz" | awk '/cpu MHz/ {print $4}'
This command returns just four decimal numbers but I am struggling to store these four numbers.
I have tried to store them in array but it has not been fruitful for me. Please help!!

How to "grep" lines of a file by range of value [duplicate]

This question already has answers here:
Grep inside all files created within date range
(3 answers)
Closed 5 years ago.
I have a file with timestamps and I want to grep it so that I can see the lines matching 12:30:00 - 12:32:00 . I know that in bash there is the {0..2} for doing something similar but I dont know in grep how to do it?
At the moment I am doing grep 2015-01-12 12:3{0..2} but it doesnt work?
you can use the following for this.
grep '2015-01-12 12:3[0-2]'
or
grep '2015-01-12 12:3[0,1,2]'
I hope it helps.

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 line command Sorting command [duplicate]

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

Resources