![]() |
|
Understanding Angle Brackets in Bash - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Computers (https://sickgaming.net/forum-86.html) +--- Forum: Linux, BSD & Unix (https://sickgaming.net/forum-88.html) +--- Thread: Understanding Angle Brackets in Bash (/thread-88387.html) |
Understanding Angle Brackets in Bash - xSicKxBot - 01-25-2019 Understanding Angle Brackets in Bash Bash provides many important built-in commands, like Code: lsCode: cdCode: mvCode: grepCode: awk,Code: sedCode: < >Pushing Around If you are familiar with other programming and scripting languages, you may have used Code: <Code: >In shell scripting, you can also use brackets to push data from place to place, for example, to a file: Code: ls > dir_content.txtIn this example, instead of showing the contents of the directory on the command line, Code: >You can avoid overwriting existing content by tacking the new stuff onto the end of the old stuff. For that you use Code: >>Code: >Code: ls $HOME > dir_content.txt; wc -l dir_content.txt >> dir_content.txtThis line stores the list of contents of your home directory into dir_content.txt. You then count the number of lines in dir_content.txt (which gives you the number of items in the directory) with Code: wc -lAfter running the command line on my machine, this is what my dir_content.txt file looks like: Code: Applications bin cloud Desktop Documents Downloads Games ISOs lib logs Music OpenSCAD Pictures Public Templates test_dir Videos 17 dir_content.txtThe mnemonic here is to look at Code: >Code: >>Code: John Goodman 5
John Turturro 3
George Clooney 2
Frances McDormand 6
Steve Buscemi 5
Jon Polito 4
Tony Shalhoub 3
James Gandolfini 1Something like Code: sort < CBActors # Do this
Frances McDormand 6 # And you get this
George Clooney 2 James Gandolfini 1 John Goodman 5 John Turturro 3 Jon Polito 4 Steve Buscemi 5 Tony Shalhoub 3Will Code: sortCode: <Code: sortCode: sort CBActorsHowever, if you need to see who is the Coens’ favorite actor, you can check with : Code: while read name surname films; do echo $films $name $surname > filmsfirst.txt; done < CBActorsOr, to make that a bit more readable: Code: while read name surname films;\ do echo $films $name $surname >> filmsfirst;\ done < CBActorsLet’s break this down, shall we?
At the end of all that, you have a file called filmsfirst that looks like this: Code: 5 John Goodman 3 John Turturro 2 George Clooney 6 Frances McDormand 5 Steve Buscemi 4 Jon Polito 3 Tony Shalhoub 1 James Gandolfiniwhich you can now use with Code: sortCode: sort -r filmsfirstto see who is the Coens’ favorite actor. Yes, it is Frances McDormand. (The Code: -rWe’ll look at more angles on this topic next time! |