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.
# 8  
Old 04-12-2011
Did you mean this?
Code:
awk -F, '{print $4","$5","$6","$7 > FILENAME".new"}' inputfile

# 9  
Old 04-12-2011
Use nawk instead of awk if your in Solaris machine. Below is a similar one to Kato's solution
Code:
for fil in `ls -1 *.csv`
do
	awk -F, 'NR==1{for(i=1;i<=NF;i++) if($i ~ /NAME/ || $i=="STLC"){c[i]++;!f?f=$i:f=f FS $i}print f;getline}{for(b in c) {!g?g=$b:g=g FS $b}print g;g=""}' $fil > ${fil}_new
done

# 10  
Old 04-12-2011
Thanks michaelrozar17 , but i am using bash, and one more thing i am not getting what does names.awk mean, it is giving a syntax error and can you explain your code if possible.

Last edited by dsh007; 04-12-2011 at 04:44 AM..
# 11  
Old 04-12-2011
According to post# 6, you need to save the given whole awk command as is in a file name called names.awk and run as
Code:
awk -f names.awk your_file.txt

Explanation:
1
-F, -- Since your file is csv file we keep the field separator to comma 
NR==1{for(i=1;i<=NF;i++) if($i ~ /NAME/ || $i=="STLC"){c[i]++;!f?f=$i:f=f FS $i}print f;getline} -- Read line number 1 and execute whatever is within {}. i.e 
if($i ~ /NAME/ || $i=="STLC") -- Check for each field matching NAME and STLC
{c[i]++; -- if matched store the column number in a array c[i]
!f?f=$i:f=f FS $i} -- Store the header in a variable f
print f;getline -- print the value of f and read the nextline
2
{for(b in c) {!g?g=$b:g=g FS $b} --again read the indexes of array c and store the values in a variable g
print g;g=""} -- come out of loop and print g and make var g to null

If your a newbie to awk then i guess you would not be able to understand much of the explanation. However practicing would help you.

Last edited by michaelrozar17; 04-12-2011 at 06:17 AM..
# 12  
Old 04-12-2011
Kato your code is working but it is not giving the STCL i.e the fourth column, it is giving the other three cloumns.
And Michaelrozar 1st of all thanks for the code and explanation, i understand what you want to explain and i tried your code also but sorry to say it is giving error in the line
for fil in `ls -1 *.csv`
and again it is giving error in do statement , they are having the same errors. And the main problem which i have mentioned earlier is that as i am having 100's of files to run on the same script so i want to save the file data with its extension name say file1._format.csv and when ever i will run another file it should not overwrite the previous data file, so i want to save each file data with its extension name, so i want to use the file name as parameter file inside the code, so can anyone provide code for this.

Last edited by dsh007; 04-12-2011 at 06:57 AM..
# 13  
Old 04-12-2011
Could you post the error..? The ls -1 (one) and not l (L) if your mistaken. And i guess you are aware that the given script (in post# 9) should be run in the location where you have those 100 *.csv files.
# 14  
Old 04-12-2011
yes i did not mistaken the ls -1, and i am aware of the code you have written that, i was saying the problem for the 7th code of Kato.
And the error i am getting is syntax error for both for and do.
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