use of format file to extract columns from a source file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting use of format file to extract columns from a source file
# 1  
Old 12-15-2008
use of format file to extract columns from a source file

hi experts

lets say my format file is B

B
=========

column no,name,type,length

1,ee,N,12
3,hj,N.4
4,kl,N,5


source file
========

d e f g h i
5 8 9 7 6 5
1 3 4 5 6 6
1 3 4 5 7 8

by using the format file i wanna columns of source file ...that means
i need to extract columns dynamically( 1,3, 5 )from source file..


i am working on AIX

i have tried the code below . but it is not working...

Code:
set -x
#!/bin/ksh
format_file=$1
source_file=$2
i=0
for col in `cat $format_file | awk 'BEGIN {FS=","} {print $1}'`
do
#col_val=`echo $col|cut -d"," -f0`
echo $col>> tmp.dat
done
#sed -iq 's/^/\$/g' tmp.dat
for ch in `cat tmp.dat`
do
echo -n $ch >> tmp_2.dat
done
i=0
for col_2 in `cat tmp_2.dat`
do
i=`expr $i + 1`
echo $col_2
#col2=$(echo $col_2|sed 's/\$/ \$/g')
col2=$(echo $col_2|sed 's/./& /g')
#col2=$col_2
echo $col2
awk -v var="$col2" '
BEGIN {n=split(var,a)}
{
  for(i=1;i<=n;i++) {
    printf "%s%s", $a[i], i==n?"\n":" "
  }
}' $source_file > tmp.csv
#awk '{print $'colmn'}' $source_file> tmp.csv
done
echo $i

can you please help me regarding the same?

a quick response is appreciable...


thanks,
subhendu

Last edited by vino; 12-15-2008 at 06:35 AM..
# 2  
Old 12-15-2008
Code:
awk -F "(,|[ \t]+)" '
NR == FNR {
  if (cnt[$1] ++ == 0)
     col[j++] = $1
}
NR != FNR {
  for (i = 0; i < j; i ++)
     printf ("%s%s", i ? " " : "", $(col[i]))
  printf ("\n")
}' your-format-file  your-data-file

output:
d f g
5 9 7
1 4 5
1 4 5

is this what you want?
# 3  
Old 12-16-2008
ya .....absolutely...
Thanks a lot.....

while comparing the output file with another file i am getting an error as below:

"0403-007 Generated or received a file descriptor number
that is not valid."

here is the code:

counter=1
exec 3< $new_required_file

while read -u3 line
do
grep "$line" $file1 1>/dev/null
(( counter += 1 ))
done

its showing error for the two lines:

exec 3< $new_required_file

while read -u3 line

Can you please help me in removing that error.

Thanks,
Subhendu
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match Columns in one file and extract columns from another file

Kindly help merging information from two files with the following data structure. I want to match for the CHR-SNP in Foo and get the columns that match from CHROM-rsID Fields 1 & 2 of foo may have duplicates, however, a joint key of Fields $1$2$3$4 is unique. Also would be helpful to clean up... (4 Replies)
Discussion started by: genehunter
4 Replies

2. Shell Programming and Scripting

Extract columns into seperate file

I have a comma delimited file as per the one below and I am currently extracting the values in 2 columns (COL1 & COL6) to produce a smaller trimmed down version of the file which only contains the columns we need; COL1,COL2,COL3,COL4,COL5,COL6,COL7,COL8,COL9... (1 Reply)
Discussion started by: Ads89
1 Replies

3. Shell Programming and Scripting

Extract the correct format of file Name

All, Objective : Extract the correct format of a file name. Please share the script command which will extract the correct format of the file after untarring. Example : If the file is of .csv format then extract filename.csv if the file is having .CSV then extract the same.CSV if the file... (1 Reply)
Discussion started by: Mahesh G
1 Replies

4. Shell Programming and Scripting

How to Split a source file in specified format?

Requirement: Need to split a source file say a1.txt which can be of size upto 150 MB into 25 target files each with a max size of 25 MB along with the header line in each target file. NOTE: Few target files can be empty also ,but 25 files must be generated for 1 source file( I can expect upto... (4 Replies)
Discussion started by: mad_man12
4 Replies

5. Shell Programming and Scripting

extracting columns using pattern file from source file

Hi experts,Please help me for the below requirement. i have a source file.(lets say contains 50 columns). I am extarcting five columns from the source file by using pattern file. for example input file:--------a,b,c,d,"a,g","v b",s,koutputfile=======a,"a,g","v b",s,kThanks in advance subhendu (2 Replies)
Discussion started by: subhendu81
2 Replies

6. Shell Programming and Scripting

fileutility to extract columns from source file

Hi experts,Please help me for the below requirement.i have a source file.(lets say contains 50 columns). I am extarcting five columns from the source file by using pattern file.for exampleinput file:--------a,b,c,d,"a,g","v b",s,koutputfile=======a,"a,g","v b",s,kThanks in advancesubhendu (1 Reply)
Discussion started by: subhendu81
1 Replies

7. Shell Programming and Scripting

Extract Columns from file

Hi All, Could you please help me with following: I have to parse a .csv file. For example: If the csv file contains 3 columns, then i have to print the column names. The field separator is (comma). example.csv (contains 2 lines as follows) This is,a test file, for validation... (2 Replies)
Discussion started by: vfrg
2 Replies

8. UNIX for Advanced & Expert Users

How to extract a particular file in tar.Z format

Hi , I need to extarct only a particulay files from the tar.Z. ie i need to extract one.txt from test.tar.Z. The test.tar.Z may contain lot of file and folders. Please help me to extract particular file to some location. Regards, Kalai. (1 Reply)
Discussion started by: kalpeer
1 Replies

9. Shell Programming and Scripting

Help, need to extract columns from file

I have huge fixed width, text file in unix box and I need to extract columns found between the width 105 and 200 and output it to a new file. Can anyone tell me how to extract it? Thanks for your help. (1 Reply)
Discussion started by: kiran2k
1 Replies

10. Shell Programming and Scripting

extract and format information from a file

Hi, Following is sample portion of the file; <JDBCConnectionPool DriverName="oracle.jdbc.OracleDriver" MaxCapacity="10" Name="MyApp_DevPool" PasswordEncrypted="{3DES}7tXFH69Xg1c=" Properties="user=MYAPP_ADMIN" ShrinkingEnabled="false" ... (12 Replies)
Discussion started by: sujoy101
12 Replies
Login or Register to Ask a Question