Bash: my output is on one line [duplicate] - linux

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

Related

Execute agrep within script | grep terminal output [duplicate]

This question already has answers here:
Assign output to variable in Bash [duplicate]
(2 answers)
Closed 2 years ago.
Can I execute a grep in my script below?
echo 5
echo 4
result='çat output.txt | grep flag'
echo $result
The scipt gets used like
./script | ./program > output.txt
The script is used as input for the program, and the output of the program gets put into output, which I want to be able to grep for instantly. At the moment, it seems to finish without doing the grep command
I have the impression you are using single quotes while it should be backtics. Luckily there is something easier to use
result=$(cat output.txt | grep "flag")
The $(some_command) is used for getting the results of some_command.

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 }'

What is the difference between cat source.txt | grep x and grep x source.txt? [duplicate]

This question already has answers here:
difference between grep Vs cat and grep
(5 answers)
Closed 8 years ago.
I saw an example where some one did this:
cat source.txt | grep a
But I always do it like:
grep a source.txt
What's the difference between the two?
The first one is a classical “useless use of cat” (UUOC).

Linux sorting awk command [duplicate]

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

How can I print intermediate results from a pipeline to the screen? [duplicate]

This question already has answers here:
How can I gzip standard in to a file and also print standard in to standard out?
(4 answers)
Closed 7 years ago.
I'm trying to count the lines from a command and I'd also like to see the lines as they go by. My initial thought was to use the tee command:
complicated_command | tee - | wc -l
But that simply doubles the line count using GNU tee or copies output to a file named - on Solaris.
complicated_command | tee /dev/tty | wc -l
But keep in mind that if you put it in a script and redirect the output, it won't do what you expect.
The solution is to tee to the console directly as opposed to STDOUT:
tty=`tty`
complicated_command | tee $tty | wc -l

Resources