LinuxQuestions.org
Review your favorite Linux distribution.
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 12-14-2011, 07:08 AM   #1
noony123
Member
 
Registered: Oct 2010
Posts: 167

Rep: Reputation: 0
consolidating rows from csv file


Hi all. I have a csv file with complete rows like this
(xxx is just random value but is needed)
0801 xxxx xxxx xxxx
0801 xxxx xxxx xxxx
0801 xxxx xxxx xxxx
0801 xxxx xxxx xxxx
0801 xxxx xxxx xxxx
0851 yyyy yyyy yyyy
0851 yyyy yyyy yyyy
0851 yyyy yyyy yyyy
0851 yyyy yyyy yyyy
0851 yyyy yyyy yyyy

(xxxx values in all rows are distinct and unique, i just wrote xxx for simplicity)

Now i want to consolidate all rows containing 0801 to a csv file named 0801, same with 0851 with 0851.csv file. But need to automate this process, is it possible using any builtin linux tool like awk or something ? kindly guide me
 
Old 12-14-2011, 07:34 AM   #2
zQUEz
Member
 
Registered: Jun 2007
Distribution: Fedora, RHEL, Centos
Posts: 294

Rep: Reputation: 54
Does this achieve what you are after:
Code:
for i in `cat input.file |cut -d" " -f1 |sort -u`; do cat input.file |grep ^$i >$i; sed -i 's/ /,/g' $i; done
this assumes your origonal csv is called "input.file".

Last edited by zQUEz; 12-14-2011 at 07:35 AM.
 
Old 12-14-2011, 08:08 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Code:
awk '{ print > $1 ".csv" }' file
 
Old 12-14-2011, 08:47 AM   #4
AwesomeMachine
Senior Member
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora
Posts: 1,829

Rep: Reputation: 257Reputation: 257Reputation: 257
You could try

#!/bin/bash
$ tr -t " " "," < input.file > output.file
if grep '0801' output.file
then `grep '0801' output.file` >> 0801.csv
elif grep '0851' output.file
`grep '0851' output.file` >> 0851.csv
else
echo "done"
fi
exit 0
 
  


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
How to ignore rows with a specific character in a csv file ziggy25 Linux - Newbie 8 03-13-2010 01:38 PM
How to create RHEL 5.3 kickstart installation consolidating 4 DVDs? ranjanm1 Linux - General 1 12-08-2009 03:40 AM
convert columns to rows (tab separated file to csv) doug23 Programming 16 08-16-2009 10:14 PM
Compare two fields on consecutive rows and print the two rows aditi_borkar Linux - Newbie 3 04-09-2009 06:49 AM
Comparing two csv files and write different record in third CSV file irfanb146 Linux - Newbie 3 06-30-2008 10:15 PM


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