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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-16-2006
tricky tricky is offline
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?
  #2 (permalink)  
Old 08-16-2006
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
for n in `cat filename` will also read all words into $n if you change the IFS variable to newlines only.
  #3 (permalink)  
Old 08-16-2006
muirhejs's Avatar
muirhejs muirhejs is offline
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
  #4 (permalink)  
Old 08-17-2006
tricky tricky is offline
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.....
  #5 (permalink)  
Old 08-17-2006
grumpf grumpf is offline
Registered User
  
 

Join Date: May 2006
Posts: 101
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
  #6 (permalink)  
Old 08-17-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
Code:
#!/bin/ksh
filename="$1"
n=$2
awk -v fld=$n '{if(NF>=fld) {print $fld} } ' "$filename"
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 07:28 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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