select particular column and creat a new file with comma


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting select particular column and creat a new file with comma
# 1  
Old 04-29-2009
select particular column and creat a new file with comma

Hi all,
I am very new in programming.
Can anyone please help me in the matter below?

I have one raw file like:
gi|77|ref|NC_002971.3| Coxiella burnetii RSA 493, complete genome 6371 ATCGTGGT[GC]TG[AG]TGGTTCAT 5032 P 2 12
gi|71|ref|NC_005773.3| Pseudomonas syringae pv. phaseolicola 1448A, complete genome 6280 ATC[AG]G[CG]TTTCAT 5032 P 2 12
gi|19|ref|NW_002063157.1| Streptomyces pristinaespiralis ATCC 25486 supercont1.24, whole genome shotgun sequence 47242 CACC[TG]CAGGACCG 6071 P 1 4
gi|17|ref|NW_002062785.1| Streptomyces sp. Mg1 supercont1.3, whole genome shotgun sequence 249645 CACC[TG]GACCG 6071 P 1 4
gi|32|ref|NC_003888.3| Streptomyces coelicolor A3(2), complete genome 139563 CACCC[GC] 6071 P 1 4

gi is the starting of a new line.

I need one out put like :
Coxiella burnetii RSA 493, 5032, 50
Pseudomonas syringae pv. phaseolic, 5032, 50
Streptomyces pristinaespiralis ATC, 6071, 50
Streptomyces sp. Mg1 supercont1.3, 6071, 50
Streptomyces coelicolor A3(2), 6071,

where
1st column is 5th column of raw file
2nd column is 9th column of raw file
last column 50 is an arbitary number added as a column.

My main problem is there are several separators as: pipe, comma, space, tab in the file. ore over the names like 'Coxiella burnetii RSA 493' and descriptors 'complete genome ' should be treated as one column.

Can any body help me?
Thanks in advance,
Mitra.
# 2  
Old 04-29-2009
nawk -f ia.awk myRawFile

ia.awk:
Code:
BEGIN {
  SEP=FS
  FS="|"
  OFS=","

  arb=50
}
{
  f1=substr($NF,2,index($NF, ",")-2)
  n=split($NF, tmp, SEP)
  f2=tmp[n-3]
  print f1, f2, arb
}

In the future please use the BB code tags when posting code/data samples.
# 3  
Old 04-29-2009
Try this

Code:
awk -F "," '{match($1,/[|] [A-Z]+/);n=split($2,a," ");print substr($1,RSTART+2,length($1)-(RSTART+1)),a[n-3],50}' OFS="," filename


cheers,
Devaraj Takhellambam
# 4  
Old 05-02-2009
Thank you very much.

Thank you very much. It works...
Thanks a lot...
Best,
Mitra.Smilie
# 5  
Old 05-05-2009
Code:
open $fh,"<","a.txt";
while(<$fh>){
	chomp;
	my @tmp=split("[||,]",$_);
	my @t=split(" ",$tmp[5]);
	print $tmp[4],",",$t[$#t-3],",50\n";
}
close $fh;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bring values in the second column into single line (comma sep) for uniq value in the first column

I want to bring values in the second column into single line for uniq value in the first column. My input jvm01, Web 2.0 Feature Pack Library jvm01, IBM WebSphere JAX-RS jvm01, Custom01 Shared Library jvm02, Web 2.0 Feature Pack Library jvm02, IBM WebSphere JAX-RS jvm03, Web 2.0 Feature... (10 Replies)
Discussion started by: kchinnam
10 Replies

2. Shell Programming and Scripting

How to use regex on particular column (Removing comma from particular column)?

Hi, I have pipe separated file which contains some data having comma(,) in it. I want to remove the comma(,) only from particular column without changing data in other columns. Below is the sample data file, I want to remove the comma(,) only from 5th column. $ cat file1 ABC | DEF, HIJ|... (6 Replies)
Discussion started by: Prathmesh
6 Replies

3. Shell Programming and Scripting

Select distinct rows in a file by last column

Hi, I have the following file: LOG:015608::ERR:2310:map_spsrec:Invalid parameter LOG:015608::ERR:2471:map_dgdrec:Invalid parameter LOG:015608::ERR:2487:map_nnmrec:Invalid number LOG:015608::ERR:2310:map_nmrec:Invalid number LOG:015608::ERR:2438:map_nmrec:Invalid number As a delimiter I... (2 Replies)
Discussion started by: apenkov
2 Replies

4. Shell Programming and Scripting

Pick the column value including comma from csv file using awk

Source 1 column1 column2 column 3 column4 1,ganesh,1,000,1 222,ram,2,000,5 222,ram,50,000,5 33,raju,5,000,7 33,raju,5,000,7 33,raju,5,000,8 33,raju,5,000,4 33,raju,5,000,1 In my .csv file, third column is having price value with comma (20,300), it has to be considered 1,000 as... (1 Reply)
Discussion started by: Ganesh L
1 Replies

5. Shell Programming and Scripting

Count number of column in a comma delimited file

I have a comma (,) delimited file. 106232145,"medicare","medicare,medicaid",789 I would like to count the number of fields in each line. I tried the below code awk -F ',' '{print NF-1}' This returns me the result as 5 instead of 4. This is because the awk takes... (9 Replies)
Discussion started by: machomaddy
9 Replies

6. Shell Programming and Scripting

rearrange the column names with comma as column delimiter

Hi, I am new to shell scripting, i have requirement can any one help me out in this regrads, in directory i have file like invoice1.txt, invoice2.txt in each file i have fixed number of columns, 62 in number but they are randomly arranged.like for first file invoice1.txt can have columns... (5 Replies)
Discussion started by: madhav62
5 Replies

7. UNIX for Dummies Questions & Answers

select first column of file

Hi, Following is my file output 247 Sleep 25439 NULL 259 Sleep 25460 NULL 277 Sleep 15274 NULL 361 Sleep 2 NULL 362 Sleep 202 NULL I want to select only first column to other file How can... (2 Replies)
Discussion started by: kaushik02018
2 Replies

8. Shell Programming and Scripting

Convert comma text file to Column in html format

I am trying to generate a report with below file : File1 : EQADM,edrtere9-phys,8122caef0,gpatmon,/bin/ksh,nuten Erick EQADM,edrtere11-phys,8227caef0,gpatmon,/bin/ksh,nuten Erick EQADM,edrtere3-phys,822caef0,gpatmon,/bin/ksh,nuten Erick can you help me convert it to html and add... (9 Replies)
Discussion started by: sriram003
9 Replies

9. Shell Programming and Scripting

how to convert the result of the select query to comma seperated data - urgent pls

how to convert the result of the select query to comma seperated data and put in a .csv file using korn shell. Pls help me as its very urgent. Thanks, Hema. (1 Reply)
Discussion started by: Hemamalini
1 Replies

10. AIX

Creat a File with a Specific Size

Hi everybody, Is there a command that can create a new file with a specific size? Thanks in advance. (3 Replies)
Discussion started by: aldowsary
3 Replies
Login or Register to Ask a Question