5 ms·
> Make directory trees in a single swipe. Really? How often will I need to make a directory structure that is several levels deep, but only one directory at e
by brent 19y ago
> Make directory trees in a single swipe.
Really? How often will I need to make a directory structure that is several levels deep, but only one directory at each level?
>Match certain fields in output, not just lines
For someone who was trying to save a few keystrokes, instead of trying waste time getting the right field for awk, just use grep. Its easy, you can't mess it up, and unless the formatting really matters its the best option.
- fendale 19y agomkdir -p is really useful. If I have a script that needs to be a file in (security concerns aside) /basedir/some/path/thats/not/there/ I can just do mkdir -p /basedir/some/path/thats/not/there and not worry about which of the lower level directories exist or not - it will just create them or not complain if they don't exist. Before I knew of the -p switch, I used to split up the whole path and check for/create each directory level in turn - while its not difficult, its not much fun either!
- ralph 19y ago> How often will I need to make a directory structure that is several levels deep, but only one directory at each level? When you want to put a file into a directory structure where theres several levels of directories to avoid one directory containing many files, e.g. com/ycombinator/news/foo. > For someone who was trying to save a few keystrokes, instead of trying waste time getting the right field for awk, just use grep. Its easy, you can't mess it up, and unless the formatting really matters its the best option. The FA shows grep fails because 'Dec' matches part of the filename instead of just on the date. awk's a worthy solution in that case and formatting of the output wasn't relevant to the point made.
- brent 19y agoBy formatting I was referring to the fact that he had to match the 6th field. If grep would return 100k and only 10 in the desired field awk is obviously handy (the exception). In the general case where you are just trying to hunt things down who stops and counts which field their target string is in. I could have run grep and analyzed the results by the time you count to the right field. If you miscounted however... how do you know?
- ralph 19y agoThe example was using awk to pick "Dec" out in the month column whilst pointing out a grep for "Dec" would also match the filename "December" which didn't have "Dec" in the month column. awk's clearly better than grep in this case. It's not hard to count the column and you can always do a ... | awk '{print $6}' | sort | uniq -c to see if you've got it right. In fact, a common way to develope the pipeline for a one-off task is to keep appending a pipe and another command to the previous pipeline command. That way, you can see a sample of what's being produced instead of imagining it. You couldn't have analysed the results of grep, discarding the non-month matches of "Dec" in the time taken to count up to field 6 if the number of lines was at all significant. Don't be silly. If I did miscount and no lines matched I may be suspicious and look more closely. If some lines matched but on a different column than intended I may notice. I can re-run the command after correction or try the {print $6} idea. How would you notice if your manual* analysis failed to spot the extra result from grep here and there. Would you do your manual analysis twice? Ask someone else to do it too? Or just think "good enough" and use the data for the next stage?
- mark-t 19y ago> Really? How often will I need to make a directory structure that is several levels deep, but only one directory at each level? Didn't take me long to find one in my scripts directory. Just imagine you're doing something similar by hand: #!/bin/bash # This script strips debugging symbols from programs and stores them separately. # They will be loaded by gdb when needed but will not take up memory when # running the program normally. # determine where to save debugging symbols DEBUG_PATH="/usr/lib/debug" if [[ ${EUID} != 0 ]]; then DEBUG_PATH="${HOME}/lib/debug"; fi # go through list of command line arguments and split each one for x in "$@" do echo "Splitting ${x}" y="${DEBUG_PATH}${x}.debug" mkdir -p "$(dirname $y)" objcopy --only-keep-debug "${x}" "${y}" || continue objcopy --add-gnu-debuglink="${y}" "${x}" || continue chmod a-x,o-w "${y}" strip "${x}" done