LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2015, 11:44 AM   #1
xpac
LQ Newbie
 
Registered: Nov 2006
Posts: 7

Rep: Reputation: 0
Question about redirecting awk script output to a csv file?


Hi,
I have an awk script that reads through our text logs, and I would like to redirect that output to a csv file for easy use.

The output returns 3 columns separated by spaces in Linux as such:

number date username

However, when I redirect this to a csv file it puts all three values in the same Excel column. Is there an easy way to have each value show up in its own column in the csv sheet?

Last edited by xpac; 05-28-2015 at 11:57 AM.
 
Old 05-28-2015, 12:20 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Two options:
a. Tell Excel the fields are separated by spaces. IIRC when you open a CSV, Excel asks you about filed separators and formats.

b. (The better option): insert commas between your values: number, date, username. The format is named Comma Separated Values for a reason.

I don't know what your awk script looks like, but the simplest way to acieve this is:
Code:
print $1 "," $2 "," $3
Assuming $1 is number, $2 is username etc.

jlinkels
 
1 members found this post helpful.
Old 05-28-2015, 02:06 PM   #3
xpac
LQ Newbie
 
Registered: Nov 2006
Posts: 7

Original Poster
Rep: Reputation: 0
Here's the script:
Code:
awk '
{       split($3, d, "@")
        match($0, /"username":"[^"]*"/)
        user = substr($0, RSTART + 12, RLENGTH - 13)
        c[d[1] OFS user]++
}
END {   for(i in c)
                printf("%4d %s\n", c[i], i)
}' company.log | sort -k3,3 -k2,2
 
Old 05-28-2015, 02:11 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So change OFS to be "," and put a comma in your printf after 4d.
 
Old 05-28-2015, 02:31 PM   #5
xpac
LQ Newbie
 
Registered: Nov 2006
Posts: 7

Original Poster
Rep: Reputation: 0
Awesome, thanks guys!
 
Old 05-28-2015, 04:02 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Remember to mark as SOLVED when you have a solution
 
  


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
awk: how can I print out a message to the screen when redirecting the output to file. quanba Programming 8 07-13-2015 01:54 AM
Problem in redirecting awk output into a file paldebojyoti Linux - General 2 10-06-2012 05:19 PM
redirecting input from file in awk script Trd300 Linux - Newbie 51 09-27-2012 05:57 PM
[SOLVED] awk or sed to use CSV as input and XML as template and output to a single file bridrod Linux - Newbie 6 03-13-2012 07:00 PM
how to convert a simplte text file into csv using awk script certteam Linux - General 1 09-15-2010 12:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:03 PM.

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