Copying of multiple columns of one table to another by mapping with particular strings.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying of multiple columns of one table to another by mapping with particular strings.
# 1  
Old 04-11-2011
Bug Copying of multiple columns of one table to another by mapping with particular strings.

Hi,

I would like to copy some columns from a particular file by mapping with the string names. i am using the .csv file format.
my one file consist of 100 of columns but i want only particular 4 columns such as ( First_name, Middle_name,Last_name & Stlc). but they are listed in many files randomly in different places. so i want a script where my input file should be the file name and will map with the four particular string names and will give output in another file name with the same file name with extension _format. So here i also want to use my file as a parameter file, because i am having many files to check this and want to keep the output file saved with the input_format file name.

Any kind of help will be appreciated.
# 2  
Old 04-11-2011
Could you post sample input file and the desired output format..?
# 3  
Old 04-11-2011
Sure i will.
Here is my input file let say file1.csv
Code:
STBD_KEY     ,STLC_STATE,     STLC_TYPE     ,FIRST_NAME,      MIDDLE_NAME,      LAST_NAME,       STLC 
IDPA,               PA-612               ID,             PA, SHARON, MATHEW,THUESON                       ,PA-612
 IDPA              ,PA-218               ID,             PA, ZABRINA,                      YVETTE,                          THURMAN,                      PA-218                            
IDPA               ,PA-219 ID,             PA, GALE,                 CYNTHIA,                        TINKER,                          PA-219

And i want the output file like this:
Code:
     FIRST_NAME,                MIDDLE_NAME,                LAST_NAME,                 STLC 
SHARON, MATHEW,                                                                                                  THUESON ,PA-612
 ZABRINA,                         YVETTE,                                          THURMAN,                       PA-2 18        
GALE,                                        CYNTHIA,                                          TINKER,                            PA-219             
DAVID,                                WAYNE,                                              TOMEY,                              PA-297

And one more thing i want to do with it is to pass the input file as a parameter file because i am having many file in which i have to do the same work, and and i want that my output file should have the name with extension of input file let say file1_new.csv so that i can have a file of every file and it not get overwrite the previous data if i will change the input file name. And one more thing i will make you clear that the column numbers or places changes with every file, it will not remain in the same format they are in random way in the table.\

Hope i am more clear to make you understand what i wanted.

Last edited by Franklin52; 04-12-2011 at 03:28 AM.. Reason: Please use code tags
# 4  
Old 04-11-2011
There is a problem when some names do not have middle names, e.g. Sharon Thueson. Where is the empty column? Without the empty column it is not possible to correctly associate the data fields with the headers.

Also, you seem to have 8 data fields but only 7 header fields.

Perhaps you could export the table with comma delimiters?

Last edited by kato; 04-11-2011 at 09:33 AM..
# 5  
Old 04-11-2011
i am sorry it was mistake, i collected the data from a file where data is not totally correct, and yes the 1st row is missing its middle name, but the format which i have written is exactly the same way the original file is with all the cells having data.
so i hope now you can help me.

---------- Post updated at 06:10 PM ---------- Previous update was at 06:03 PM ----------

Hey Kato you can now go through the question i edited it and make it comma delimiters.
# 6  
Old 04-11-2011
ok, a bit long but it should work:

Code:
$cat names.awk
BEGIN{FS=","}
NR==1 {
    for(i=1;i<=NF;i++) {
        if($i~/NAME/ || $i=="STLC") {
            cols[i]=i
            head = head==""? $i : head","$i
        }
    }   
    print head
    next
}   
{
    for (item in cols) {
        row = row==""? $item : row","$item
    }
    print row
    row = ""
} 
$ awk -f names.awk file
FIRST_NAME,MIDDLE_NAME,LAST_NAME,STLC
SHARON,,THUESON,PA-612
ZABRINA,YVETTE,THURMAN,PA-218
GALE,CYNTHIA,TINKER,PA-219

# 7  
Old 04-12-2011
Thanks kato for your code but sorry to say but it is giving error.
the error is :
$cat names.awk having a syntax error
and if possible can you explain the code in short.

Last edited by dsh007; 04-12-2011 at 03:55 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mapping the values of ids of two columns of file1 from file2

I have of two space separated files: ==> File1 <== PT|np_496075.1 st|K92748.1 st|K89648.1 PT|np_001300561.1 PT|np_497284.1 st|K90752.1 st|K90279.1 PT|np_740775.1 PT|np_497749.1 st|K90752.1 st|K92038.1 PT|np_490856.1 PT|np_497284.1 st|K90752.1 st|K88095.1 PT|np_494764.1 ==> File 2 <==... (2 Replies)
Discussion started by: sammy777888
2 Replies

2. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

3. Programming

awk to count occurrence of strings and loop for multiple columns

Hi all, If i would like to process a file input as below: col1 col2 col3 ...col100 1 A C E A ... 3 D E G A 5 T T A A 6 D C A G how can i perform a for loop to count the occurences of letters in each column? (just like uniq -c ) in every column. on top of that, i would also like... (8 Replies)
Discussion started by: iling14
8 Replies

4. UNIX and Linux Applications

Help in copying table structure to another table with constraints in Oracle

hi, i need to copy one table with data into another table, right now am using create table table1 as select * from table2 i want the constraints of table1 to be copied to table2 also , can anyone give me some solution to copy the constraints also, now am using oracle 10.2.0.3.0... (1 Reply)
Discussion started by: senkerth
1 Replies

5. Shell Programming and Scripting

Merge left hand strings mapping to different right hand strings

Hello, I am working on an Urdu to Hindi dictionary which has the following structure: a=b a=c n=d n=q and so on. i.e. Headword separated from gloss by a = I am giving below a live sample بتا=बता بتا=बित्ता بتا=बुत्ता بتان=बतान بتان=बितान بتانا=बिताना I need the following... (3 Replies)
Discussion started by: gimley
3 Replies

6. Shell Programming and Scripting

Copying lines between two strings

Hello All, I want to copy some lines from one file to other with following condition. Only lines between two specified strings should copy. Example :- "My First String " Some_Other_String .... Some_Other_String .... Some_Other_String .... "My Second String" So only... (5 Replies)
Discussion started by: anand.shah
5 Replies

7. Shell Programming and Scripting

Creating unique mapping from multiple mapping

Hello, I do not know if this is the right title to use. I have a large dictionary database which has the following structure: where a b c d e are in English and p q r s t are in a target language., the two separated by the delimiter =. What I am looking for is a perl script which will take... (5 Replies)
Discussion started by: gimley
5 Replies

8. Shell Programming and Scripting

Numbering, copying and replacing strings

hello everybody there, I'm a new bash shell programmer and I'm dealing with a problem. To start, I have a file with a number of string lines which may contain a particular string or not. I have to write a code that identifies the line containing one particular string and keeps it, but also writes... (10 Replies)
Discussion started by: leaf
10 Replies

9. Shell Programming and Scripting

Copying and pasting columns from different files

hi all, first time posting, hoping i can get some help on this. I have about 80 text files containing text in this format: # Rg Mass Density Rcm-x Rcm-y Area Rsph/sqrt(2) # == ==== ======= ===== ===== ==== ... (9 Replies)
Discussion started by: Arlamos
9 Replies

10. Shell Programming and Scripting

copying strings...

hey i want to know the unix command for copying 1 string to another and allso adding a third string to the result. for eg: a b="nbno" c="uioio" i want to copy contents of b to a and the append the contents of c to the contents of a and the result shud be in string a (1 Reply)
Discussion started by: priya_9patil
1 Replies
Login or Register to Ask a Question