3 replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
source | link

Yet Another Unconventional Use of Grep -- a Schwartzian Transform that goes through several gyrations to number the lines, then uses grep to look for the line number, then strip the line number back off:

function grep1() (
  nlines=$(wc -l < "$1")
  nlw=$(printf "%d" "$nlines" | wc -c)
  nl -d '\n' -ba -n ln -w "$nlw" -s ' ' "$1" | grep '^1 ' | sed 's/^1 *//'
)

function greplast() (
  nlines=$(wc -l < "$1")
  nlines=$((nlines + 0))
  nlw=$(printf "%d" "$nlines" | wc -c)
  nl -d '\n' -ba -n ln -w "$nlw" -s ' ' "$1" | grep "^$nlines " | sed "s/^$nlines *//"
)

I'm putting this Answer here as an example of the idea that just because you can do something in (grep or bash or ... etc), doesn't mean that you should -- there's probably a better tool for the job. sedsed (sed 1q or sed -n 1p) and headhead (head -n 1) are the more appropriate tools for printing the first line from a file. For printing the last line of a file, you have tail -n 1 or sed -n '$p'. Not only are these tools a single command (instead of 3+ in the above functions), they are also much clearer for future readers -- perhaps yourself! -- of the scripts they're in. While I am not one of the (currently 3) down-voters of your question, it's likely that your insistence on an arbitrary tool for the job (without any supporting reasons) is the reason for the downvotes. It's extremely unlikely that a system that has grep does not also have head, tail, and sed.

Yet Another Unconventional Use of Grep -- a Schwartzian Transform that goes through several gyrations to number the lines, then uses grep to look for the line number, then strip the line number back off:

function grep1() (
  nlines=$(wc -l < "$1")
  nlw=$(printf "%d" "$nlines" | wc -c)
  nl -d '\n' -ba -n ln -w "$nlw" -s ' ' "$1" | grep '^1 ' | sed 's/^1 *//'
)

function greplast() (
  nlines=$(wc -l < "$1")
  nlines=$((nlines + 0))
  nlw=$(printf "%d" "$nlines" | wc -c)
  nl -d '\n' -ba -n ln -w "$nlw" -s ' ' "$1" | grep "^$nlines " | sed "s/^$nlines *//"
)

I'm putting this Answer here as an example of the idea that just because you can do something in (grep or bash or ... etc), doesn't mean that you should -- there's probably a better tool for the job. sed (sed 1q or sed -n 1p) and head (head -n 1) are the more appropriate tools for printing the first line from a file. For printing the last line of a file, you have tail -n 1 or sed -n '$p'. Not only are these tools a single command (instead of 3+ in the above functions), they are also much clearer for future readers -- perhaps yourself! -- of the scripts they're in. While I am not one of the (currently 3) down-voters of your question, it's likely that your insistence on an arbitrary tool for the job (without any supporting reasons) is the reason for the downvotes. It's extremely unlikely that a system that has grep does not also have head, tail, and sed.

Yet Another Unconventional Use of Grep -- a Schwartzian Transform that goes through several gyrations to number the lines, then uses grep to look for the line number, then strip the line number back off:

function grep1() (
  nlines=$(wc -l < "$1")
  nlw=$(printf "%d" "$nlines" | wc -c)
  nl -d '\n' -ba -n ln -w "$nlw" -s ' ' "$1" | grep '^1 ' | sed 's/^1 *//'
)

function greplast() (
  nlines=$(wc -l < "$1")
  nlines=$((nlines + 0))
  nlw=$(printf "%d" "$nlines" | wc -c)
  nl -d '\n' -ba -n ln -w "$nlw" -s ' ' "$1" | grep "^$nlines " | sed "s/^$nlines *//"
)

I'm putting this Answer here as an example of the idea that just because you can do something in (grep or bash or ... etc), doesn't mean that you should -- there's probably a better tool for the job. sed (sed 1q or sed -n 1p) and head (head -n 1) are the more appropriate tools for printing the first line from a file. For printing the last line of a file, you have tail -n 1 or sed -n '$p'. Not only are these tools a single command (instead of 3+ in the above functions), they are also much clearer for future readers -- perhaps yourself! -- of the scripts they're in. While I am not one of the (currently 3) down-voters of your question, it's likely that your insistence on an arbitrary tool for the job (without any supporting reasons) is the reason for the downvotes. It's extremely unlikely that a system that has grep does not also have head, tail, and sed.

2 have to quote the $p to avoid it being a variable expansion
source | link

Yet Another Unconventional Use of Grep -- a Schwartzian Transform that goes through several gyrations to number the lines, then uses grep to look for the line number, then strip the line number back off:

function grep1() (
  nlines=$(wc -l < "$1")
  nlw=$(printf "%d" "$nlines" | wc -c)
  nl -d '\n' -ba -n ln -w "$nlw" -s ' ' "$1" | grep '^1 ' | sed 's/^1 *//'
)

function greplast() (
  nlines=$(wc -l < "$1")
  nlines=$((nlines + 0))
  nlw=$(printf "%d" "$nlines" | wc -c)
  nl -d '\n' -ba -n ln -w "$nlw" -s ' ' "$1" | grep "^$nlines " | sed "s/^$nlines *//"
)

