Solved

csh script read multiple values from each line in text file

Posted on 2013-11-16
3
3,490 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…

732 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