Read and Store


 
Thread Tools Search this Thread
Top Forums Programming Read and Store
# 1  
Old 01-14-2010
Read and Store

How can I read from the user input and store each word individually

for example

word1 word2 number1 number2 word 3 number3

server 192.25.1.1 55555 22 logged 15

I need to store and manipulate each word/number individually and these inputs are entered by the user.
# 2  
Old 01-14-2010
Shell has positional parameters - you can just read them with read into variables or address each separated by a blank with $1,$2,$3, ... or just get them all with $* or $@ and parse them.
# 3  
Old 01-14-2010
I'm new in C, so could you please elaborate.
# 4  
Old 01-14-2010
The above answer was for shell script since you didn't say you were using C.

What have you tried already?
# 5  
Old 01-14-2010
Oh absolutely my fault sorry lol. I thought this was posted in an appropriate forum for shell scripting, not in https://www.unix.com/high-level-programming/.

Anyhow for C, I'd try to parse the input from argc and argv. For reading input I would take fgets() but I am not very educated at C so better waiting for other answers. Sorry again for misleading.
# 6  
Old 01-14-2010
This may or may not do what you want. It parses words from stdin line by line and cuts them up with strtok.

Code:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
        char buf[512];
        while(fgets(buf, 512, stdin) != NULL)
        {
                char *t=strtok(buf, " ");
                while(t != NULL)
                {
                        printf("%s\n", t);
                        t=strtok(NULL, " ");
                }
        }

        return(0);
}

# 7  
Old 01-14-2010
thanks, but what's the difference between this code and using argc and argv arguments in main? is argc for numbers and argv for characters? will it do the work?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read a value from a file and store in a variable?

Hi, I have a file service.xml which has following content: <?xml version="1.0" encoding="UTF-8"?> <Service Ver="2.31.13"/> I want to read the value of Ver (that is 2.31.13) and assign to a variable which i further use. Please help me in that. (3 Replies)
Discussion started by: laxmikant15
3 Replies

2. Shell Programming and Scripting

Shell script to read a file and store in variables

I have a input file like this. Sample.txt 30 | TXDatacenter | TXBackupDC 10 | UKDatacenter | UKBackupDC 0 | NLDatacenter | NLBackupDC ...... ...... ...... I need to get these values in different variables like this. Load1=30 PriCenter1=TXDatacenter... (5 Replies)
Discussion started by: Visha
5 Replies

3. Shell Programming and Scripting

How to read values and store in array?

I am reading a value from a file and want to store the value in a dynamic array as i don't know the number of occurrences of the value in that file. How can i do that and then later fetch that value from array (25 Replies)
Discussion started by: Prachi Gupta
25 Replies

4. UNIX for Dummies Questions & Answers

How to store/read multiple values from a varible

Hi, when I enter 'ps -ef| grep process_name'/'psu | grep process_name', i am getting multiple number of lines output( i mean multiple no of processes).how can i store it one by one and echo it in the same way(one by one). part of script is var1=$(remsh hostname -l username ps -ef|grep... (2 Replies)
Discussion started by: jeanzibbin
2 Replies

5. Shell Programming and Scripting

Read the contents of a file and store them in a variable

Hi Gurus, I am trying for a scenario where in I want to read the contents of a file line by line and then store them in variables. Below is the script: #!/bin/ksh while read line do id=`echo $line | cut -f1 -d |` name=`echo $line | cut -f2 -d |` echo $id ... (11 Replies)
Discussion started by: svajhala
11 Replies

6. Shell Programming and Scripting

read xml tag attribute and store it in variable

Hi, How to read xml tag attributes and store into variable in shell script? Thanks, Swetha (5 Replies)
Discussion started by: swetha123
5 Replies

7. UNIX for Dummies Questions & Answers

Read a file and store each value in a variable

Hi, How to read a file and put the values in a script. E.g. file1.txt 02/12/2009;t1;t2 The script should read this file and put these values in 3 different variables x1,x2,x3 which can be used further. Thanks Ashu (3 Replies)
Discussion started by: er_ashu
3 Replies

8. UNIX for Dummies Questions & Answers

Read and store each line of a file to a variable

Hi all, I'm quite new to unix and hope that someone can help me on this. I'm using csh. Below is what i intend to do. 1. I stored some data in a file. 2. I intend to read the file line by line and store each line of data into a variable, so that i can used it later. Anyone have any... (4 Replies)
Discussion started by: seijihiko
4 Replies

9. UNIX for Advanced & Expert Users

how to read each letter from file and store it in variable.

Dear friends, i am writing csh script i have one dat file containing following data.like this. 08FD3 03A26 000FA0 FFFF0 BBA0F 00000 00000 from the above file i want to read each letter and store it in one variable. how it is possible. please help (7 Replies)
Discussion started by: rajan_ka1
7 Replies

10. Shell Programming and Scripting

store output to a file and read from it

Hello all, I need to run snoop command for a period of time (a day) and extract remote host column from it to find out who is accessing my server. When I run the following on the command line it works snoop -port 22 | awk '{print $3}' but when I do snoop -port 22 | awk '{print $3}' | while... (2 Replies)
Discussion started by: afadaghi
2 Replies
Login or Register to Ask a Question