The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to print empty line when the a field is changed yahyaaa Shell Programming and Scripting 9 06-17-2008 10:31 AM
Print last occurrence if first field match Raynon Shell Programming and Scripting 9 03-13-2008 03:28 AM
Compare dates in a field and print the latest date row cvkishore Shell Programming and Scripting 1 08-04-2007 07:58 AM
regexp to print after a field seperator ramky79 Shell Programming and Scripting 7 05-09-2007 06:31 PM
egrep field print knc9233 UNIX for Dummies Questions & Answers 2 02-14-2007 02:07 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-13-2008
Deanne Deanne is offline
Registered User
  
 

Join Date: May 2007
Posts: 31
Question how to print field n of line m

Hi everyone, I have a basic csh/awk question.

How do I print a given field from a given line in a given file?

Thanks in advance!
  #2 (permalink)  
Old 10-13-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
Code:
awk 'NR==given_line{print $given_field}' given_file
Regards
  #3 (permalink)  
Old 10-13-2008
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
^^^^You missed the given field separator
  #4 (permalink)  
Old 10-13-2008
Deanne Deanne is offline
Registered User
  
 

Join Date: May 2007
Posts: 31
thanks franklin! will try it!

oh im using the default field separator (space)
  #5 (permalink)  
Old 01-14-2009
Deanne Deanne is offline
Registered User
  
 

Join Date: May 2007
Posts: 31
Hmmm, can someone help me why it doesnt work when i use a variable for the "given_line", eg:

awk 'NR==$counter{print $25}' $argv[1]

thanks in advance!
  #6 (permalink)  
Old 01-14-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
Quote:
Originally Posted by Deanne View Post
Hmmm, can someone help me why it doesnt work when i use a variable for the "given_line", eg:

awk 'NR==$counter{print $25}' $argv[1]

thanks in advance!
Try this:

Code:
 awk -v var=$counter 'NR==var{print $25}' $argv[1]
Regards
  #7 (permalink)  
Old 01-14-2009
krishmaths krishmaths is offline
Registered User
  
 

Join Date: Sep 2006
Location: Mysore, India
Posts: 191
You need to use the -v option in awk to use shell variables, as below:

Code:
awk -v c=$counter 'NR==c {print $25}' $argv[1]
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 08:05 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0