Inverse of Cut


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inverse of Cut
# 1  
Old 02-29-2012
Java Inverse of Cut

Hi List,

I have a CSV file which I am manipulating. Each time I receive the CSV for processing, there will be extra columns of data.

Here's what I want to do; I want to be able to remove specific columns from the CSV data file, but keep the remaining columns, where the remaining columns are variable in how many columns there are. (there will be more columns each time the script is run on new data)

Lets say I want to remove columns 2,3,4,5,10 and keep the rest

Normally, in a fixed length CSV, which has the same amount of columns every time it is processed, say 20 columns each time, I would use the following command to achieve this: (thereby specifying columns I do want)

cut -d"," -f 1,6,7,8,9,11,12,13,14,15,16,17,18,19,20

But since the amount of columns will increase each time I have to run this process, then I need a means of specifying columns that I don't want (inverse cut) rather than what I do want. Perhaps this could be achieved in sed or awk..

Has anyone got any ideas?

Any help much appreciated.

thanks,
land
# 2  
Old 02-29-2012
If your cut supports --complement then:
Code:
cut -d, --complement -f2-5,10 infile

Otherwise, try this:

Code:
awk -F, -vR="2,3,4,5,10" 'BEGIN{split(R,A,",");for(v in A) S[A[v]]=1}
{for(i=1;i<=NF;i++)if(!(i in S)) printf $i " ";print ""}' infile

---------- Post updated at 01:34 PM ---------- Previous update was at 01:29 PM ----------

Or for something a bit more fancy:

Code:
awk -F, -vR="2-5,10" '
BEGIN {
 split(R,A,",");
 for(v in A) if (split(A[v],V,"-") > 1)
 for(i=V[1];i<=V[2];i++)S[i]=1
 else S[V[1]]=1}
{for(i=1;i<=NF;i++)if(!(i in S)) printf $i " ";print ""}' infile

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 02-29-2012
Thanks for the comprehensive options Chubler.. I'll do some testing with this next time I'm logged in.
# 4  
Old 03-01-2012
Quote:
Originally Posted by landossa
Lets say I want to remove columns 2,3,4,5,10 and keep the rest

Normally, in a fixed length CSV, which has the same amount of columns every time it is processed, say 20 columns each time, I would use the following command to achieve this: (thereby specifying columns I do want)

cut -d"," -f 1,6,7,8,9,11,12,13,14,15,16,17,18,19,20

But since the amount of columns will increase each time I have to run this process, then I need a means of specifying columns that I don't want (inverse cut) rather than what I do want. Perhaps this could be achieved in sed or awk..
Quote:
Originally Posted by Chubler_XL
If your cut supports --complement then:
Code:
cut -d, --complement -f2-5,10 infile

Code:
awk -F, -vR="2,3,4,5,10" 'BEGIN{split(R,A,",");for(v in A) S[A[v]]=1}
{for(i=1;i<=NF;i++)if(!(i in S)) printf $i " ";print ""}' infile


Code:
awk -F, -vR="2-5,10" '
BEGIN {
 split(R,A,",");
 for(v in A) if (split(A[v],V,"-") > 1)
 for(i=V[1];i<=V[2];i++)S[i]=1
 else S[V[1]]=1}
{for(i=1;i<=NF;i++)if(!(i in S)) printf $i " ";print ""}' infile


You can easily do this with cut itself, using standard functionality:
Code:
cut -d, -f 1,6-9,11-

Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I generate a script with an inverse action?

I have an interactive script which sorts and processes a variety of filetypes from an unsorted folder into newly created directories. The script works great, But I was wondering how I could generate a script with an inverse action so that I could unwind / undo the executed script and its sorting... (3 Replies)
Discussion started by: Braveheart
3 Replies

2. Programming

Python re.findall inverse Match

I ask of you but yet another simplistic question that I hope can be answered. Its better explained showing my code. Here is my list(tmp_pkglist), which contains a list of all Debian (Jessie) packages: snippet 'zssh (1.5c.debian.1-3.2+b1 , 1.5c.debian.1-3.2 )', 'zsync (0.6.2-1)', 'ztex-bmp... (2 Replies)
Discussion started by: metallica1973
2 Replies

3. Shell Programming and Scripting

Grep -v (inverse matching)

I am totally unexperienced in writing scripts of any kind. I am working on Mac OS X and would like to run a shell script to find files in a directory that do not conform to a specific naming convention and print to a text file in the same directory. For example, I have a folder called... (9 Replies)
Discussion started by: j_alicea
9 Replies

4. Shell Programming and Scripting

Inverse Grep

Hi, I'm trying to wtite a script which actually print the text which doesn't contain a word , i mean to say. eg:- if a file contains the follwoing data Hello how ru ??? What ru doing ? what is the % of data contained ??? I want to write a script such that it prints the line excluding... (1 Reply)
Discussion started by: nagios
1 Replies

5. UNIX for Dummies Questions & Answers

inverse remove command

Hi All, Is there an option for 'rm' to "remove everything but these files"? For instance if I have: a.txt <-- I just want to remove this one a_1.txt a_2.txt Is there an option ? for this: rm -? *_*.txt Thanks, ScKaSx (4 Replies)
Discussion started by: ScKaSx
4 Replies

6. Shell Programming and Scripting

matrix inverse (awk)

I need to inverse a matrix given in a file. The problem is I'm stuck with writing determinant finding algoritm into code. I found this algoritm about finding determinant of nxn matrix. This is what i need: Matrices and Determinants and here: a11 a12 a13 a21 a22 a23 a31 a32 a33... (0 Replies)
Discussion started by: vesyyr
0 Replies

7. UNIX for Dummies Questions & Answers

need help-matrix inverse (awk)

I have few days to complete my awk homework. But I'm stucked. i hope some1 will help me out. I have to inverse n x n matrix, but I have problems with finding the determinant of the matrix. I found the algoritm, how to find a determinant of n x n matrix:... (0 Replies)
Discussion started by: vesyyr
0 Replies

8. UNIX for Dummies Questions & Answers

Inverse Video

I am trying to discover the sequence of escape characters that I need to pass ksh in order to turn inverse video on and off. I need this information for two reasons: * I want to include inverse video text in some shell scripts * I wish to highlight a section of an /etc/motd file Thanks for... (2 Replies)
Discussion started by: sam_pointer
2 Replies
Login or Register to Ask a Question