Solved

csh script read multiple values from each line in text file

Posted on 2013-11-16
3
3,618 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] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
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…
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…

728 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