?
Solved

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

Posted on 2003-03-15
6
Medium Priority
?
450 Views
Last Modified: 2013-12-26
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
Comment
Question by:sunjzblue
[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
  • 2
6 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 8144101
Do you have to cripple yourself by writing it in csh, or can you use a sensible shell?
0
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8146161
0
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8146223
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:Tintin
ID: 8148523
Here's a sh/ksh/bash version

#!/bin/sh
while read NAMES IDS PASSWORD
do
  ..
done <a.txt
0
 
LVL 9

Accepted Solution

by:
HamdyHassan earned 200 total points
ID: 8152294
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
 
LVL 18

Expert Comment

by:liddler
ID: 9845939
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

764 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