extract fields from text file using delimiter!!


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers extract fields from text file using delimiter!!
# 1  
Old 09-25-2008
Bug extract fields from text file using delimiter!!

Hi All,

I am new to unix scripting, please help me in solving this assignment..

I have a scenario, as follows:
1. i have a text file(read1.txt) with the following data
sairam,123
kamal,122
etc..


2. I have to write a unix script such a way that, i need to take first row from text file and store "sairam" in variable1 and
"123" in variable 2 using comma as delimiter ...like that for all rows in that textfile


Thanks in advance.....Smilie
# 2  
Old 09-25-2008
i am using bourne shell
# 3  
Old 09-25-2008
var1=`head -1 read1.txt | cut -d"," -f1`
var2=`head -1 read1.txt | cut -d"," -f2`

see man page of "cut" for more options.
# 4  
Old 09-25-2008
What is the end goal you are trying to achieve?
# 5  
Old 09-25-2008
not satisfied with the solution!!

Quote:
Originally Posted by mk1216
var1=`head -1 read1.txt | cut -d"," -f1`
var2=`head -1 read1.txt | cut -d"," -f2`

see man page of "cut" for more options.


i need for all the rows to be cutted and keep field1 in var1 and field in var2 ...
i will be great full if u give the code in "for loop"......
# 6  
Old 09-25-2008
Quote:
please help me in solving this assignment..
Is this an assignment ? Smilie
# 7  
Old 09-25-2008
Try this..

Code:
awk -F"," '{print $1,$2}' read1.txt | while read var1 var2
do
        echo $var1 $var2
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to extract multiple values from file and add two additional fields

In the attached file I am trying to use awk to extract multiple values and create the tab-delimited desired output. In the output R_Index is a the sequential # and Pre_Enrichment is defaulted to .. I can extract from the values to the side of the keywords, but most are above and I can not... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

How to target certain delimiter to split text file?

Hi, all. I have an input file. I would like to generate 3 types of output files. Input: LG10_PM_map_19_LEnd_1000560 LG10_PM_map_6-1_27101856 LG10_PM_map_71_REnd_20597718 LG12_PM_map_5_chr_118419232 LG13_PM_map_121_24341052 LG14_PM_1a_456799 LG1_MM_scf_5a_opt_abc_9029993 ... (5 Replies)
Discussion started by: huiyee1
5 Replies

3. Shell Programming and Scripting

Splitting records in a text file based on delimiter

A text file has 2 fields (Data, Filename) delimited by # as below, Data,Filename Row1 -> abc#Test1.xml Row2 -> xyz#Test2.xml Row3 -> ghi#Test3.xml The content in first field has to be written into a file where filename should be considered from second field. So from... (4 Replies)
Discussion started by: jayakkannan
4 Replies

4. Shell Programming and Scripting

Shell script to put delimiter for a no delimiter variable length text file

Hi, I have a No Delimiter variable length text file with following schema - Column Name Data length Firstname 5 Lastname 5 age 3 phoneno1 10 phoneno2 10 phoneno3 10 sample data - ... (16 Replies)
Discussion started by: Gaurav Martha
16 Replies

5. Shell Programming and Scripting

extract fields from a downloaded html file

I have around 100 html files and in each html file I have 5-6 such paragraphs of a company and I need to extract the Name of the company from either the one after "title" or "/company" and then the number of employees and finally the location . <div class="search_result"> <div... (1 Reply)
Discussion started by: gubbu
1 Replies

6. UNIX for Dummies Questions & Answers

How to extract fields from etc/passwd file?

Hi! i want to extract from /etc/passwd file,the user and user info fileds, to a another file.I've tried this: cut -d ':' -f1 ':' -f6 < file but cut can be used to extract olny one field and not two. maybe with awk is this possible? (4 Replies)
Discussion started by: strawhatluffy
4 Replies

7. Shell Programming and Scripting

Extract 2 or more int from the text with delimiter.

Hi All, I want to extract the integers from the each line, each line may have 2 or more integers. The following command is appending each integers. echo "Hi I am 100, my friend is 500, his friend is 423" | sed "s///g" 100500423 I need to have delimiter "|" between the integers. If anyone... (18 Replies)
Discussion started by: sarwan
18 Replies

8. Shell Programming and Scripting

Adding a delimiter to a text file

Im writing a KSH script to read a simple text file and add a delimiter. Ive written the following script but it runs very slow. I initially used the cut command to substring the input record then switched to this version using awk to substring... both run too slow. Any ideas how to make this more... (2 Replies)
Discussion started by: lock
2 Replies

9. UNIX for Dummies Questions & Answers

Extract some common fields from 1 file that are presnt in another file

I have 2 files FILEA 720646363*PHILIPPINES 117183970*USA 116274291*USA 107940983*USA 107395824*USA 106632425*USA 105861926*USA 105208607*USA 053077046*USA 065428026*ENGLAND FILEB 001125236 001408905 002316511 002521094 020050725 035018308 052288735 (1 Reply)
Discussion started by: unxusr123
1 Replies

10. Shell Programming and Scripting

Formatting a text file based on newline and delimiter characters

Hi Everybody, I need some help on formatting the files coming into unix box on the fly. I get a file some thing like this in a single line. ISA^M00^M ^M00^M ^M14^M006929681900 ^M01^M095449419 ... (5 Replies)
Discussion started by: ntekupal
5 Replies
Login or Register to Ask a Question