Linux sorting awk command [duplicate] - linux

This question already has answers here:
Sort a text file by line length including spaces
(13 answers)
Closed 9 years ago.
I'm new to linux and checking to see if there is any sort command available for below scenario. I have a file containing lines like below.
this is a 10
this is 5
this 40
this is a boy in 3
this is a boy 6
I would like to sort it like
this 40
this is 5
this is a 10
this is a boy 6
this is a boy in 3
Any help is appreciated!

Assuming all of your data is in a text file called testfile this answer should work:
cat testfile | awk '{ print length, $0 }' | sort -n | cut -d" " -f2-
It comes from this question: Sort a text file by line length including spaces

If you don't care about blank lines you can run
$ sort <filename>
this 40
this is 5
this is a 10
this is a boy 6
this is a boy in 3
sort can read a file and organize the output. If you want to remove the empty lines, one option you have is sed
$ sort test.txt | sed '/^$/d'
this 40
this is 5
this is a 10
this is a boy 6
this is a boy in 3

Related

how to cut the last field using cut linux bash? [duplicate]

This question already has answers here:
How to find the last field using 'cut'
(14 answers)
Closed 4 years ago.
Hello everyone i want to know to cut the last field with separator :
without knowing how many fields that i have any ideas please .
is there any option for command cut .
You can revert the string and then print 1st character. Itself cut can't work from backwards.
echo "Your string ABC" | rev | cut -c 1
Awk is the right tool for this. Try :
ls -lh | awk '{ print $NF }'

Bash: my output is on one line [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 6 years ago.
I have this code that counts the occurrences of different college majors within the file:
#!/bin/bash
file=$1
OUT=$( cat $file | cut -d',' -f3 | sort | uniq -c)
echo $OUT
That produces this output:
4 Computer Information Systems 2 Computer Science 2 History 1 Marketing 2 Social Studies
How do I get the output to look like this:
4 Computer Information Systems
2 Computer Science
2 History
1 Marketing
2 Social Studies
With every major being on its own line?
This may seem like a silly question but I am very new to bash scripting. TIA for any help!
Don't assign the value to a variable, just this would do:
cut -d',' -f3 "$1" | sort | uniq -c

How to get the line number?

i want to get the line number from a file.In a file where 5 dashes(-----) is appear i want to get that line number.
For example
Day to Day
------------------
Month
-----
Jan
feb
mar
Year
-----
2014
2013
2012
so here Month and year have 5 dashes.i want to get that month and year line number.
Thanks.
If you only want the line number, and not the text behind, you can use cut in complement of grep:
grep -n '^-----$' file.txt | cut -f1 -d:
Output:
4
11
You can use awk:
awk '/-{5}/{print NR, $0}' file
2 ------------------
4 -----
11 -----
To get only line numbers use:
awk '/-{5}/{print NR}' file
2
4
11
EDIT: If you want to find lines with exact 5 dashes then use:
awk '/^-{5}$/{print NR}' file
4
11
One of sed's lesser-used commands:
sed -n '/^-----$/=' file
you can use the -n option with grep to make grep output the line number. So, it would be something like this:
grep -n ^-----$ filename.txt

Insert character in a file with bash

Hello I have a problem in bash.
i have a file and i am trying insert a point in the final line of each line:
cat file | sed s/"\n"/\\n./g > salida.csv
but not works =(.
Because i need count the lines with a word
I need count the lines with the same country
and if i do a grep the grep take colombia and colombias.
And other question how i can count lines with the same country?
for example
1 colombia
2 brazil
3 ecuador
4 colombias
5 colombia
colombia 2
colombias 1
ecuador 1
brazil 1
how about
cut -f2 -d' ' salida.csv | sort | uniq -c
since a sed solution was posted (probably the best tool for this task), I'll contribute an awk
awk '$NF=$NF"."' file > salida.csv
Update:
$ cat input
1 colombia
2 brazil
3 ecuador
4 colombias
5 colombia
$ awk '{a[$2]++}END{for (i in a) print i, a[i]}' input
brazil 1
colombias 1
ecuador 1
colombia 2
...and, please stop updating your question with different questions...
Your command line has a few problems. Some that matter, some that are style choices, but here's my take:
Unnecessary cat. sed can take a filename as an argument.
Your sed command doesn't need the g. Since each line only has one end, there's no reason to tell it to look for more.
Don't look for the newline character, just match the end of line with $.
That leaves you with:
sed s/$/./ file > salida.csv
Edit:
If your real question is "How do I grep for colombia, but not match colombias?", you just need to use the -w flag to match whole words:
grep -w colombia file
If you want to count them, just add -c:
grep -c -w colombia file
Read the grep(1) man page for more information.

How to catch duplicate entries in text file in linux [duplicate]

This question already has answers here:
How to delete duplicate lines in a file without sorting it in Unix
(9 answers)
Closed 4 years ago.
Text file:
1 1
2 2
3 3
1 1
I want to catch 1 1 as duplicated
Your question is not quite clear, but you can filter out duplicate lines with uniq:
sort file.txt | uniq
or simply
sort -u file.txt
(thanks RobEarl)
You can also print only repeating lines with
sort file.txt | uniq -d
One way using GNU awk:
awk 'array[$0]++' file.txt
Results:
1 1
You can use it easily:
sort -u file.txt
OR
awk '!x[$0]++' file.txt

Resources