Converting text file in a matrix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting text file in a matrix
# 1  
Old 12-13-2012
Converting text file in a matrix

Hi All,

I do have a file with many lines (rows) and it is space delimited. For example: I have a file named SR345_pl.txt. If I open it in an editor, it looks like this:

Code:
adfr A2 0.9345
dtgr/2 A2 0.876
fgh/3 A2 023.76
fghe/4 A2 2345
bnhy/1 A3 3456
bhy A3 0.9876
phy A5 0.987
kdrt A5 0.985
kdfg A7 0.345
klp A9 0.4567

The output I want is in a single line with the part of the name of the file as the first column and then count the number of A2's, A3's, A5's and all. The characters or values in column 2 of the files varies from A1 to A10. Basically counts of number of A1's, A2's.....upto A10.

this is what I want as output
Code:
file name      A1  A2 A3 A4 A5 A6 A7 A8 A9 A10
SR345_pl.txt  0   3    2   0   2   0   1  0  1    0

Let me know the best way to do it using awk. A tab delimited output file will be good
# 2  
Old 12-13-2012
try:
Code:
awk '
{a[$2]=$2; c[$2]++}
END {
  printf "file name\t";
  for (i=0; i<=11; i++) printf i==11?"\n":"A" i "\t";
  printf FILENAME"\t";
  for (i=0; i<=11; i++) printf i==11?"\n":(c["A" i]?c["A" i]:0) "\t";
}' SR345_pl.txt

# 3  
Old 12-14-2012
I am getting a syntax error in line 5
Code:
awk: syntax error at source line 5
 context is
	  for (i=0; i<=11; i++) printf >>>  i== <<< 
awk: illegal statement at source line 5
awk: illegal statement at source line 5

# 4  
Old 12-14-2012
Use nawk on solaris.
# 5  
Old 12-14-2012
don't have access to solaris
# 6  
Old 12-14-2012
Not what I mean.

Some systems have a limited version of awk by default, and must use nawk to have certain features. Since the program didn't work I guessed you were on solaris.

What's your system?
# 7  
Old 12-14-2012
mac and a linux
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Converting text file to html page

Hello Everyone, I have the following text file with the each field separated by newline Text file P file1-en-us_US-20170718T150157Z.json Wed 19 Jul 2017 06:10:13 AM EDT P file2-en-us_US-20170718T160150Z.json Wed 19 Jul 2017 06:10:13 AM EDT P file3-en-us_US-20170718T163218Z.json Wed... (9 Replies)
Discussion started by: nextStep
9 Replies

2. Shell Programming and Scripting

Converting data for text file to csv

Gents Using the script attached (raw2csv). i use to create the file .csv.. The input file is called 201.raw. Kindly can you check if there is easy way to do it. The script works fine but takes a lot time to process Thanks for your help (8 Replies)
Discussion started by: jiam912
8 Replies

3. Shell Programming and Scripting

[Solved] Converting the data into matrix with 0's and 1's

I have a file that contains 2 columns tag,pos cat input_file tag pos atg 10 ata 16 agt 15 agg 19 atg 17 agg 14 I have used following command to sort the file based on second column sort -k 2 input_file tag pos atg 10 agg 14 agt 15 ata 16 agg 19 atg 17 (2 Replies)
Discussion started by: raj_k
2 Replies

4. Shell Programming and Scripting

Format problem while converting text file to csv

Hi , I need a help in following scenario.I tried searching in google but couldn't able to find the exact answer. Sorry if i am re-posting already answered query. While i am trying to convert into log file into csv i couldn't able to get the format which i am looking for. I converted file... (4 Replies)
Discussion started by: varmas424
4 Replies

5. Programming

Converting columns to matrix

Dear All I would like to convert columns to matrix For example my data looks like this D2 0 D2 0 1.0 D2 0 D2 1 0.308 D2 0 D2 2 0.554 D2 0 D2 3 0.287 D2 0 D2 4 0.633 D2 0 D2 5 0.341 D2 0 D2 6 0.665 D2 0 D2 7 0.698 D2 0 D2 8 0.625 D2 0 D2 9 0.429 D2 0 D2 10 0.698 D2 0 D2 11... (7 Replies)
Discussion started by: bala06
7 Replies

6. Shell Programming and Scripting

Converting to matrix-like file using AWK

Hi, Needs for statistics, doing converting Here is a sample file Input : 1|A|17,94 1|B|22,59 1|C|56,93 2|A|63,71 2|C|23,92 5|B|19,49 5|C|67,58 expecting something like that Output : 1|A|17,94|B|22,59|C|56,93 2|A|63,71|B|0|C|23,92 5|A|0|B|19,49|C|67,58 (11 Replies)
Discussion started by: fastlane3000
11 Replies

7. UNIX for Dummies Questions & Answers

Converting a text file with irregular spacing into a space delimited text file?

I have a text file with irregular spacing between values which makes it really difficult to manipulate. Is there an easy way to convert it into a space delimited text file so that all the spaces, double spaces, triple spaces, tabs between numbers are converted into spaces. The file looks like this:... (5 Replies)
Discussion started by: evelibertine
5 Replies

8. Shell Programming and Scripting

Converting columns of a text file.

Not sure the most effient way to do this. I have figiured out how to extract columns with shell script, but not sure how to convert This is what I have... NEWDNS 04-Jun-2011 06:00:59.762 10.220.136.217 crl.verisign.com This is what I need.... Change date, remove mil seconds,... (1 Reply)
Discussion started by: mrlayance
1 Replies

9. Shell Programming and Scripting

Sybase Interface file and converting in text format.

Does anyone knows how to decode the address in interface file using shell , i have done it using perl but can it be done in shell. master tli tcp /dev/tcp \x00021004ac1414230000000000000000 query tli tcp /dev/tcp \x00021004ac1414230000000000000000 (0 Replies)
Discussion started by: dinjo_jo
0 Replies

10. Shell Programming and Scripting

Converting a text file to HTML

Hi, I need to convert a text file formatted like this ("tshark -z conv,ip" output) to HTML: ===================================================================================================== IPv4 Conversations Filter:<No Filter> | <- ... (4 Replies)
Discussion started by: ph0enix
4 Replies
Login or Register to Ask a Question