Bash One-Liners Explained, Part I: Working with files - good coders code, great reuse |
| Bash One-Liners Explained, Part I: Working with files Posted: 01 Jun 2012 08:20 AM PDT I love being super fast in the shell so I decided to do a new article series called Bash One-Liners Explained. It's going to be similar to my other article series - Awk One-Liners Explained, Sed One-Liners Explained, and Perl One-Liners Explained. After I'm done with this bash series, I'll release an e-book by the same title, just as I did with awk, sed, and perl series. The e-book will be available as a pdf and in mobile formats (mobi and epub). I'll also be releasing bash1line.txt, similar to perl1line.txt that I made for the perl series. In this series I'll use the best bash practices, various bash idioms and tricks. I want to illustrate how to get various tasks done with just bash built-in commands and bash programming language constructs. Let's start. Part I: Working With Files1. Empty a file (truncate to 0 size) $ > file This one-liner uses the output redirection operator If you wish to replace the contents of a file with some string or create a file with specific content, you can do this: $ echo "some string" > file This puts the string 2. Append a string to a file $ echo "foo bar baz" >> file This one-liner uses a different output redirection operator $ echo -n "foo bar baz" >> file 3. Read the first line from a file and put it in a variable $ read -r line < file This one-liner uses the built-in bash command The $ IFS= read -r line < file This will change the value of Another way to read the first line from a file into a variable is to do this: $ line=$(head -1 file) This one-liner uses the command substitution operator $ line=`head -1 file` However 4. Read a file line-by-line $ while read -r line; do # do something with $line done < file This is the one and only right way to read lines from a file one-by-one. This method puts the Remember that $ while IFS= read -r line; do # do something with $line done < file If you don't like the to put $ cat file | while IFS= read -r line; do # do something with $line done 5. Read a random line from a file and put it in a variable $ read -r random_line < <(shuf file) There is no clean way to read a random line from a file with just bash, so we'll need to use some external programs for help. If you're on a modern Linux machine, then it comes with the This one-liner uses the process substitution When bash sees $ read -r random_line < /dev/fd/n Which reads the first line from the shuffled file. Here is another way to do it with the help of GNU $ read -r random_line < <(sort -R file) Another way to get a random line in a variable is this: $ random_line=$(sort -R file | head -1) Here the file gets randomly sorted by 6. Read the first three columns/fields from a file into variables $ while read -r field1 field2 field3 throwaway; do # do something with $field1, $field2, and $field3 done < file If you specify more than one variable name to the Sometimes it's shorter to just write $ while read -r field1 field2 field3 _; do # do something with $field1, $field2, and $field3 done < file Or if you have a file with exactly three fields, then you don't need it at all: $ while read -r field1 field2 field3; do # do something with $field1, $field2, and $field3 done < file Here is an example. Let's say you wish to find out number of lines, number of words, and number of bytes in a file. If you run $ cat file-with-5-lines x 1 x 2 x 3 x 4 x 5 $ wc file-with-5-lines 5 10 20 file-with-5-lines So this file has 5 lines, 10 words, and 20 chars. We can use the $ read lines words chars _ < <(wc file-with-5-lines) $ echo $lines 5 $ echo $words 10 $ echo $chars 20 Similarly you can use here-strings to split strings into variables. Let's say you have a string $ packets=$(echo $info | awk '{ print $1 }') $ time=$(echo $info | awk '{ print $4 }') However given the power of $ read packets _ _ time _ <<< "$info" Here the 7. Find the size of a file, and put it in a variable $ size=$(wc -c file) This one-liner uses the command substitution operator 8. Extract the filename from the path Let's say you have a $ filename=${path##*/} This one-liner uses the In this case the pattern is 9. Extract the directory name from the path This is similar to the previous one-liner. Let's say you have a $ dirname=${path%/*} This time it's the In this case the pattern is 10. Make a copy of a file quickly Let's say you wish to copy the file at $ cp /path/to/file /path/to/file_copy However you can do it much quicker by using the brace expansion $ cp /path/to/file{,_copy} Brace expansion is a mechanism by which arbitrary strings can be generated. In this particular case Similarly you can move a file quickly: $ mv /path/to/file{,_old} This expands to Enjoy!Enjoy the article and let me know in the comments what you think about it! If you think that I forgot some interesting bash one-liners related to file operations, let me know in to comments also! |
| You are subscribed to email updates from good coders code, great reuse To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
| Google Inc., 20 West Kinzie, Chicago IL USA 60610 | |
No comments:
Post a Comment
Keep a civil tongue.