To read data word by word from given file & storing in variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To read data word by word from given file & storing in variables
# 1  
Old 06-09-2010
To read data word by word from given file & storing in variables

File having data in following format :
file name : file.txt
--------------------
111111;name1
222222;name2
333333;name3

I want to read this file so that I can split these into two paramaters i.e. 111111 & name1 into two different variables(say value1 & value2).

i.e val1=11111 & val2=name1...& so on....

In between these two parameters semicolon is there ..


Can anybody help me to write a shell script for this ?
# 2  
Old 06-09-2010
Hi,

look for "awk", "cut" in man pages or in the forum, there is a lot of similar problems solved.
# 3  
Old 06-09-2010
Quote:
Originally Posted by sjoshi98
File having data in following format :
file name : file.txt
--------------------
111111;name1
222222;name2
333333;name3

I want to read this file so that I can split these into two paramaters i.e. 111111 & name1 into two different variables(say value1 & value2).

i.e val1=11111 & val2=name1...& so on....

In between these two parameters semicolon is there ..


Can anybody help me to write a shell script for this ?
For something simply formatted like this you can change your IFS (internal field separator) to whatever is separating your values and then loop through it. By default the IFS is a space " ", but in this case it's a ";". So you can do the following:

Code:
(12:25:08\[D@DeCoBox15)
[~]$ cat test
111111;name1
222222;name2
333333;name3

(12:26:43\[D@DeCoBox15)
[~]$ IFS=";";while read value name;do echo "Value is $value and name is $name";done < test
Value is 111111 and name is name1
Value is 222222 and name is name2
Value is 333333 and name is name3

What's even more fun (and I've only recently started to understand) is putting everything into an array and then call it from there:

Code:
(12:29:50\[D@DeCoBox15)
[~]$ IFS=";";while read line;do line=($line);echo "name is "${line[0]}" value is "${line[1]}"";done < test
name is 111111 value is name1
name is 222222 value is name2
name is 333333 value is name3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to check word count of each word in file

I am trying to figure out to find word count of each word from my file sample file hi how are you hi are you ok sample out put hi 1 how 1 are 1 you 1 hi 1 are 1 you 1 ok 1 wc -l filename is not helping , i think we will have to split the lines and count and then print and also... (4 Replies)
Discussion started by: mirwasim
4 Replies

2. Shell Programming and Scripting

Find a word and increment the number in the word & save into new files

Hi All, I am looking for a perl/awk/sed command to auto-increment the numbers line in file, P1.tcl: run_build_model sparc_ifu_dec run_drc set_faults -model path_delay -atpg_effectiveness -fault_coverage add_delay_paths P1 set_atpg -abort_limit 1000 run_atpg -ndetects 1000 I would like... (6 Replies)
Discussion started by: jypark22
6 Replies

3. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

4. Programming

C Code to read a word from file

Hello All, I have to write a C Code to read a word from file and Keep track of the number of word occurrence in each line and total in the file. Maintaining total count is easier but maintaining per line count is what I am struggling to achieve. I thought of maintaining linked list... (3 Replies)
Discussion started by: anand.shah
3 Replies

5. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

6. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

7. UNIX for Dummies Questions & Answers

how to read the second word of a text file

Folks, how to read the second word of the first line from a text file. Text file does not have any delimiters in the line and has words at random locations. Basically the text file is a log and i want to capture a number that is in second position. Appreciate your help Venu (1 Reply)
Discussion started by: venu
1 Replies

8. UNIX for Dummies Questions & Answers

Read last word of file name

Hi I am writing a script that needs to read all file from a directory and print only last word of all file names. My script: for file in /documents/files/ do $shortFile=$(file##.*) echo $shortFile done All my file names in /document/files/ directory are like unix_ubuntu but I need to... (1 Reply)
Discussion started by: watsup
1 Replies

9. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies

10. Shell Programming and Scripting

how to read each word in a file

Hi, I want to read each word in a file. start at a particular character say '%' and read till another character say ')' (these two characters form the part of my file). then i want to delete the whole sentence(that is between '%' and ')' ) and keep the remaining file intact. Its urgent... (1 Reply)
Discussion started by: kaps_jhaver
1 Replies
Login or Register to Ask a Question