How to read the first column in a flat file with ~ as delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read the first column in a flat file with ~ as delimiter
# 1  
Old 11-04-2009
How to read the first column in a flat file with ~ as delimiter

I have one flat file like below

id1~col~batch1
id2~col2~batch2
id3~col3~batch3

I need to read the first column one by one and I need to write one db2 query based on that column1
Like
for (i=0;i<=10;i++)
do
insert into table column (con_id) values (select column from table where id=$i)
done<input_file

Here i should be id1,id2 & id3 etc...
something like this..
Can you please help me in reading the first column of the flat file and how can I use that value in the query.

Thanks in advance.
# 2  
Old 11-04-2009
try this
Code:
cut -d'~' -f1 file1

# 3  
Old 11-04-2009
By awk:

Code:
awk -F"~" '{print $1}' urfile

# 4  
Old 11-04-2009
Code:
while IFS='~' read i x; do
  echo "insert into table column (con_id) values (select column from table where id=$i)"
done<infile

Code:
insert into table column (con_id) values (select column from table where id=id1)
insert into table column (con_id) values (select column from table where id=id2)
insert into table column (con_id) values (select column from table where id=id3)


Last edited by Scrutinizer; 11-04-2009 at 07:23 AM..
# 5  
Old 11-04-2009
Ok, by awk:

Code:
$ awk -F"~" '{print "insert into table column (con_id) values (select column from table where id="$1")"}' urfile
insert into table column (con_id) values (select column from table where id=id1)
insert into table column (con_id) values (select column from table where id=id2)
insert into table column (con_id) values (select column from table where id=id3)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read from Multiple character delimited flat file

Hello Guys I have a multiple character delimited flat file "|~|". when I tried to read the data the "|" character also coming Example I/P file 9882590|~|20111207|~|K03501000063005574033961|~|K|~| Command to get the second column I used awk -F"|~|" ' {print $2}' ... (2 Replies)
Discussion started by: Pratik4891
2 Replies

2. Shell Programming and Scripting

perl: Read array from a flat file

Hello Guru's I want to read an array into a flatfile Please let me know how to do the same So far this the below code use strict; use warnings; open (my $data , '<', $ARGV)|| die "could not open $ARGV:\n$!"; my @array=(<$data>); my @sorted=sort... (8 Replies)
Discussion started by: Pratik4891
8 Replies

3. Shell Programming and Scripting

Read flat file upto certain number of columns

Hello Guys Please help me with the below issue I want to read a flat file source upto certain number of columns Say my flat file has 30 columns but I want to read upto 25 columns only How come the above issue can be addressed? Thanks a lot!!!! (1 Reply)
Discussion started by: Pratik4891
1 Replies

4. Shell Programming and Scripting

Summation of column value in flat file

Hello Guys Please find my below requirement I have a flat file with column headers in first line and data The structure like below col1 col2 col3 A 1 2 B 3 4 C 5 6 Say I have to take the summation of col2 (that will depend on the... (2 Replies)
Discussion started by: Pratik4891
2 Replies

5. Shell Programming and Scripting

To read a flat file containing XML data

I have a file something like this:aaaa.xml content of the file is 0,<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <storeInformation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <s> <BRANCH_NO>3061</BRANCH_NO> <BRANCH_NAME>GREEN EXPRESS</BRANCH_NAME> ... (4 Replies)
Discussion started by: kmanivan82
4 Replies

6. UNIX for Advanced & Expert Users

Insert Delimiter at fixed locations in a flat file

Hi Can somebody help me with solution for this PLEASE? I have a flat file and need to insert delimiters at fixed positions in all the lines so that I can easily convert into EXCEL with columns defined as per their width. For Example Here is the file { kkjhdhal sdfewss sdtereetyw... (7 Replies)
Discussion started by: jd_mca
7 Replies

7. Shell Programming and Scripting

how to read fixed length flat file....

Hi Gurus, Thanks in advance... I am new to writing shell scripting and help me out reading a flat file with fixed length. I have a fixed length flat file with storename(lenth 6) , emailaddress(lenth 15), location(10). There is NO delimiters in that file. Like the following str00001.txt... (2 Replies)
Discussion started by: willywilly
2 Replies

8. Shell Programming and Scripting

Flat File column manipulation

Hi All, I have a tab delimited input file with say 4 fields (columns) as below : 0000443 1AGPR061 2006 Daiml 0002198 1B3XG0K2 1989 Chdds 0002199 1Bd64J0L 1990 Ch34s 0002275 1B3s4J0K 1989 Chadys 0002276 1B465302 2002 Dageml 0002290 1B45430K 1989 Cays I want the 2nd column in file to... (5 Replies)
Discussion started by: net
5 Replies

9. Shell Programming and Scripting

How to read lines from a flat file

i have some commands written line by line in one flat file i have to read each linefrom this file(file name passed as parameter) and then i have to execute this How can i do it? (2 Replies)
Discussion started by: bihani4u
2 Replies

10. Shell Programming and Scripting

Look up column in a flat file

Here is on more go ! Need a shortcut for my problem ! problem is i have a look_update with fixed sequence of column that is : MANDT:SERAIL:SERSCHA:SEREX:EQTYP:BSTVP I will be getting data in a flat file having same number of column but the sequence could be different in each... (5 Replies)
Discussion started by: jambesh
5 Replies
Login or Register to Ask a Question