![]() ![]() ![]() Otherwise, our solution may lead to an unexpected result - particularly, if it’s a part of a script. However, we have to think if our awk implementation has covered all the corner cases. When grep is combined with regex ( reg ular ex pressions), advanced searching and output filtering become simple. Usually, solutions with these two commands are stable and easier to understand.Īwk is a powerful utility, and it can certainly solve the problem. Introduction The grep command (short for G lobal R egular E xpressions P rint) is a powerful text processing tool for searching through files and directories. When we need to handle path strings, we can first consider if basename and/or dirname can solve the problem. It’s worth mentioning that the basename command has a brother, dirname, which does the opposite - strip the last component from the given path string: $ dirname "/tmp/dir/target" Next, let’s do a test with different inputs: $ basename "/tmp/dir/target"Īs the output above shows, the basename command is a straightforward solution to the problem. Using the basename Commandįurther, it’s pretty stable and covers all the corner cases. Here, we show an awk one-liner as an example: $ awk -F'/' '$0=FS’ ), it’s pretty complex now.Īctually, the Coreutils package has provided a convenient command to solve our problem. Similarly, a Bash function can do the job as well. If we like, we can still extend the awk one-liner to cover all the cases. Now, let’s summarize all the possible patterns of a Linux path string (input) and our expected result (output): Input Therefore, it’s also a valid path string if a file or a directory is named by ” “. ![]() Additionally, most Linux filesystems allow spaces to be filenames or directory names. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |