• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 456
  • Last Modified:

C shell programming---how to read a file (word by word) and put into variable

Hi, I am a new user to UNIX.
I faced some C shell script problem. I need to read a file(for eg. a.txt). The content of file are like this:

JOHN 12345 abcd
ANTHONY 12344 efrg
ALVINSON 11131 ytry

So, how can I read this file(word by word) and then put into appropiate variables? For each line, the first word I want to put in $NAMES, the second word I want to put in $ID and the third word I want to put in $PASSWORD.

So how can I write this C shell script?
Thank you.  


 
 
0
sunjzblue
Asked:
sunjzblue
  • 3
  • 2
1 Solution
 
TintinCommented:
Do you have to cripple yourself by writing it in csh, or can you use a sensible shell?
0
 
HamdyHassanCommented:
0
 
HamdyHassanCommented:
I am not sure about the syntax

set key_file = `cat file.txt`
while ($#key_file > 0)
    set f1 = `echo $key_file[1] | awk -F- '{print $1}'`
    set f2 = `echo $key_file[1] | awk -F- '{print $2}'`
    set f3 = `echo $key_file[1] | awk -F- '{print $3}'`
    shift key_file
end

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
TintinCommented:
Here's a sh/ksh/bash version

#!/bin/sh
while read NAMES IDS PASSWORD
do
  ..
done <a.txt
0
 
HamdyHassanCommented:
If csh is a must

----------------
Source code
----------------
bin/ee> cat csh1.csh
#!/usr/bin/csh -f
set line  = "$<"
while ( "$line" != "" )
 set f1 = `echo $line | awk  '{print $1}'`
 set f2 = `echo $line | awk  '{print $2}'`
 set f3 = `echo $line | awk  '{print $3}'`
   
 echo "Name : " $f1
 echo "ID   : " $f2
 echo "PW   : " $f3
 set line  = "$<"
end

----------------
How to run this
----------------
bin/ee> csh1.csh < file.txt
Name :  JOHN
ID   :  12345
PW   :  abcd
Name :  ANTHONY
ID   :  12344
PW   :  efrg
Name :  ALVINSON
ID   :  11131
PW   :  ytry
0
 
liddlerCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by HamdyHassan

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

liddler
EE Cleanup Volunteer
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now