Skip to main content
deleted 1 character in body
Source Link
terdon
  • 252.9k
  • 69
  • 481
  • 720

I wouldn't really use sed or grep here. You can, but in my opinion, tail is simpler. Use tail to get the last three bytes—we need three to account for the trailing \n character—of the file (note that this won't work if instead of nt you are looking for a more complicated Unicode string where characters can be more than one byte each), and then check if that matches nt:

if [ "$(tail -c3 file)" = "nt" ]];]; then echo yes else echo no fi 

You could do this with GNU grep, for instance by treating the entire file (assuming it doesn't contain NUL characters) as a single "line" and checking the last characters:

if grep -qPz 'nt\n$' file; then echo yes else echo no fi 

Or, with sed, something like this (or @steeldriver's better and simpler GNU-specific approach) :

if [ "$(sed -n '${/nt$/p;}' file | wc -c)" -gt 0 ]; then echo yes; else echo no fi 

Or even perl:

if perl -ne '$l=$_; }{ exit($l=~/nt$/ ? 0 : 1)' file; then echo yes else echo no fi 

I wouldn't really use sed or grep here. You can, but in my opinion, tail is simpler. Use tail to get the last three bytes—we need three to account for the trailing \n character—of the file (note that this won't work if instead of nt you are looking for a more complicated Unicode string where characters can be more than one byte each), and then check if that matches nt:

if [ "$(tail -c3 file)" = "nt" ]]; then echo yes else echo no fi 

You could do this with GNU grep, for instance by treating the entire file (assuming it doesn't contain NUL characters) as a single "line" and checking the last characters:

if grep -qPz 'nt\n$' file; then echo yes else echo no fi 

Or, with sed, something like this (or @steeldriver's better and simpler GNU-specific approach) :

if [ "$(sed -n '${/nt$/p;}' file | wc -c)" -gt 0 ]; then echo yes; else echo no fi 

Or even perl:

if perl -ne '$l=$_; }{ exit($l=~/nt$/ ? 0 : 1)' file; then echo yes else echo no fi 

I wouldn't really use sed or grep here. You can, but in my opinion, tail is simpler. Use tail to get the last three bytes—we need three to account for the trailing \n character—of the file (note that this won't work if instead of nt you are looking for a more complicated Unicode string where characters can be more than one byte each), and then check if that matches nt:

if [ "$(tail -c3 file)" = "nt" ]; then echo yes else echo no fi 

You could do this with GNU grep, for instance by treating the entire file (assuming it doesn't contain NUL characters) as a single "line" and checking the last characters:

if grep -qPz 'nt\n$' file; then echo yes else echo no fi 

Or, with sed, something like this (or @steeldriver's better and simpler GNU-specific approach) :

if [ "$(sed -n '${/nt$/p;}' file | wc -c)" -gt 0 ]; then echo yes; else echo no fi 

Or even perl:

if perl -ne '$l=$_; }{ exit($l=~/nt$/ ? 0 : 1)' file; then echo yes else echo no fi 
missing quotes, removed sed GNUism, wc -l counts newlines, wc -c might give more consistent results.
Source Link
Stéphane Chazelas
  • 587.9k
  • 96
  • 1.1k
  • 1.7k

I wouldn't really use sed or grep here. You can, but in my opinion, tail is simpler. Use tail to get the last three bytes—we need three to account for the trailing \n character—of the file (note that this won't work if instead of nt you are looking for a more complicated Unicode string where characters can be more than one byte each), and then check if that matches nt:

if [ $"$(tail -c3 file)" = "nt" ]]; then echo yes else echo no fi 

You could do this with GNU grep, for instance by treating the entire file (assuming it doesn't contain NUL characters) as a single "line" and checking the last characters:

if grep -qPz 'nt\n$' file; then echo yes else echo no fi 

Or, with sed, something like this (or @steeldriver's better and simpler GNU-specific approach) :

if [ $"$(sed -n '${/nt$/pp;}' file | wc -lc)" -gt 0 ]; then echo yes; else echo no fi 

Or even perl:

if perl -ne '$l=$_; }{ exit($l=~/nt$/ ? 0 : 1)' file; then echo yes else echo no fi 

I wouldn't really use sed or grep here. You can, but in my opinion, tail is simpler. Use tail to get the last three bytes—we need three to account for the trailing \n character—of the file (note that this won't work if instead of nt you are looking for a more complicated Unicode string where characters can be more than one byte each), and then check if that matches nt:

if [ $(tail -c3 file) = "nt" ]]; then echo yes else echo no fi 

You could do this with GNU grep, for instance by treating the entire file as a single "line" and checking the last characters:

if grep -qPz 'nt\n$' file; then echo yes else echo no fi 

Or, with sed, something like this (or @steeldriver's better and simpler GNU-specific approach) :

if [ $(sed -n '${/nt$/p}' file | wc -l) -gt 0 ]; then echo yes; else echo no fi 

Or even perl:

if perl -ne '$l=$_; }{ exit($l=~/nt$/ ? 0 : 1)' file; then echo yes else echo no fi 

I wouldn't really use sed or grep here. You can, but in my opinion, tail is simpler. Use tail to get the last three bytes—we need three to account for the trailing \n character—of the file (note that this won't work if instead of nt you are looking for a more complicated Unicode string where characters can be more than one byte each), and then check if that matches nt:

if [ "$(tail -c3 file)" = "nt" ]]; then echo yes else echo no fi 

