Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

awk to print exact field number

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-14-2009
covis covis is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 14 December 2009, 5:51 AM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Data awk to print exact field number

Hello there.
I want some help with awk
I have this script that reads a file from awk and you can insert from keyboard any list from the fields that exist so to be printed on monitor


Code:
echo "give a choice"
read -a ans
array=${ans[@]}
awk -F: -v k="$array" '{
                         split(k,l," ")
                         for(i=1;i<=NF;i++)
                            {          
                                         printf("%s \t",$l[i])
                               
                             }  printf("\n")
                       }' /etc/passwd

I get the field numbers from an array and then I split this array in awk.
If I try to print "l[i]" it works but when I try to print it as field number I dont get the correct result.

I want my outpout to be something like this :

Code:
for example:
>give a choice
:1 5 3
and then I will like to print the first field in each line then the 5th and then the 3rd
first fifth third
first fifth third
first fifth third
............

Sponsored Links
    #2  
Old Unix and Linux 12-14-2009
vidyadhar85's Unix or Linux Image
vidyadhar85 vidyadhar85 is offline Forum Advisor  
The Tutor
 
Join Date: Jun 2008
Last Activity: 17 April 2015, 12:45 AM EDT
Location: INDIA, Bangalore
Posts: 2,049
Thanks: 16
Thanked 106 Times in 102 Posts
don't use "$" while printing the array element
Sponsored Links
    #3  
Old Unix and Linux 12-14-2009
covis covis is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 14 December 2009, 5:51 AM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
if i dont it prints the number that i have given ... not the first and the second field for example but the number 1 and 2.
    #4  
Old Unix and Linux 12-14-2009
vidyadhar85's Unix or Linux Image
vidyadhar85 vidyadhar85 is offline Forum Advisor  
The Tutor
 
Join Date: Jun 2008
Last Activity: 17 April 2015, 12:45 AM EDT
Location: INDIA, Bangalore
Posts: 2,049
Thanks: 16
Thanked 106 Times in 102 Posts
Quote:
Originally Posted by covis View Post
if i dont it prints the number that i have given ... not the first and the second field for example but the number 1 and 2.
sorry my mistake... use this..

Code:
awk -v var="2 5 7" '{split(var,a," ")
for(i=1;i<=(length(var)+1)/2);i++){printf $(a[i])"\t"}{printf "\n"}}' filename


Last edited by vidyadhar85; 12-14-2009 at 06:13 AM..
Sponsored Links
    #5  
Old Unix and Linux 12-14-2009
covis covis is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 14 December 2009, 5:51 AM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
I can't really understand why we take this "i<=(length(var)+1)/2" in for but it works great.
Thanx a lot Linux Linux
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[awk] print from field n to field x daPeach UNIX for Dummies Questions & Answers 5 09-25-2009 08:25 AM
wt is the exact procedure to share windows printer on linux machine to print from it pankajd UNIX Desktop for Dummies Questions & Answers 1 11-14-2008 12:24 AM
SED: delete and print the only exact matched pattern new_buddy Shell Programming and Scripting 6 07-09-2008 04:19 AM
to print number one less than actual number cdfd123 Shell Programming and Scripting 4 09-06-2007 06:56 AM
egrep field print knc9233 UNIX for Dummies Questions & Answers 2 02-14-2007 01:07 PM



All times are GMT -4. The time now is 12:36 AM.