bash - Getting head to display all but the last line of a file: command substitution and standard I/O redirection -


i have been trying head utility display last line of standard input. actual code needed along lines of cat myfile.txt | head -n $(($(wc -l)-1)). didn't work. i'm doing on darwin/os x doesn't have nice semantics of head -n -1 have gotten me similar output.

none of these variations work either.

cat myfile.txt | head -n $(wc -l | sed -e -e 's/\s//g') echo "hello" | head -n $(wc -l | sed -e -e 's/\s//g') 

i tested out more variations , in particular found work:

cat <<eof | echo $(($(wc -l)-1)) >hola >raul >como esta >bueno? >eof 3 

here's simpler works.

echo "hello world" | echo $(($(wc -w)+10)) 

this 1 understandably gives me illegal line count error. @ least tells me head program not consuming standard input before passing stuff on subshell/command substitution, remote possibility, 1 wanted rule out anyway.

echo "hello" | head -n $(cat && echo 1) 

what explains behavior of head , wc , interaction through subshells here? help.

head wrong tool. if want see last line, use:

sed \$d 

the reason

# sample of incorrect code: echo "hello" | head -n $(wc -l | sed -e -e 's/\s//g') 

fails wc consumes of input , there nothing left head see. wc inherits stdin subshell in running, reading output of echo. once consumes input, returns , head tries read data...but gone. if want read input twice, data have saved somewhere.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -