Solved

csh script read multiple values from each line in text file

Posted on 2013-11-16
3
2,939 Views
Last Modified: 2013-11-16
Hi,

I have a text file with entries as follows:

00001 99999 testlinea
00002 99998 testlineb
00002 99997 testlineb

In a unix script (which is csh and can't be changed), I want to read the contents of each file line by line and attribute each "column" of data to a separate variable so that further processing can be done on it before then reading the second line

In an ideal world this would be something line

foreach $line (`cat filea`)
   set valuea= `echo $line | awk '{print1}'`
   set valueb= `echo $line | awk '{print2}'`
   set valuec= `echo $line | awk '{print3}'`
   [do other things before moving on to the next line in filea]
end

Open in new window


but that doesn't work as it sees each "column" as a "line".

Googling shows plenty of suggestions for when other unix shells, but I'm looking for a way to do this in csh.

Any help much appreciated.
0
Comment
Question by:yelbow
3 Comments
 
LVL 27

Assisted Solution

by:serialband
serialband earned 250 total points
ID: 39653846
Your awk statement needs to define the separator to a space
awk -F" " '{print $1}' filea


Since you're calling awk, you might be able to just use awk to do the work a little more efficiently by calling awk just once instead of 3 times.  If you're running that on some really large files, it might save you a lot of time.

awk -F" " '{valuea = $1; valueb = $2; valuec = $3; RUN_OTHERSTUFF }' filea
0
 
LVL 48

Accepted Solution

by:
Tintin earned 250 total points
ID: 39653935
csh really is an awful scripting language, so commiserations on having to use it.

You should be able to do:

#!/bin/csh
foreach line ( "`cat file`" )
   set argv = ( $line )
   set valuea = $1
   set valueb = $2
   set valuec = $3

   echo $valuea
   echo $valueb
   echo $valuec
end 

Open in new window

0
 

Author Closing Comment

by:yelbow
ID: 39654059
Thanks!  Both seem valid, but went with Tintins solution - thanks for your time, and your sympathy ;)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now