grep only from a range of columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep only from a range of columns
# 1  
Old 04-30-2012
grep only from a range of columns

Hello all,

I have a .csv file with over 100 columns. I would like to grep for a pattern only searching within a range of those fields, and print the entire line. For example: grep a pattern from columns $47-$87, but print fields $0 - $100

Thanks!
# 2  
Old 04-30-2012
grep doesn't understand columns. awk does, however.

Is it an actual csv, as in, comma-separated?

Code:
awk -F"," '{ M=0; for(N=47; (!M) && (N<=87); N++) if($N ~ /regex/) M++; } M' filename

# 3  
Old 04-30-2012
Thank you for such quick reply. Yes it is comma separated. Will this print every column? And I'm assuming 'regex' is where i will put my desired pattern?
# 4  
Old 04-30-2012
It will print every column, and /regex/ is where you put your pattern, yes. The // are what you use instead of quotes, sort of like in sed.
# 5  
Old 04-30-2012
This command is working, but it seems to stop searching after line 34. My file has about 32,000 rows, and only 34 were printed. I know there will be at least 5000 rows containing my pattern.
# 6  
Old 04-30-2012
Please post enough of your data to show the problem, and please show the exact program you ran, including the regex.
# 7  
Old 04-30-2012
Unfortunately I can't really show the data, it has patient information Smilie. To be exact, I have 83 columns/fields and 32,224 rows/lines of data - comma seperated delimiter. I'm interested in the regex /hom/, but only as it appears in fields 43-83.

for example, a few lines will look like this:

PHP Code:
col    1 42     col 43      to       83
patient data      
- - - - - hom - - - - -
patient data      - - - - - - - - - - - -  
patient data      - - hom - - - - - - - -
hom ....data      - - - - - - - - - - - - 
And I don't want to return the last line, which contains a "hom" in col 1 - 42

The code I used:
PHP Code:
awk -F"," '{ M=0; for(N=43; (!M) && (N<=83); N++) if($N ~ /hom/) M++; } M' input.csv >hom.csv 
This code works, but my output file hom.csv only has 34 lines returned. By visual inspection, this is only the first 34 instances of "hom" in columns 43-83. I know by looking at my file that in line 23743, there is a "hom".

Any reason why this may be happening? Or will you have to see the entire file?

Thanks
 
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 letters after a certain character within a range of columns

Hi there, I am trying to remove al letters after : character on specific columns from 10th column till 827. I used sed and cut to do so but I am sure there is better one liner someone can think of from unix community members. Huge file but it has this structure (Total number of Columns =... (10 Replies)
Discussion started by: daashti
10 Replies

2. Shell Programming and Scripting

Reading specific range of columns in an Excel file

Hi All, I want to read an excel file. PFA excel, I want to read the cloumn from A to G and the V to AH starting from Row number 3. Please help me on this. (7 Replies)
Discussion started by: Abhisrajput
7 Replies

3. Shell Programming and Scripting

Sum of range of rows and columns in matrix

Hi all, I have a large matrix of 720 x 25. I want to get sum of range of rows and columns. Like, I need sum of all columns and row number 2 to 21, then leaving 22nd row, again sum of all columns and row number 23 to 42 again leaving 43rd row and then sum of 44th to 63. Means I want to add all... (4 Replies)
Discussion started by: CAch
4 Replies

4. Shell Programming and Scripting

Grep ip range

I want to find all files under /var directory that refer to any ip from 10.1.1.1 to 10.1.1.255 using grep. How can this be done. Please help (5 Replies)
Discussion started by: proactiveaditya
5 Replies

5. UNIX Desktop Questions & Answers

grep a range of text

hey, i need to grep / awk a list of "DEV ID" range for example for greping 0936 - 09C1 from this file: 0x5000097310036d05 558 1 20531 LOCAL GLOBAL 42048000 YES 0x8e36c8bd07ce9e13 DEV ID: 0933 0x5000097310036d05 559 1 20531 ... (1 Reply)
Discussion started by: boaz733
1 Replies

6. UNIX for Dummies Questions & Answers

How to match 2 columns where one column has data as a range - extended

Dear all, there is a nice solution for a text merge where the second file has only variables with a numeric range ( sorry, cannot post URL + thread is closed ). The real world is however more complicated than in the earlier example. file1 A 1 A 2 A 3 B 1 B 2 B 3 B 4 C 1 C 2 C 3 C... (4 Replies)
Discussion started by: underscore
4 Replies

7. Shell Programming and Scripting

Select columns from a matrix given within a range in BASH

I have a huge matrix file which looks like this (example matrix): 1 2 3 5 4 5 6 7 7 6 8 9 1 2 4 2 7 6 5 1 3 2 1 9 As one can see, this matrix has 4 columns and 6 rows. But my original matrix has some 3 million rows and 6000 columns. For example, on this matrix I can define my task as... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

8. Shell Programming and Scripting

awk to match a numeric range specified by two columns

Hi Everyone, Here's a snippet of my data: File 1 = testRef2: A1BG - 13208 13284 AAA1 - 34758475 34873943 AAAS - 53701240 53715412File 2 = 42MLN.3.bedS2: 13208 13208 13360 13363 13484 13518 13518My awk script: awk 'NR == FNR{a=$1;next} {$1>=a}{$1<=a}{print... (5 Replies)
Discussion started by: heecha
5 Replies

9. UNIX for Dummies Questions & Answers

How to match 2 columns where one column has data as a range

Hi, I have a query about joining files using data ranges. Example files below - I want to join file1 to file2 with matches where file1 column 1 is equal to file2 column1, and file1 column 2 is within the range of file2 columns 3 and 4. I would like rows which don't match to be printed too. ... (4 Replies)
Discussion started by: auburn
4 Replies

10. Shell Programming and Scripting

displaying range of columns

can i display range of columns in AWk... say in cut command i can display columnm 2-13.... is there is any simmilar command in awk (1 Reply)
Discussion started by: mahabunta
1 Replies
Login or Register to Ask a Question