![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | 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. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need to replace the first word of a line if it occurs again in the next line(shell) | geeko | Shell Programming and Scripting | 4 | 06-18-2009 02:36 PM |
| find a word in a file, and change a word beneath it ?? | vikas027 | Shell Programming and Scripting | 2 | 02-13-2008 04:23 PM |
| Add a word at the end of each line in a file | bhalotias | Shell Programming and Scripting | 6 | 01-29-2008 11:07 AM |
| how to move word by word on command line | pbsrinivas | UNIX for Dummies Questions & Answers | 1 | 11-23-2007 06:17 AM |
| how to substitute more than one word in a text file? | forevercalz | Shell Programming and Scripting | 2 | 01-12-2006 06:32 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Greetings.
I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be treated differently, and captured as a variable. If I try using the for n in `cat filename` approach, it simply takes each word from the filename. And since the file may have multiple lines, I want to use a text file as the basis for a shell script, using variables taken from the file on a line by line basis. Is there a simple way? Searching google and other search engines pulls up too many read herrings or inappropriate pages. Can anyone please help? ![]() |
|
||||
|
Code:
#!/bin/ksh
filename="$1"
n=$2
awk -v fld=$n '{if(NF>=fld) {print $fld} } ' "$filename"
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|