The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find a word in a file, and change a word beneath it ?? vikas027 Shell Programming and Scripting 2 02-13-2008 12:23 PM
Add a word at the end of each line in a file bhalotias Shell Programming and Scripting 6 01-29-2008 07:07 AM
how to move word by word on command line pbsrinivas UNIX for Dummies Questions & Answers 1 11-23-2007 02:17 AM
Need to replace the first word of a line if it occurs again in the next line(shell) geeko Shell Programming and Scripting 1 09-25-2007 07:15 AM
how to substitute more than one word in a text file? forevercalz Shell Programming and Scripting 2 01-12-2006 02:32 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-16-2006
Registered User
 

Join Date: Aug 2006
Posts: 2
Unhappy Can a shell script pull the first word (or nth word) off each line of a text file?

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?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 08-16-2006
Registered User
 

Join Date: Jan 2005
Posts: 682
for n in `cat filename` will also read all words into $n if you change the IFS variable to newlines only.
Reply With Quote
  #3 (permalink)  
Old 08-16-2006
muirhejs's Avatar
Registered User
 

Join Date: Aug 2006
Posts: 17
So if I understand it, you might have a file like this:
--- filename ---
file1 10
file2 20
file3 30
file4 40
---------------

Say I want to get the word count of each file and print next to it the corresponding 10, 20, 30, or 40..
~$ cat filename | while read line; do x=`echo $line | awk '{print $1}'`; y=`echo $line | awk '{print $2}'`; echo `wc -l $x` $y; done

6 file1 10
46 file2 20
8 file3 30
87 file4 40

Another way uses awk:

~$ cat filename | while read line; do a=`echo $line | awk '{print "b="$1,"c="$2}'`; eval $a; echo `wc -l $b` $c;done
Reply With Quote
  #4 (permalink)  
Old 08-17-2006
Registered User
 

Join Date: Aug 2006
Posts: 2
Wink Reading word number x of line y in a script of a file of z lines

I'll give that thought a try and post the complete script.....
Reply With Quote
  #5 (permalink)  
Old 08-17-2006
Registered User
 

Join Date: May 2006
Posts: 95
the first word ? assuming hat is word are space separated you can use 'cut'

cut -d\ -f1 <file

please note the are 2 space after the \ 1. as argument 2. as separator
Reply With Quote
  #6 (permalink)  
Old 08-17-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,502
Code:
#!/bin/ksh
filename="$1"
n=$2
awk -v fld=$n '{if(NF>=fld) {print $fld} } ' "$filename"
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:20 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0