Rajagopal Swaminathan
2012-Aug-28 19:59 UTC
[CentOS] Cut command behaviour - bug or feature
Greetings, I have a CSV file with three fields. eg. a1,b1,c1 a2,b2,c2 .... I wanted the output to be: b1,c1,a1 b2,c2,a2 .... the command cut -d, -f2,3,1 <file> returns a1,b1,c1 cut -d, -f2,3 <file> works as advertised. Is it specific to linux? In that case how do I go about swapping two columns? I do not think a gazzillion byte gui is required. The file size is about 43Megs. any ideas? -- Regards, Rajagopal
Rajagopal Swaminathan wrote:> Greetings, > > I have a CSV file with three fields. > eg. > > a1,b1,c1 > a2,b2,c2 > .... > > I wanted the output to be: > b1,c1,a1 > b2,c2,a2 > .... > > the command > cut -d, -f2,3,1 <file> > > returns > > a1,b1,c1 > > cut -d, -f2,3 <file> > > works as advertised. > > Is it specific to linux? > > In that case how do I go about swapping two columns? I do not think a > gazzillion byte gui is required. > > The file size is about 43Megs.I've never been that good with cut. I'm going to see the author, Dave Ihnat this weekend, who sometimes shows up here... but in the meantime, you might use awk 'BEGIN {FS=",";}{print $2 "," $3 "," $1;}' infile mark
On 28.8.2012 21:59, Rajagopal Swaminathan wrote:> Greetings, > > I have a CSV file with three fields. > eg. > > a1,b1,c1 > a2,b2,c2 > .... > > I wanted the output to be: > b1,c1,a1 > b2,c2,a2 > .... > > the command > cut -d, -f2,3,1 <file> > > returns > > a1,b1,c1cut cuts out, that what it does. I think it works exactly as advertised. On my system the manpage says ...snip Selected input is written in the same order that it is read, and is written exactly once snap...> Is it specific to linux?No! -- Kind Regards, Markus Falb -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 307 bytes Desc: OpenPGP digital signature URL: <http://lists.centos.org/pipermail/centos/attachments/20120828/bb550d8f/attachment-0001.sig>
On Tue, Aug 28, 2012 at 2:59 PM, Rajagopal Swaminathan <raju.rajsand at gmail.com> wrote:> I have a CSV file with three fields. > eg. > > a1,b1,c1 > a2,b2,c2 > .... > > I wanted the output to be: > b1,c1,a1 > b2,c2,a2 > .... > > the command > cut -d, -f2,3,1 <file> > > returns > > a1,b1,c1 > > cut -d, -f2,3 <file> > > works as advertised. > > Is it specific to linux? > > In that case how do I go about swapping two columns? I do not think a > gazzillion byte gui is required. > > The file size is about 43Megs. > > any ideas?If you are sure that the fields will never contain commas, there are any number of ways to split lines into variables in any number of scripting languages including bash. However, if the input is 'generic csv' that is allowed to have quotes around fields with embedded commas it can be hard to parse. I'd recommend perl with the Text::CSV module so you don't have to deal with parsing yourself. -- Les Mikesell lesmikesell at gmail.com
On Wed, Aug 29, 2012 at 01:29:05AM +0530, Rajagopal Swaminathan wrote:> a1,b1,c1 > a2,b2,c2> cut -d, -f2,3,1 <file>Will not work. The "-f" flag specifies what columns; not the order. Everyone makes that mistake once :-)> In that case how do I go about swapping two columns? I do not think a > gazzillion byte gui is required.Use "awk" awk -F, '{print $2 "," $3 "," $1}' -- rgds Stephen
Rajagopal Swaminathan
2012-Aug-29 15:43 UTC
[CentOS] Cut command behaviour - bug or feature
On Wed, Aug 29, 2012 at 2:15 AM, Markus Falb <markus.falb at fasel.at> wrote:> On 28.8.2012 21:59, Rajagopal Swaminathan wrote: > > cut cuts out, that what it does. > I think it works exactly as advertised. > On my system the manpage says > > ...snip > Selected input is written in the same order that it is > read, and is written exactly once > snap... >See below>> Is it specific to linux? > > No!I would think, yes. From: http://www.unix.com/unix-dummies-questions-answers/117504-question-cut-command.html [quote] 08-22-2009 Scott's Avatar Scott Scott is offline Forum Staff Administrator Join Date: Jun 2009 Location: Switzerland - ZH Posts: 5,632 Thanks: 136 Thanked 596 Times in 515 Posts In AIX (for example) if you said Code: echo 1,2,3,4,5 | cut -d, -f3,1,5 you would get output as 3,1,5 But the cut command in Linux behaves differently.>From the man page:Quote: Selected input is written in the same order that it is read, and is written exactly once. Which I think is poor. I can't see any way to do what you want with cut. [unquote] Thanks for everybody who pitched in Any the issue got transformed into something else altogether.... (PHB, vlookup 'experts' and the such for time being) -- Regards, Rajagopal