Find the answer to your Linux question:
Results 1 to 5 of 5
What's the best way to use the command line to read a specific line from a file. I need a bash script which goes through a bunch of files in ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2004
    Posts
    24

    Quick Question in command line


    What's the best way to use the command line to read a specific line from a file. I need a bash script which goes through a bunch of files in a directory and pulls out specific lines from them. cat will read the entire file, and grep only seems to allow me to search for strings. I need a good way to read in the say 7950th line of a file into my script.

    Thanks for your help,

    ~Lyuokdea

  2. #2
    Linux User
    Join Date
    Jun 2006
    Posts
    311
    Hi Lyuokdea,
    You can use either sed or awk.

    If you want to use sed, then run : -
    $ sed '7950q;d' file_name
    If you want to use awk, then run : -
    $ awk 'NR==7950 {print;exit}' file_name
    With Regards,
    Thinker

  3. #3
    Just Joined!
    Join Date
    Aug 2004
    Posts
    24
    cool, thanks for the quick reply.

    ~Lyuokdea

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Aug 2004
    Posts
    24
    one more question on a similar problem, how can I take a line that was read in, and extract a number from a line of multiple numbers. Right now I have a bunch of lines of numbers that look like

    50000 100 200 -400 16 7

    and I need to get the number 16 into the program. Is there some sort of string splicing command in BASH that allows you to split on the spaces?

    ~Lyuokdea

  6. #5
    Linux User
    Join Date
    Jun 2006
    Posts
    311
    Hi Lyuokdea,
    Right now I have a bunch of lines of numbers that look like

    50000 100 200 -400 16 7

    and I need to get the number 16 into the program. Is there some sort of string splicing command in BASH that allows you to split on the spaces?
    You can use awk for doing that. You need to extract the 5th element in the every line of a file in which each elements in the line are separated by spaces. So, run this for getting that done : -
    $ gawk '{print $5}' file_name
    With Regards,
    Thinker

Posting Permissions

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