Solved

csh script read multiple values from each line in text file

Posted on 2013-11-16
3
3,394 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 29

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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 …
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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