If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Extract 2nd and 5th line from a file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-08-04, 16:13
normanjr29 normanjr29 is offline
Registered User
 
Join Date: Apr 2004
Location: Winston-Salem, NC
Posts: 25
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
Reply With Quote
  #2 (permalink)  
Old 04-08-04, 16:45
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,614
This is rather crude, but:
Code:
cat MOB.stats.04* | perl -e "while (<>) { if (2==$. || 5 == $.) { print $_; };};"
should work.

-PatP
Reply With Quote
  #3 (permalink)  
Old 04-08-04, 16:49
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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*
__________________
Jean-Pierre.

Last edited by aigles; 04-08-04 at 16:54.
Reply With Quote
  #4 (permalink)  
Old 04-08-04, 17:37
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,614
...or you could also use:
Code:
cat MOB.stats.04* | perl -n -e "if (2==$. || 5==$.) {print}"
too!

-PatP
Reply With Quote
  #5 (permalink)  
Old 04-09-04, 08:54
normanjr29 normanjr29 is offline
Registered User
 
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

Quote:
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*
Reply With Quote
  #6 (permalink)  
Old 04-09-04, 09:03
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Have you tried the last command ?
Code:
awk 'FNR==1 || FNR==5' MOB.stats.04*
__________________
Jean-Pierre.
Reply With Quote
  #7 (permalink)  
Old 04-09-04, 09:29
normanjr29 normanjr29 is offline
Registered User
 
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

Quote:
Originally posted by aigles
Have you tried the last command ?
Code:
awk 'FNR==1 || FNR==5' MOB.stats.04*
Reply With Quote
  #8 (permalink)  
Old 04-09-04, 09:33
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Try with nawk instead of awk
__________________
Jean-Pierre.
Reply With Quote
  #9 (permalink)  
Old 04-09-04, 09:39
normanjr29 normanjr29 is offline
Registered User
 
Join Date: Apr 2004
Location: Winston-Salem, NC
Posts: 25
Ah ha, that worked! Thanks so much.

Jeff

Quote:
Originally posted by aigles
Try with nawk instead of awk
Reply With Quote
  #10 (permalink)  
Old 04-09-04, 09:42
normanjr29 normanjr29 is offline
Registered User
 
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

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

Jeff
Reply With Quote
  #11 (permalink)  
Old 04-09-04, 09:45
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Try something like this :
Code:
nawk -F'|' 'FNR==1 || FNR==5 {print $2}' MOB.stats.04*
__________________
Jean-Pierre.
Reply With Quote
  #12 (permalink)  
Old 04-09-04, 09:49
normanjr29 normanjr29 is offline
Registered User
 
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

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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On