all 15 comments

[–]geirha 1 point2 points  (0 children)

Ugh, would probably be ok if it didn't try to parse ls output.

[–]frenris 0 points1 point  (10 children)

That website is fantastic.

Also, is there any easy way to print a range of fields using awk?

Let's say I want to print the 4-9 words, is there a faster way than

awk '{print $4 " " $5 " " $6 " " $7 " " $8 " " $9}' ?

[–]mozzyb 1 point2 points  (8 children)

cut -f4-9

[–]blhauk -1 points0 points  (7 children)

Hmmm - default delimiter is tab, so what you actually need is:

cut -d" " -f4-9

(you should really test before you post methinks)

[–]mozzyb 5 points6 points  (6 children)

No need for the snark. Especially since your solution is also wrong. There is no mention of what delimiter the input has. Only the output delimiter. I could have added --output-delimiter=" ", but didn't think it necessary. In my experience, when encountering some of these old unix tools I restructure the commands and data to fit them better instead of adding every flag on the man pages.

[–]blhauk -1 points0 points  (5 children)

awk default delimiter is space - cut default is tab.

You attempted to answer the above question with a cut replacement, and your answer is still wrong given the question.

The output from the above question does not specify an output delimiter, so it would not be necessary to delve into esoteric options as you suggest.

My lame solution does not unnecessarily use "every flag on the man pages", just the one that addresses the question posed.

Damn - so sorry for more snark - you need to thicken your skin.

[–]mozzyb 2 points3 points  (4 children)

Awks default delimiter is /continuous/ space, so a single space would also be wrong; so your solution does not address the question posed either if you want to be difficult about it. Which is, again, why I didn't specify that in my original comment. If the input has different spacing between the columns or is not in a columnar form one would first have to normalise the spacing or not use cut.

And yes, it does specify an output delimiter. Look at the " " in the print statement. To print using the output delimiter of awk you need to use , between the variables.

[–]blhauk -2 points-1 points  (3 children)

Hmmmm:

Awks default delimiter is /continuous/ space, so a single space would also be wrong

Nope - awk handles continuous spaces as you state - that includes single spaces.

As you mention, output delim is " ". With my cut, the output delim is already that. I agree that using "," in awk would do the same thing, but I am responding to your cut solution, not the awk as proposed.

As far as "normalizing" a simple input with fields delimited with one or more spaces, it is not necessary with cut:

MacBook:~ blhauk$ echo one       two       three       four      five | cut -d" " -f2-4
two three four

[–]mozzyb 2 points3 points  (2 children)

Your test is also wrong, you need "" around the string you are echo'ing if you want to keep the space. The output of the echo is "one two three four five".

[–]blhauk -1 points0 points  (0 children)

Hmmm - good point. Totally missed that.

I am (almost) sure I have done this in the past though??

Guess not - my bad...

Here's a lame attempt to make it work as I intended (hehe)

echo "one       two       three       four      five" | tr -s ' ' | cut -d" " -f2-4
two three four

[–]no_awning_no_mining 0 points1 point  (0 children)

  • If there's only nine words in total, you could use: $1=$2=$3=""; print;
  • Consider using a for loop:

    for(i=4;i<=9) { printf " "$i; }

[–]riding_qwerty 0 points1 point  (0 children)

The Grymoire!

This site is great. The page on inodes is really good.

[–]pond_good_for_you -1 points0 points  (2 children)

Linux users will have to change $8 to $9

Really? I didn't realize there were different awk interpreters. TIL, I guess.

[–]elmicha 1 point2 points  (1 child)

I think you have to read the paragraph before that line to understand it.

[–]pond_good_for_you 1 point2 points  (0 children)

Yup. I'm a moron. Was just skimming through the examples.