LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-05-2012, 07:08 AM   #1
arjun_b
LQ Newbie
 
Registered: Jan 2012
Posts: 1

Rep: Reputation: Disabled
Text file to Excel adding column


Hi,
I am new to linux.
I am currently doing a script to write text file to excel file.I have may text files with 2 columns each.When i write excel file from one text file and try to append the next text file's data in excel , i am getting the datas in rows.. but i need them in column.

One thing is the first column in all text files are same.
so i would like to have an excel with first column once and the remaining columns(only 2nd column from remaining files).

i used the below script.
i had the text files - log1, log2, log3, log4
( echo "count, time1"; sed 's/\s\+/,/g' log1) >Log.csv
( echo "time2"; awk -F' ' '{ print $2 }' log2) >> Log.csv
( echo "time3"; awk -F' ' '{ print $2 }' log3) >> Log.csv
( echo "time4"; awk -F' ' '{ print $2 }' log3) >> Log.csv

Last edited by arjun_b; 01-05-2012 at 07:09 AM.
 
Old 01-05-2012, 08:14 AM   #2
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,656

Rep: Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538
Welcome to LQ!

I think that the 'paste' command should achieve your desired result.
i.e.'paste log1 log2 log3 log4 > log.csv'

You may want to further format the file using 'colrm' to delete columns and 'sed' to change tab characters to comma space delimiters. This could also be handled within Excel.

It is a little difficult to be more precise without knowing the exact data format.
 
Old 01-05-2012, 08:30 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 16,316

Rep: Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905
The -n option will not output a newline.

http://linux.die.net/man/1/echo
 
Old 01-05-2012, 08:53 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Maybe a bit advanced for a novice (?), but let's unleash the power of awk:
Code:
awk '
BEGIN {
  _[0,0] = "count"
}

{
  _[0,ARGIND] = ("time" ARGIND)
  _[FNR,0] = $1
  _[FNR,ARGIND] = $2
}

END {
  for ( i = 0; i <= FNR; i++ ) {
    printf "%s", _[i,0]
    for ( j = 1; j <= ARGC-1; j++ )
      printf ",%s", _[i,j]
    printf "\n"
  }
}' log1 log2 log3 log4
A must-read, if you want to learn awk and understand the code, http://www.gnu.org/software/gawk/manual/gawk.html.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Insert a column of text to a text file docaia Linux - Newbie 5 06-02-2011 11:55 AM
want to open & manipulate column data from excel file. rinu budhbhatti LinuxQuestions.org Member Intro 1 02-01-2011 09:50 AM
Read text file column by column RVF16 Programming 11 05-31-2009 08:16 AM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 03:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:17 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration