Find the answer to your Linux question:
Results 1 to 7 of 7
Can someone explain how you would go about printing the Nth line from a file. I understand multiple ways of doing it if N is a constant echo `awk '{ ...
  1. #1
    Just Joined!
    Join Date
    Mar 2009
    Posts
    31

    Printing Nth line from file

    Can someone explain how you would go about printing the Nth line from a file. I understand multiple ways of doing it if N is a constant

    echo `awk '{ if(NR==5) print $0 }' input`
    echo `sed -n '5p' input`
    echo `head -n5 input | less -n1`

    but how do you do this if N is not a constant. The way I have my script setup N is a parameter passed into the script when it is invoked. It tests to see if it is a number then either grabs that line from a file if it is a number or searches for other files if it is a name. Here's some of the code

    #!/bin/bash
    input=$1
    if [[ `echo $input | grep "[0-9]"` ]]
    then
    echo `head -n"$input" tempoutput | less -n 1` --this line doesn't work but it shows kinda what I want
    else
    for name in `find . -iname *$input*`
    do
    echo "$name" >> tempoutput
    done
    fi


    Thanks,
    John

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    This isn't a school assignment, is it?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Mar 2009
    Posts
    31
    Nope, this is a project I'm working on for myself. The whole project entails receiving a message from my phone and acting appropriately on it (downloading web pages using a Perl script, checking the status of some of my computers and other such things). It kinda mimics Googles service but extends it further.

    - John

  4. #4
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    Take the double quotes out, that's what's wrong:

    echo `head -n$input tempoutput | less -n 1`

    Also your test for a number is wrong. Anything with a digit in it (like aaa1bbb) will match. You want:

    if [[ `echo $input | grep "^[0-9]*$"` ]]

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by ProgramIt View Post
    Nope, this is a project I'm working on for myself. The whole project entails receiving a message from my phone and acting appropriately on it (downloading web pages using a Perl script, checking the status of some of my computers and other such things). It kinda mimics Googles service but extends it further.

    - John
    Ok. It's just that the TOS of this site prohibits us from doing school assignments for people, though we can point them in the right direction. You'd be amazed how many people try to get us to do their assignments for them.

    As for doing this, my personal approach would be to write a 10 line C or C++ program 'getNthLine' that takes one or more numerical arguments and pulls those lines out of the input file, printing them to stdout. The input could be stdin to keep it simple. You can also use shell, perl, or other scripts to do this (sed would work also, probably better than awk).
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  6. #6
    Linux Newbie
    Join Date
    Sep 2004
    Location
    UK
    Posts
    160
    try

    head -${LINE_NUMBER) $FILE_NAME | tail -1
    In a world without walls and fences, who needs Windows and Gates?

  7. #7
    Just Joined!
    Join Date
    Mar 2009
    Posts
    31
    Thanks for all of your replies got it working just how I wanted.

    Quote Originally Posted by lomcevak View Post
    Also your test for a number is wrong. Anything with a digit in it (like aaa1bbb) will match. You want:

    if [[ `echo $input | grep "^[0-9]*$"` ]]
    Thanks for the help on my regular expression. I knew it would take things like that for my input but didn't know how to easily fix it so I had just left it like it was. Now it works correctly.

    Thanks,
    John

Posting Permissions

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