You could do this with GNU grep, for instance by treating the entire file (assuming it doesn't contain NUL characters) as a single "line" and checking the last characters:

if grep -qPz 'nt\n$' file; then echo yes else echo no fi 

Or, with sed, something like this (or @steeldriver's better and simpler GNU-specific approach) :

if [ "$(sed -n '${/nt$/p;}' file | wc -c)" -gt 0 ]; then echo yes; else echo no fi 

Or even perl:

if perl -ne '$l=$_; }{ exit($l=~/nt$/ ? 0 : 1)' file; then echo yes else echo no fi 
consistent indentation
Source Link
choroba
  • 49.7k
  • 7
  • 92
  • 119

I wouldn't really use sed or grep here. You can, but in my opinion, tail is simpler. Use tail to get the last three bytes—we need three to account for the trailing \n character—of the file (note that this won't work if instead of nt you are looking for a more complicated Unicode string where characters can be more than one byte each), and then check if that matches nt:

if [ $(tail -c3 file) = "nt" ]]; then echo yes else echo no fi 

You could do this with GNU grep, for instance by treating the entire file as a single "line" and checking the last characters:

if grep -qPz 'nt\n$' file; then  echo yes else echo no fi 

Or, with sed, something like this (or @steeldriver's better and simpler GNU-specific approach) :

if [ $(sed -n '${/nt$/p}' file | wc -l) -gt 0 ]; then echo yes; else   echo no fi 

Or even perl:

if perl -ne '$l=$_; }{ exit($l=~/nt$/ ? 0 : 1)' file; then echo yes else echo no fi 

I wouldn't really use sed or grep here. You can, but in my opinion, tail is simpler. Use tail to get the last three bytes—we need three to account for the trailing \n character—of the file (note that this won't work if instead of nt you are looking for a more complicated Unicode string where characters can be more than one byte each), and then check if that matches nt:

if [ $(tail -c3 file) = "nt" ]]; then echo yes else echo no fi 

You could do this with GNU grep, for instance by treating the entire file as a single "line" and checking the last characters:

if grep -qPz 'nt\n$' file; then echo yes else echo no fi 

Or, with sed, something like this (or @steeldriver's better and simpler GNU-specific approach) :

if [ $(sed -n '${/nt$/p}' file | wc -l) -gt 0 ]; then echo yes; else echo no fi 

Or even perl:

if perl -ne '$l=$_; }{ exit($l=~/nt$/ ? 0 : 1)' file; then echo yes else echo no fi 

I wouldn't really use sed or grep here. You can, but in my opinion, tail is simpler. Use tail to get the last three bytes—we need three to account for the trailing \n character—of the file (note that this won't work if instead of nt you are looking for a more complicated Unicode string where characters can be more than one byte each), and then check if that matches nt:

if [ $(tail -c3 file) = "nt" ]]; then echo yes else echo no fi 

You could do this with GNU grep, for instance by treating the entire file as a single "line" and checking the last characters:

if grep -qPz 'nt\n$' file; then  echo yes else echo no fi 

Or, with sed, something like this (or @steeldriver's better and simpler GNU-specific approach) :

if [ $(sed -n '${/nt$/p}' file | wc -l) -gt 0 ]; then echo yes; else   echo no fi 

Or even perl:

if perl -ne '$l=$_; }{ exit($l=~/nt$/ ? 0 : 1)' file; then echo yes else echo no fi 
Source Link
terdon
  • 252.9k
  • 69
  • 481
  • 720
Loading