Can a shell script pull the first word (or nth word) off each line of a text file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can a shell script pull the first word (or nth word) off each line of a text file?
# 1  
Old 08-16-2006
Data 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?
Smilie
# 2  
Old 08-16-2006
for n in `cat filename` will also read all words into $n if you change the IFS variable to newlines only.
# 3  
Old 08-16-2006
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  
Old 08-17-2006
Hammer & Screwdriver 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  
Old 08-17-2006
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  
Old 08-17-2006
Code:
#!/bin/ksh
filename="$1"
n=$2
awk -v fld=$n '{if(NF>=fld) {print $fld} } ' "$filename"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

2. Shell Programming and Scripting

How to grep nth word in line?

my input file content is like this GEFITINIB 403 14 -4.786873 -4.786873 -1.990111 0.000000 0.000000 -1.146266 -39.955912 483 VANDETANIB 404 21 -4.754243 -4.754243 -2.554131 -0.090303 0.000000 -0.244210 -41.615502 193 VANDETANIB 405 21 -4.737541 -4.737541 -2.670195 -0.006006 0.000000 -0.285579... (4 Replies)
Discussion started by: chandu87
4 Replies

3. Shell Programming and Scripting

Get the nth word of mth line in a file

Hi.. May be a simple question but I just began to write unix scripts a week ago, for sorting some huge amount of experiment data, so I got no common sense about unix scripting and really need your helps... The situation is, I want to read the nth word of mth line in a file, and then store it... (3 Replies)
Discussion started by: freezelty
3 Replies

4. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word "description" excluding weird characters like $&lmp and without html tags in the new file output.txt. Help me. Thanx in advance. I have attached the input... (4 Replies)
Discussion started by: sachit adhikari
4 Replies

5. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script?

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word description excluding weird characters like $$#$#@$#@***$# and without html tags in the new file output.txt. Help me. Thanx in advance. My final goal is to... (11 Replies)
Discussion started by: sachit adhikari
11 Replies

6. Shell Programming and Scripting

How to read nth word from delimited text file?

Hi i am new in scripting how i can get 2 elements from first line of delimited txt file in shell scripts. AA~101010~0~AB~8000~ABC0~ BB~101011~0~BC~8000~ABC~ CC~101012~0~CD~8000~ABC0~ DD~101013~0~AB~8000~ABC~ AA~101014~0~BC~8000~ABC0~ CC~101015~0~CD~8000~ABC~ can anyone plse help?... (3 Replies)
Discussion started by: sushine11
3 Replies

7. Shell Programming and Scripting

Reading a word from a text file into shell script

Hi, I am new to shell programming.I need to write a script that would accept a word from each line fo an input text file.Can anyone help me with this?Exact requirement: word1 word2 word3 (separated by space) .Now I need word3 from each such line in the text file. Thanks in Advance, Manish (3 Replies)
Discussion started by: manish007
3 Replies

8. Shell Programming and Scripting

how to find third(nth) word in all line from a file

For example i'm having the below contents in a file: expr is great when you want to split a string into just two parts. The .* also makes expr good for skipping a variable number of words when you don't know how many words a string will have. But expr is lousy for getting, say, the fourth word... (2 Replies)
Discussion started by: bangarukannan
2 Replies

9. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

10. Shell Programming and Scripting

Read last word of nth line

Hi people; i want to read the last word of the 14th line of my file1.txt. Here is the EXACT 14th line of the file. 250 SectorPortnum=3,AuxPortInUngo=2,PortDeviceGroup=1,PortDeviceSet=1,PorDevice=1 20 >>> Set. i have to get the word Set. how can i call it and also how... (3 Replies)
Discussion started by: gc_sw
3 Replies
Login or Register to Ask a Question