Solved

csh script read multiple values from each line in text file

Posted on 2013-11-16
3
3,062 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 28

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

910 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

21 Experts available now in Live!

Get 1:1 Help Now