isaric
isaric

Reputation: 305

homework: use number of words in one file(through wc) and send it to head as the number of lines

Today I had homework in an introductory programming class.

One of the assignments was to take the number of words from one file (using wc) and send it to head as the number of lines.

This is my code (StackOverflow already helped here):

head -n\`wc -w boot.ok\` /var/log/udev > udev.moj

After checking:

`wc -w boot.ok`

it does not equal

`wc -l udev.moj`

Can someone please explain to me what I am doing wrong?

Upvotes: 1

Views: 88

Answers (1)

Mureinik
Mureinik

Reputation: 312146

wc -w doesn't only produce the number of words in the file, it also prints the file name. E.g.:

[mureinik@computer tmp]$ echo "hello world" > boot.ok
[mureinik@computer tmp]$ wc -w boot.ok 
2 boot.ok

If you feed this into head -n as is, head will output an error to stderr, and print the entire file. You could use cut to extract the numeric part of wc:

head -n `wc -w boot.ok | cut -f1 -d" "` /var/log/udev > udev.moj

Upvotes: 1

Related Questions