I'm putting this Answer here as an example of the idea that just because you can do something in (grep or bash or ... etc), doesn't mean that you should -- there's probably a better tool for the job. sed (sed 1q or sed -n 1p) and head (head -n 1) are the more appropriate tools for printing the first line from a file. For printing the last line of a file, you have tail -n 1 or sed -n $p'$p'. Not only are these tools a single command (instead of 3+ in the above functions), they are also much clearer for future readers -- perhaps yourself! -- of the scripts they're in. While I am not one of the (currently 3) down-voters of your question, it's likely that your insistence on an arbitrary tool for the job (without any supporting reasons) is the reason for the downvotes. It's extremely unlikely that a system that has grep does not also have head, tail, and sed.

Yet Another Unconventional Use of Grep -- a Schwartzian Transform that goes through several gyrations to number the lines, then uses grep to look for the line number, then strip the line number back off:

function grep1() (
  nlines=$(wc -l < "$1")
  nlw=$(printf "%d" "$nlines" | wc -c)
  nl -d '\n' -ba -n ln -w "$nlw" -s ' ' "$1" | grep '^1 ' | sed 's/^1 *//'
)

function greplast() (
  nlines=$(wc -l < "$1")
  nlines=$((nlines + 0))
  nlw=$(printf "%d" "$nlines" | wc -c)
  nl -d '\n' -ba -n ln -w "$nlw" -s ' ' "$1" | grep "^$nlines " | sed "s/^$nlines *//"
)

I'm putting this Answer here as an example of the idea that just because you can do something in (grep or bash or ... etc), doesn't mean that you should -- there's probably a better tool for the job. sed (sed 1q or sed -n 1p) and head (head -n 1) are the more appropriate tools for printing the first line from a file. For printing the last line of a file, you have tail -n 1 or sed -n $p. Not only are these tools a single command (instead of 3+ in the above functions), they are also much clearer for future readers -- perhaps yourself! -- of the scripts they're in. While I am not one of the (currently 3) down-voters of your question, it's likely that your insistence on an arbitrary tool for the job (without any supporting reasons) is the reason for the downvotes. It's extremely unlikely that a system that has grep does not also have head, tail, and sed.

Yet Another Unconventional Use of Grep -- a Schwartzian Transform that goes through several gyrations to number the lines, then uses grep to look for the line number, then strip the line number back off:

function grep1() (
  nlines=$(wc -l < "$1")
  nlw=$(printf "%d" "$nlines" | wc -c)
  nl -d '\n' -ba -n ln -w "$nlw" -s ' ' "$1" | grep '^1 ' | sed 's/^1 *//'
)

function greplast() (
  nlines=$(wc -l < "$1")
  nlines=$((nlines + 0))
  nlw=$(printf "%d" "$nlines" | wc -c)
  nl -d '\n' -ba -n ln -w "$nlw" -s ' ' "$1" | grep "^$nlines " | sed "s/^$nlines *//"
)

I'm putting this Answer here as an example of the idea that just because you can do something in (grep or bash or ... etc), doesn't mean that you should -- there's probably a better tool for the job. sed (sed 1q or sed -n 1p) and head (head -n 1) are the more appropriate tools for printing the first line from a file. For printing the last line of a file, you have tail -n 1 or sed -n '$p'. Not only are these tools a single command (instead of 3+ in the above functions), they are also much clearer for future readers -- perhaps yourself! -- of the scripts they're in. While I am not one of the (currently 3) down-voters of your question, it's likely that your insistence on an arbitrary tool for the job (without any supporting reasons) is the reason for the downvotes. It's extremely unlikely that a system that has grep does not also have head, tail, and sed.

1
source | link

Yet Another Unconventional Use of Grep -- a Schwartzian Transform that goes through several gyrations to number the lines, then uses grep to look for the line number, then strip the line number back off:

function grep1() (
  nlines=$(wc -l < "$1")
  nlw=$(printf "%d" "$nlines" | wc -c)
  nl -d '\n' -ba -n ln -w "$nlw" -s ' ' "$1" | grep '^1 ' | sed 's/^1 *//'
)

function greplast() (
  nlines=$(wc -l < "$1")
  nlines=$((nlines + 0))
  nlw=$(printf "%d" "$nlines" | wc -c)
  nl -d '\n' -ba -n ln -w "$nlw" -s ' ' "$1" | grep "^$nlines " | sed "s/^$nlines *//"
)

I'm putting this Answer here as an example of the idea that just because you can do something in (grep or bash or ... etc), doesn't mean that you should -- there's probably a better tool for the job. sed (sed 1q or sed -n 1p) and head (head -n 1) are the more appropriate tools for printing the first line from a file. For printing the last line of a file, you have tail -n 1 or sed -n $p. Not only are these tools a single command (instead of 3+ in the above functions), they are also much clearer for future readers -- perhaps yourself! -- of the scripts they're in. While I am not one of the (currently 3) down-voters of your question, it's likely that your insistence on an arbitrary tool for the job (without any supporting reasons) is the reason for the downvotes. It's extremely unlikely that a system that has grep does not also have head, tail, and sed.