Egrep or awk for removing values within CSV file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Egrep or awk for removing values within CSV file?
# 1  
Old 02-25-2015
Egrep or awk for removing values within CSV file?

Hello,

I have a large CSV file that contains values all on the same column, and in one very long row (e.g. no line breaks till end, with all data values separated by a comma).

The file has two types of data for the values. One begins with the letters rs and some numbers. The other begins with the letter i and some numbers. An example is below (id's are genome identifiers).

Code:
rs28931576,rs11542040,rs28931577,rs429358,i6007484,i6007510,rs28931578,i6007500,i6007489,i5000217,i6007504,i6007493,rs769455,i6007507,i6007497,i6007512,i6007495,i6007485,i6007492,i5000216,i5000205,rs7412

My Unix command line knowledge was enough to use the cat and cut commands to get the above data to this point.

I can't seem to figure out how to remove all of the values that begin with the letter i. I've tried some awk and egrep commands, but don't have the mastery yet to get this figured out.

I also need a way to get rid of duplicate commas after the i values are removed.

Right now, I'm using Find-Replace with TextEdit on mac to do these steps, however I'd love to be able to script this.

Any help is much appreciated!

Last edited by Don Cragun; 02-26-2015 at 01:29 AM.. Reason: Add CODE tags.
# 2  
Old 02-26-2015
Try:

Code:
awk '!/i/' RS="," ORS="," file.csv

# 3  
Old 02-26-2015
Please use CODE tags around sample input, output, and code in your posts. Without them, long lines get split into multiple lines adding extraneous whitespace into your sample data.

You could also use something like:
Code:
sed -e 's/i[^,]*,//g' -e 's/i[^,]*$//' file

With your sample input, this produces the output:
Code:
rs28931576,rs11542040,rs28931577,rs429358,rs28931578,rs769455,rs7412

If your input lines are longer than LINE_MAX on your system, sed may fail due to line length limitations, while the awk script pilnet101 suggested should still work. But if the line length limit isn't a problem, sed is usually smaller and faster than awk for jobs like this. You can find the value of LINE_MAX on your system with the command:
Code:
getconf LINE_MAX

but it should not be less than 2048.
# 4  
Old 02-26-2015
With this particular input, this might also work:
Code:
sed 's/i[^,]*,\{0,1\}//g' file



Quote:
Originally Posted by pilnet101
Try:
---
Code:
awk '!/i/' RS="," ORS="," file.csv

This works fine, but it does leave a trailing comma instead of a newline.
It would not hurt to use !/^i/ which would provide extra security here, even though it is not required with this particular input.
# 5  
Old 02-26-2015
Thank you for the help. With the suggestion, I was able to get the text formatted correctly.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Removing commas from CSV file

Hi I'm creating a sh script to generate a csv file. The CSV contains the values from a sql table. The content looks this: a,b,c,c2,c3,,,,,,,,,,,d,e I have some code that can separate the fields using the comma as delimiter, but some values actually contain commas, such as... (2 Replies)
Discussion started by: preema
2 Replies

2. Shell Programming and Scripting

Bash script help - removing certain rows from .csv file

Hello Everyone, I am trying to find a way to take a .csv file with 7 columns and a ton of rows (over 600,000) and remove the entire row if the cell in forth column is blank. Just to give you a little background on why I am doing this (just in case there is an easier way), I am pulling... (3 Replies)
Discussion started by: MrTuxor
3 Replies

3. Shell Programming and Scripting

Removing \n from .csv file

Hi, I have a requirement like my .csv file is generating from a db2 table using export command like below: file format: ----------- 2011 4 0 0 N S C C "BHPC BHPC" 0 0 0 2011 5 0 0 N S C C "BHPC BHPC" 0 0 0 here BHPC is having new line character and because this when i am trying... (4 Replies)
Discussion started by: RRVARMA
4 Replies

4. Shell Programming and Scripting

removing lines with similar values from file

Hello, got a file with this structure: 33274 171030 02/29/2012 37897 P_GEH 2012-02-29 10:31:26 33275 171049 02/29/2012 38132 P_GEH 2012-02-29 10:35:27 33276 171058 02/29/2012 38515 P_GEH 2012-02-29 10:43:26 33277 170748 02/29/2012 40685 P_KOM ... (3 Replies)
Discussion started by: krecik28
3 Replies

5. Shell Programming and Scripting

Removing zero values from text file

Hi all, I wrote the following code to remove the value which are 0 in the input file (a columns if numbers). awk 'BEGIN { for (i=1; i<=NF; i++) if ($i) printf("%13.6e\n",$i) }' $1 >> $2 The script works if the zeros are written as 0.0000 but not as 0.000000e+00 In... (10 Replies)
Discussion started by: f_o_555
10 Replies

6. Shell Programming and Scripting

Removing lines of a .csv file

Hello, Does anyone have a one-liner to remove lines of a csv file if the value in a specific column is zero? For example, I have this file, 12345,COM,5,0,N,29.95,Y 12345,MOM,1,0,N,29.95,Y 12345,COM,4,0,N,9.99,Y 12345,MOM,0,2,N,9.99,Y 12345,REN,0,1,N,9.99,Y and I want to remove lines... (4 Replies)
Discussion started by: palex
4 Replies

7. Linux

Removing non printing characters from a csv file

Hi, I have an csv file and there are some non printable characters(extended ascii) so I am trying to create a clean copy of the csv file . I am using this command: tr -cd "" < /opt/informatica/PowerCenter8.6.0/server/infa_shared/SrcFiles/ThirdParty/locations.csv > ... (4 Replies)
Discussion started by: gerkus
4 Replies

8. Shell Programming and Scripting

How to grep/awk/egrep two values for given output?

Dear Friends, I have a command which can result following output. Packet is: /var/adm/yyyy/pkt6043 Intended for network : /vob/repo I would like to retrive pkt6043 and /vob/repo using single command. Blue color test will be always contstant and red color text will be dynamic ... (2 Replies)
Discussion started by: baluchen
2 Replies

9. UNIX for Dummies Questions & Answers

Help removing strings from one file that match any of the values in a second file.

Hello, I have a file that lists a few hundred values. Example: abca abcb abcc abcd I have a 2nd file with a few thousand lines. I need to remove every line from the 2nd file that contains any of the values listed in first file. Example of strings to delete: line1 *abca* end of... (1 Reply)
Discussion started by: upstate_boy
1 Replies

10. Shell Programming and Scripting

Need to compare two csv files values and write into another csv file

Hi all, Am new to scripting. So i just need your ideas to help me out. Here goes my requirement. I have two csv files 1.csv 2.csv abc,1.24 abc,1 def,2.13 def,1 I need to compare the first column of 1.csv with 2.csv and if matches then need to compare... (2 Replies)
Discussion started by: chinnahyd
2 Replies
Login or Register to Ask a Question