Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Linux command for getting the no of lines in a file


    I have a huge 40 MB file in Linux.
    What is command to get the no of lines in the file.
    And what are the other commands which I can use for parsing files


  2. #2
    Just Joined!
    Join Date
    Jun 2006
    Monterey, California USA

    A few file parsing utilities.


    $ wc /var/log/messages

    Other popular file parsing utilities (remember, you can use --help or man to get more information about any of the below commands)... anything within <> means that you need to provide that information. I.e. <file> could be /etc/hosts.

    cat <file>
    - print contents of file to screen starting at the top of the file.

    tac <file>
    - print contents of files to screen starting at the bottom of the file. (thanks to mkachline for introducing me to this one)

    head <file>
    - print the top few lines of a file.
    $ head <file>
    $ head -35 <file> (this prints the first 35 lines)

    tail <file>
    - print the bottom few lines of a file.
    $ tail <file>
    $ tail -23 <file> (this prints the last 23 lines)

    grep <text> file
    - extracts the <text> from a file and prints it to standardout (your screen).
    (too powerful to really express in one little summary.)
    - grep is powerful. you'll need to `man grep` or `grep --help` for more usage.

    sort <file>
    - sorts the contents of a file

    - prints columns of text, for one use...
    (too powerful to really express in one little summary.)
    - here's a simple use of awk:
    $ cat /etc/passwd | awk -F: '{ print $3,"\t",$1 }'
    - this pipes (redirects the stdout) the output from cat to awk.
    - awk then prints columns 3 and 1 of output, using : as a separator.
    - the ,"\t", puts a tab between the entries.

    - replace text in a file.
    (too powerful to really express in one little summary.)
    $ cat /var/log/messages | sed s/"Jun 29"/"Jul 4"/g
    - this changes every Jun 29th entry to appear as Jul 4.

    - this will not change the file, but you can output the text to a new file.
    $ cat /var/log/messages | sed s/"Jun 29"/"Jul 4"/g > <newfile.text>

    I hope that this helps.

  3. #3
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Hi, ravi2082.

    I think ppoteete gave you a good start on what to look at, and excellent advice on looking at man pages.

    Did you mean something more specialized when you wrote parsing files? ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  4. $spacer_open
  5. #4
    the option to use with wc is l to get only the number of lines and the file, so:
    wc -l <file>
    To get only the number of lines use sed:
    sed -n '$=' <file>
    Now what? You have Linux installed and running. The GUI is working fine, but you are getting tired of changing your desktop themes. You keep seeing this "terminal" thing. Don't worry, they'll show you what to do @
    <~ ~>

  6. #5
    thanks !!!!!!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts