Converting txt file in csv


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting txt file in csv
# 1  
Old 07-30-2010
Converting txt file in csv

HI All,


I have a text file memory.txt which has following values.


Code:
   Average:       822387   7346605     89.93    288845   4176593   2044589     51883      2.47      7600

i want to convert this file in csv format and i am using following command to do it.


Code:
  sed s/_/\./g < memory.txt | awk -F. '{ print $1","$2","$3","$4}' > test.csv

but I am getting output in just 3 columns. I want every value to be printed in new column.

I have also used this one but result is not accurate.

Code:
awk -F_ '{ print $1","$2","$3}' < memory.txt >  MemoryTemp.csv

plz tell me where i am doing wrong. i got this code from google.

Thanks in advance.

Last edited by zaxxon; 07-30-2010 at 05:35 AM.. Reason: added 1 more pair of missing code tags
# 2  
Old 07-30-2010
Since the 1st awk code just handles only first 3 fields you only get first 3 fields. There is also a substitution of underscores where there aren't any. So a following underscore as field separator is not working either. This code does not fit to your question at all. You can try this one - I've chosen the semicolon as separator:

Code:
$> sed 's/[[:space:]]\+/;/g' memory.txt > memory.csv
$> cat memory.csv
Average:;822387;7346605;89.93;288845;4176593;2044589;51883;2.47;7600

# 3  
Old 07-30-2010
Hi,
Try this,
Code:
tr -s ' ' ',' < inputfile

# 4  
Old 07-30-2010
Hi,

zaxxon: by choosing , (comma) as a separator, i got the required results. so thanks for your input.

Code:
sed 's/[[:space:]]\+/,/g' memory.txt > memory.csv

pravin27: your code is also working so thanks for your efforts.

Regards,
Kashif.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Data extraction and converting into .csv file.

Hi All, I have a data file and need to extract and convert it into csv format: 1) Read and extract the line containing string ending with "----" (file sample_linebyline.txt file) and to make a .csv file from this. 2) To read the flat file flatfile_sample.txt which consists of similar data (... (9 Replies)
Discussion started by: abhi_123
9 Replies

2. Shell Programming and Scripting

Converting txt file into CSV using awk or sed

Hello folks I have a txt file of information about journal articles from different fields. I need to convert this information into a format that is easier for computers to manipulate for some research that I'm doing on how articles are cited. The file has some header information and then details... (8 Replies)
Discussion started by: ksk
8 Replies

3. Shell Programming and Scripting

Converting txt file to Excel file

Hi All, I have a text file with below content. TradeDate Name SecurityMnc ReasonDesc ======================================= 20120501 Robin ABC FO System Defect 20120502 Robin ABC FO System Defect I would want this in an excel file in 4 columns,... (3 Replies)
Discussion started by: robinbannis
3 Replies

4. Shell Programming and Scripting

need to save the space when converting to CSV file

Hi, I have a text file with the following format. Some of the fields are blank. 1234 3456 23 45464 327837283232 343434 5654353 34 34343 3434345 434242 .... .... .... I need to convert this file to a CSV file, like 1234, ,23, ... (3 Replies)
Discussion started by: wintersnow2011
3 Replies

5. Shell Programming and Scripting

txt file to CSV

hi.. I have a text file which looks likes this 2258 4569 1239 258 473 i need to convert it into comma seperated format eg:2258,4569,1239,258,437 pls help (8 Replies)
Discussion started by: born
8 Replies

6. UNIX for Dummies Questions & Answers

Converting txt file to csv file

Hi, Using rsync, I've sent the output to a text file. This is the text file : Pls help me on converting this text file to a csv file. Probably a script or sth to convert the text file to a csv file. (3 Replies)
Discussion started by: anaigini45
3 Replies

7. Shell Programming and Scripting

converting xls file to txt file and xls to csv

I need to convert an excel file into a text file and an excel file into a CSV file.. any code to do that is appreciated thanks (6 Replies)
Discussion started by: bandar007
6 Replies

8. UNIX for Advanced & Expert Users

converting a .txt file to comma delimeted file

Dear all, I have a file with 5L records. one of the record in the file is as shown below. MARIA THOMAS BASIL 1000 FM 1111 MD ... (1 Reply)
Discussion started by: OSD
1 Replies

9. Shell Programming and Scripting

converting .txt to comma delimeted file

Dear all, I have a file with 5L records. one of the record in the file is as shown below. MARIA THOMAS BASIL 1000 FM 1111 MD GHANA YY 77354 4774 99999999 1234567 I need to convert this record in below format "","","","","MARIA","THOMAS","BASIL","","1000 FM 1111 MD","STE... (1 Reply)
Discussion started by: OSD
1 Replies

10. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies
Login or Register to Ask a Question