LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 03-24-2013, 01:46 PM   #1
SAAAD
LQ Newbie
 
Registered: Mar 2013
Posts: 14

Rep: Reputation: Disabled
cat command to make tabular format report


Hi,

is anybody know how to use cat command to make a report as a table using : to separate between the columns??
e.g:
name:age:gender:

Thanx
 
Old 03-24-2013, 02:14 PM   #2
DinoFly
Member
 
Registered: Nov 2007
Distribution: Fedora, CentOS,
Posts: 72

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

is anybody know how to use cat command to make a report as a table using : to separate between the columns??
e.g:
name:age:gender:

Thanx
Does it have to be cat? I think you cant do this with cat but you can use sed (stream editor). Plus if u want u can do the space replacement with comma and so make a csv file that can be read by most worksheets. To do so simply use

Quote:
sed 's/[:space:]+/,/g' /path/to/oldfile > /path/to/newfile
This way you keep old files and create new ones, of course if u wana replace them use same file after stream redirector or whole command should look like this:

Quote:
sed 's/[:space:]+/,/g' /path/to/oldfile > /path/to/oldfile
it only replaces space " " with commas "," it does not format or add empty values for missing columns in the rows. And sure make .csv extension. so sed 's/[:space:]+/,/g' /path/to/oldfile > /path/to/newfile.csv
or replace the "," with symbol of your choice.
 
Old 03-24-2013, 02:18 PM   #3
SAAAD
LQ Newbie
 
Registered: Mar 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
Thanx, But..

It should be using cat and separate it by :.
Do you know how?
 
Old 03-24-2013, 02:33 PM   #4
DinoFly
Member
 
Registered: Nov 2007
Distribution: Fedora, CentOS,
Posts: 72

Rep: Reputation: Disabled
one more command is tr that u can use it would be

Quote:
tr ' ' ':' < /path/to/oldfile > /path/to/newfile
if you need to use cat, you can combine it with pipe "|" or sth. you can do this on bash shell
Quote:
tr ' ' ':' < /path/to/oldfile > /path/to/newfile | cat /path/to/newfile
but if u dont need to save it to the newfile than simply use tr and let the standard input be your monitor not the file

Quote:
tr ' ' ':' < /path/to/oldfile
I think cat wont do the job itself.
 
Old 03-24-2013, 03:36 PM   #5
SAAAD
LQ Newbie
 
Registered: Mar 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by DinoFly View Post
one more command is tr that u can use it would be



if you need to use cat, you can combine it with pipe "|" or sth. you can do this on bash shell


but if u dont need to save it to the newfile than simply use tr and let the standard input be your monitor not the file



I think cat wont do the job itself.
I think I shouldn't use cat!
 
Old 03-24-2013, 05:47 PM   #6
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
A lot depends on the format of the input file. I can't find a way to format the output as desired, if the name, age, and gender are arranged in records horizontally.

However, if the data is vertical in the file, it can be done.

Example input filename test:
Quote:
brian
13
male
cindy
14
female
lulu
15
female
Command to output as name:age:gender
Code:
cat test |  pr -a --separator=: --columns=3
Sample output:
Quote:
2013-03-24 17:47 Page 1


brian:13 :male
cindy :14 :female
lulu :15 :female
 
Old 03-24-2013, 06:24 PM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by SAAAD View Post
I think I shouldn't use cat!
Bingo.

"cat" is short for "concatenate". The "cat" utility is designed to copy multiple files, appending to the output of the previous file.

It doesn't do columns.

column data/report generation is what awk was developed for.
 
Old 03-24-2013, 06:32 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Any chance to see a sample of the input file? What is the format of the report mentioned in the first post?
 
  


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
Nagios -report in pdf format arghyadas Linux - Software 2 12-02-2010 01:21 AM
How to generate and format a report from Ubuntu database into Open Office arthurab Linux - Desktop 1 10-07-2010 08:45 PM
tabular to xml and back to tabular ebsbel Programming 3 03-14-2007 06:43 AM
tool to format report output from mysql? alar Linux - General 0 08-03-2004 08:16 PM
Bug report: wrong date/time format in last visited wipe LQ Suggestions & Feedback 0 07-29-2004 06:21 AM

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

All times are GMT -5. The time now is 01:35 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