LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   whats the standard out put for wc -l data.txt on Ubuntu (https://www.linuxquestions.org/questions/linux-newbie-8/whats-the-standard-out-put-for-wc-l-data-txt-on-ubuntu-4175450460/)

ABOYEWAK 02-16-2013 02:50 PM

whats the standard out put for wc -l data.txt on Ubuntu
 
I just started using Ubuntu OS, the command "wc -l data.txt" printed out '"number of lines" data.txt'. I would expect to get just "number of line"...

mjolnir 02-16-2013 03:22 PM

Code:

carl@Wizard:~$ wc -l earth.txt
9 earth.txt

Nine lines. Seems to work as expected.

From wc --help:

Code:

The options below may be used to select which counts are printed, always in
the following order: newline, word, character, byte, maximum line length.
  -c, --bytes            print the byte counts
  -m, --chars            print the character counts
  -l, --lines            print the newline counts
      --files0-from=F    read input from the files specified by
                          NUL-terminated names in file F;
                          If F is - then read names from standard input
  -L, --max-line-length  print the length of the longest line
  -w, --words            print the word counts
      --help    display this help and exit
      --version  output version information and exit


rknichols 02-16-2013 03:49 PM

If you want to see just the number, pass the data to wc on stdin rather than having it open the file itself:
Code:

$ wc -l <data.txt
25
$


mjolnir 02-16-2013 04:30 PM

@rknichols Thanks, worked like a charm.
Code:

carl@Wizard:~$ wc -l <earth.txt
9


joecam1673 02-16-2013 08:43 PM

If you're new to redirects (<, <<, <<<, > etc) it should be noted that some respect should be given to the fact that they can be destructive if a typo is made. For instance

joe@debian:~$ wc -l <important_data.txt # correct usage provides desired line count
228
joe@debian:~$ wc -l >important_data.txt # command hangs
^C # until terminated
joe@debian:~$ wc -l <important_data.txt # file has been blanked
0
joe@debian:~$ cat important_data.txt
joe@debian:~$ # :(

cat'ing the file and piping it through wc is a safe, non-destructive alternative;

joe@debian:~$ cat important_data.txt | wc -l
228

mjolnir 02-18-2013 06:53 AM

Thanks joecam1673, good to remember.

joecam1673 02-19-2013 05:12 AM

My pleasure.

Of course the practice of cat'ing a file into a pipe will cost you a process so ultimately mastering redirects and keeping proper rw permissions for important files is best practice.

I actually use
Code:

if [[ -n $file ]]
then
 >$file
fi

relativelty often to scrub temp files


All times are GMT -5. The time now is 10:10 PM.