LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-20-2013, 02:25 AM   #1
kamesp
LQ Newbie
 
Registered: Jun 2012
Posts: 10

Rep: Reputation: Disabled
Converting .txt file to a csv file


Hi,

I have a .txt file with the output of a ps command as mentioned below

Apr 20 12:48:57 7590 16368 1726900 0.4 0.4
Apr 20 12:48:57 7590 16368 1726900 0.4 0.4
Apr 20 12:48:57 7590 16368 1726900 0.4 0.4
Apr 20 12:48:57 7590 16368 1726900 0.4 0.4

I have such ouptput for every 2 sec.My file will have such instances in a huge number.

I want to convert this .txt file to a csv file which i can analyze easily.

Can you please let me kknow if there is a shell script or tcl or perl script with which i can do this.
 
Old 04-20-2013, 02:37 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
just pipe it through awk.... pretty simple stuff...

Code:
# echo Apr 20 12:48:57 7590 16368 1726900 0.4 0.4 | awk '{print $1" "$2" "$3","$4","$5","$6","$7","$8}'
Apr 20 12:48:57,7590,16368,1726900,0.4,0.4
or if you want to date parts tokenized it's even easier...

Code:
# echo Apr 20 12:48:57 7590 16368 1726900 0.4 0.4 | tr ' ' ','
Apr,20,12:48:57,7590,16368,1726900,0.4,0.4

Last edited by acid_kewpie; 04-20-2013 at 02:38 AM.
 
Old 04-20-2013, 03:22 AM   #3
kamesp
LQ Newbie
 
Registered: Jun 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Hi,

Thanks for the reply.

I have a text file which has thousands of lines. How can i get a .csv file diectly ? How i need to use the command.

Do i need o write a seperate script giving this as an input file ?
 
Old 04-20-2013, 04:20 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Code:
awk '{print $1" "$2" "$3","$4","$5","$6","$7","$8}' name.of.text.file > new.file.name.csv
The above uses name.of.text.file as input (your file with thousands of lines) and puts (redirects) the output to a file called new.file.name.csv.

Do _not_ use the same name for both input and output file!
 
Old 04-20-2013, 04:27 AM   #5
fredak
Member
 
Registered: Apr 2013
Location: Montgeron near Paris
Distribution: Slackware
Posts: 32

Rep: Reputation: Disabled
Hi Kamesp

with the pipe character "|" you can take the output given by what is on the left of the pipe

and do whatever you want with this output (with what is on the right of the pipe)

so instead of the echo ... put cat

# cat my_file | awk '{print $1" "$2" "$3","$4","$5","$6","$7","$8}'

= I display every line of my file and for each line I do an awk

and then don't forget to redirect in another file with >

tell me if it worked
 
Old 04-20-2013, 04:30 AM   #6
fredak
Member
 
Registered: Apr 2013
Location: Montgeron near Paris
Distribution: Slackware
Posts: 32

Rep: Reputation: Disabled
woooops I didn't see druuna's reply

more simple indeed ( sorry I ain't a unix guru....yet)
 
Old 04-20-2013, 06:06 AM   #7
kamesp
LQ Newbie
 
Registered: Jun 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Hi,

Thanks for the reply and it is working fine.

I have a question. In the awk command you are using awk '{print $1" "$2" "$3","$4","$5","$6","$7","$8}' where there is no " before $1 and similarly after $8. As i know awk prints the columns specififed.Can you tell me the logic here.
 
Old 04-20-2013, 06:12 AM   #8
kamesp
LQ Newbie
 
Registered: Jun 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Hi,

One more issue is that when i copy the .csv file to my windows PC and open the same i am getting the details as below

4/19/2013 19:12 9135 12632 422804 0.3 0.1
4/19/2013 19:12 9135 12632 422804 0.3 0.1
4/19/2013 19:12 9135 12632 422804 0.3 0.1
4/19/2013 19:12 9135 12632 422804 0.3 0.1
4/19/2013 19:12 9135 12632 422804 0.3 0.1
4/19/2013 19:12 9135 12632 422804 0.3 0.1
4/19/2013 19:12 9135 12632 422804 0.3 0.1
4/19/2013 19:12 9135 12632 422804 0.3 0.1
4/19/2013 19:12 9135 12632 422804 0.3 0.1
4/19/2013 19:12 9135 12632 422804 0.3 0.1
4/19/2013 19:12 9135 12632 422804 0.3 0.1
4/19/2013 19:12 9135 12632 422804 0.3 0.1
4/19/2013 19:12 9135 12632 422804 0.3 0.1
4/19/2013 19:12 9135 12632 422804 0.3 0.1

Where i am getting the timestamp somany times eventhough it was printed only once when i see in my linux machine.

PS : Just to give a brief.I am running ps command with arguments in a loop with a gap of 2 sec
 
Old 04-20-2013, 07:12 AM   #9
fredak
Member
 
Registered: Apr 2013
Location: Montgeron near Paris
Distribution: Slackware
Posts: 32

Rep: Reputation: Disabled
Quote:
Originally Posted by kamesp View Post
Hi,

Thanks for the reply and it is working fine.

I have a question. In the awk command you are using awk '{print $1" "$2" "$3","$4","$5","$6","$7","$8}' where there is no " before $1 and similarly after $8. As i know awk prints the columns specififed.Can you tell me the logic here.
the " encloses the blank or the comma not the $n

but I don't know why there isn't any coma between $1 and $2 and $3
 
Old 04-20-2013, 07:45 AM   #10
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by kamesp View Post
Where i am getting the timestamp somany times eventhough it was printed only once when i see in my linux machine.

PS : Just to give a brief.I am running ps command with arguments in a loop with a gap of 2 sec
You have a line every two seconds. Your Excel format shows the day, hour and minute, but not second. So you should expect the same time stamp to appear on 30 lines before going to the next time stamp.

Is that what you mean by "so many times"? What do you mean by "printed only once"?

I think you are getting confused by the fact that Excel recognizes the input as a date and time, but does not pay attention to the exact format of that date and time. So it displays it in the default format for date and time. It has stored all the details that you don't see. It just isn't displaying them.

You can click the top of the first column (to select the whole column), then right click there and select format cells, then select a different date and time format. Excel does support the same format as your original date and time. It just doesn't select that by default.

Quote:
Originally Posted by fredak View Post
I don't know why there isn't any coma between $1 and $2 and $3
acid_kewpie guessed that the time stamp (month day time) was desired as one field, not three. So most spaces in the input delimit fields and must be replaced by "," but the first two spaces are included in the first field (what awk sees as the first three fields is what Excel should see as the first single field).

Last edited by johnsfine; 04-20-2013 at 07:56 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Parsing text file with passwords and converting to CSV list Jumpingmushroom Programming 10 04-15-2013 03:11 AM
[SOLVED] How to script csv editing? Remove rows from csv file that do not contain certain text ingram87 Linux - Software 9 08-03-2012 12:45 PM
Create 1 csv file from multiple txt files richmur Programming 10 09-03-2008 01:28 PM
Comparing two csv files and write different record in third CSV file irfanb146 Linux - Newbie 3 06-30-2008 09:15 PM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration