|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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/passwdI 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
|
||||
|
||||
|
don't use "$" while printing the array element
|
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
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"}}' filenameLast edited by vidyadhar85; 12-14-2009 at 06:13 AM.. |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
I can't really understand why we take this "i<=(length(var)+1)/2" in for but it works great.
Thanx a lot ![]() |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
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 |
|
|