Reading File Enquiry


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading File Enquiry
# 1  
Old 04-06-2011
Reading File Enquiry

Hi There,

I am a beginner when it comes to writing scripts in LINUX so please bear with me when it comes to the question as it is probably a very simple issue.

I am just using some free time to write some LINUX scripts and have just worked ona very basic program that allows a user to input their name and then enter their top ten films. The user input is then written to a file using the following command :

echo "$input1,$input2.....etc" >> filename

In the output file the data is stored as below

input1,input2,etc

I am trying to find the right code to be able to assign the data from the output file to indiviual variables when reading from the output file. As there are liking to be spaces seperating word in the film title the only thing that is unique to seperate each instance is the ,

Is there some coding that I can use to complete this operation??

Sorry if the wording is not completely friendly but like I said I'm a beginner here so trying to do my best lol

RedAlex76
# 2  
Old 04-06-2011
To be honest, this sounds suspiciously like homework (which isn't allowed in this forum and for which there is a special place).

I will give you a pointer, but then close this thread because of this suspicion - sorry if i'm wrong, but we had a lot of students being dishonest about their homework.

To split a string at a delimiter into pieces you can use "cut". See "man cut" for details.

For instance: lets split "a bc de fgh" by the delimiter "space":

Code:
# echo "a bc de fgh" | cut -d' ' -f1   # will yield "a"
# echo "a bc de fgh" | cut -d' ' -f2   # will yield "bc"
# echo "a bc de fgh" | cut -d' ' -f3   # will yield "de"
# echo "a bc de fgh" | cut -d' ' -f4   # will yield "fgh"

You can assign the output of a command to a variable using:

Code:
variable=$(somecommand)

If you wrap double quotes around it it will even protect whitespace:

Code:
variable="$(somecommand)"

This should enable you to finish the script. I hope this helps.

-closed-

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh Script, Reading A File, Grepping A File Contents In Another File

So I'm stumped. First... APOLOGIES... my work is offline in an office that has zero internet connectivity, as required by our client. If need be, I could print out my script attempts and retype them here. But on the off chance... here goes. I have a text file (file_source) of terms, each line... (3 Replies)
Discussion started by: Brusimm
3 Replies

2. UNIX for Dummies Questions & Answers

Reading Xml file and print the values into the text file in columnwise?

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (4 Replies)
Discussion started by: sravanreddy
4 Replies

3. UNIX for Dummies Questions & Answers

Reading XML file and print the values in the text file using Linux shell script

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (1 Reply)
Discussion started by: sravanreddy
1 Replies

4. Web Development

CGI enquiry

Hello, I am new to Linux and have a general question. In linux and unix you can have CGI scripts. I am aware of CGI as Computer Generated Images in graphics and Common Gateway Interface, but what are CGI scripts in relation to Linux or Unix? Are CGI's in Linux and Unix relevant to only... (1 Reply)
Discussion started by: ajfarroll
1 Replies

5. Shell Programming and Scripting

ENQUIRY WHETHER SCRIPT FOR DETECTING WORDS FROM A SET OF LETTERS EXISTS

Hello, I am interested in finding out whether someone has a perl or awk script which takes a set of letters such as wak and referring to a dictionary spews out all possible forms such as awk, kaw etc. If someone has such a script, could it be put up please. The script should handle Unicode. Many... (0 Replies)
Discussion started by: gimley
0 Replies

6. Shell Programming and Scripting

Reading UNIX commands from file and redirecting output to a file

Hi All I have written the following script: #!/bin/ksh while read cmdline do echo `$cmdline` pid="$cmdline" done<commands.txt =========== commands.txt contains: ps -ef | grep abc | grep xyz |awk '{print $2}; My objective is to store the o/p of the command in a variable and do... (8 Replies)
Discussion started by: rahulparo
8 Replies

7. Shell Programming and Scripting

Searching for Log / Bad file and Reading and writing to a flat file

Need to develop a unix shell script for the below requirement and I need your assistance: 1) search for file.log and file.bad file in a directory and read them 2) pull out "Load_Start_Time", "Data_File_Name", "Error_Type" from log file 4) concatinate each row from bad file as... (3 Replies)
Discussion started by: mlpathir
3 Replies

8. UNIX for Advanced & Expert Users

Reading a file and sending mail based on content of the file

Hi Gurus, I am having an requirement. i have to read a list file which contains file names and send mail to different users based on the files in the list file. eg. if file a.txt exists then send a mail to a@a.com simillary for b.txt,c.txt etc. Thanks for your help, Nimu (6 Replies)
Discussion started by: nimu1979
6 Replies

9. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies
Login or Register to Ask a Question