Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 2004
    Location
    Winston-Salem, NC
    Posts
    25

    Unanswered: Extract 2nd and 5th line from a file

    I'm having troubles trying to extract the 2nd and 5th line from my file. It doesn't matter to me what tool is used: grep, sed, awk, perl, etc., I just need help. I have multiple files and the 2nd and 5th line change everyday with a new timestamp in all of them. I want to do something like this:

    $ cat MOB.stats.04* | print the 2nd and 5th lines

    Any help would be greatly appreciated.

    Thanks,
    Jeff

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    This is rather crude, but:
    Code:
    cat MOB.stats.04* | perl -e "while (<>) { if (2==$. || 5 == $.) { print $_; };};"
    should work.

    -PatP

  3. #3
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Try something like this
    Code:
    # With sed
    
    sed -n '2p;5p' input_file
    
    # With awk
    
    awk 'NR==1 || NR==5' input_file
    
    # With awk, for multiple input files
    
    awk 'FNR==1 || FNR==5' input_file1 input_file2 ...
    awk 'FNR==1 || FNR==5' MOB.stats.04*
    Last edited by aigles; 04-08-04 at 17:54.
    Jean-Pierre.

  4. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    ...or you could also use:
    Code:
    cat MOB.stats.04* | perl -n -e "if (2==$. || 5==$.) {print}"
    too!

    -PatP

  5. #5
    Join Date
    Apr 2004
    Location
    Winston-Salem, NC
    Posts
    25

    Only extracting lines from the 1st file

    Thanks, all this is very useful. My problem now is that all these different commands are only extracting lines 2 and 5 from the 1st file. It apparently is ignoring the other files even though I'm specifying a wildcard: MOB.stats.04*

    Here are three of the filenames:
    MOB.stats.04012004
    MOB.stats.04022004
    MOB.stats.04032004

    Thanks for all your help so far.

    Jeff

    Originally posted by aigles
    Try something like this
    Code:
    # With sed
    
    sed -n '2p;5p' input_file
    
    # With awk
    
    awk 'NR==1 || NR==5' input_file
    
    # With awk, for multiple input files
    
    awk 'FNR==1 || FNR==5' input_file1 input_file2 ...
    awk 'FNR==1 || FNR==5' MOB.stats.04*

  6. #6
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Have you tried the last command ?
    Code:
    awk 'FNR==1 || FNR==5' MOB.stats.04*
    Jean-Pierre.

  7. #7
    Join Date
    Apr 2004
    Location
    Winston-Salem, NC
    Posts
    25
    Jean-Pierre,

    Thanks for the quick response. I did try that one, but with no luck:

    kzcd004:MOB:/home/a243450/temp/stats $ awk 'FNR==1 || FNR==5' MOB.stats.04*
    kzcd004:MOB:/home/a243450/temp/stats $

    Thanks,
    Jeff

    Originally posted by aigles
    Have you tried the last command ?
    Code:
    awk 'FNR==1 || FNR==5' MOB.stats.04*

  8. #8
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Try with nawk instead of awk
    Jean-Pierre.

  9. #9
    Join Date
    Apr 2004
    Location
    Winston-Salem, NC
    Posts
    25
    Ah ha, that worked! Thanks so much.

    Jeff

    Originally posted by aigles
    Try with nawk instead of awk

  10. #10
    Join Date
    Apr 2004
    Location
    Winston-Salem, NC
    Posts
    25
    The output is exactly what I wanted. Outstanding:

    kzcd004:MOB:/home/a243450/temp/stats $ nawk 'FNR==2 || FNR==5' MOB.stats.04*
    04/01/2004|122255
    04/01/2004|36241
    04/02/2004|131535
    04/02/2004|39169
    04/03/2004|115801
    04/03/2004|33775
    04/04/2004|91806
    04/04/2004|26890
    04/05/2004|130187
    04/05/2004|38277
    04/06/2004|133905
    04/06/2004|39793
    04/07/2004|130981
    04/07/2004|39065
    04/08/2004|132065
    04/08/2004|39326

    One quick question though. Is there anyway to grab just the numbers? If not, I can modify my orginial script to put a space b/w the date, pipe, and numbers and then awk 'em out.

    Thanks,
    Jeff

    Originally posted by normanjr29
    Ah ha, that worked! Thanks so much.

    Jeff

  11. #11
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Try something like this :
    Code:
    nawk -F'|' 'FNR==1 || FNR==5 {print $2}' MOB.stats.04*
    Jean-Pierre.

  12. #12
    Join Date
    Apr 2004
    Location
    Winston-Salem, NC
    Posts
    25
    Jean-Pierre,

    Thank you so much for your help. You've been a great help. I guess I need to learn a little nawk Have a great day.

    Thanks,
    Jeff

    Originally posted by aigles
    Try something like this :
    Code:
    nawk -F'|' 'FNR==1 || FNR==5 {print $2}' MOB.stats.04*

Posting Permissions

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