How to grep cells that contain a specific string?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to grep cells that contain a specific string?
# 1  
Old 03-09-2012
How to grep cells that contain a specific string?

How do you grep cells that contain a specific string. I tried grep but it greps the whole line and not just the cells. Thanks!
# 2  
Old 03-09-2012
What's the difference? If it's in the line, wouldn't it be in a cell too?

Or do you mean it has to take an entire cell? That's what I'll guess until you clarify.

As usual, the awk programming language can be easily used for your question. This should print only lines where a cell matches the given regular expression. The ^ and $ in REGEX have slightly different meanings here -- instead of 'beginning of the line' and 'end of the line', they match the beginning and end of the cell.

Code:
awk -v REGEX="^string$" '{ P=0; for(N=1; (!P)&&(N<=NF); N++) if($N  ~ REGEX) P=1 } P' filename

# 3  
Old 03-09-2012
For some reason that line of code does not work. I meant grepping the entire cell given a string that is included in the line such as:

Input:
string 12
forum 25
star 35
pages 45
burst 65

If the string is "st", the resulting output would be:

string
star
burst
# 4  
Old 03-09-2012
Please include example input and output sooner then so we spend less time guessing what you want.

Try this:

Code:
awk -v REGEX="st" '{ for(N=1; N<=NF; N++) if($N ~ REGEX) print $N }' filename

Another way would be to just split columns into rows. Then you really could do grep.
Code:
tr -s ' ' '\n' < inputfile | grep 'regex'

# 5  
Old 03-10-2012
Hi,

If you want to grep only for a particular string 'string' only, you can try:

Code:
cat <file_name> | grep -v .string | grep -v string.

As an example:
Here I need top display exclusively the word 'at'

Code:
 
#cat > ftest
sat
batter
latex
hat
ate
heater
at
 
#more ftest | grep -v .at | grep -v at.
at

I worked this out... please tell if it sufficed your need.
Cheer!

Last edited by satish51392111; 03-10-2012 at 02:36 PM.. Reason: For clarity
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep only words containing specific string

Hello, I have two files. All urls are space seperated. source http://xx.yy.zz http://df.ss.sd.xz http://09.09.090.01 http://11.22.33 http://canada.xx.yy http://01.02.03.04 http://33.44.55 http://98.87.76.65 http://russia.xx.zz http://aa.tt.xx.zz http://1w.2e.3r.4t http://china.rr.tt ... (4 Replies)
Discussion started by: baris35
4 Replies

2. Shell Programming and Scripting

Grep and neglect a specific string

Hi, I have a file with "n" number of lines. I need to get rid of a specific line having a specific string from the file. I tried some possibilities but not successful. For ex: in a file named "test" hope should be removed along with the line. ... (8 Replies)
Discussion started by: ricky-row
8 Replies

3. UNIX for Dummies Questions & Answers

Grep contains specific string

i have file input dsgfdgdfgd> cab |egrep -i '(active|cbu)' 130502-11:34:11 10.133.1.153 9.0j stopfile=/tmp/15959 Trying password from ipdatabase file: /opt/ericsson/amos/moshell/sitefiles/ipdatabase... .. 0 1 CBU1 OFF ON 16HZ ROJ1192209/1 R5E TU8BZ04466... (3 Replies)
Discussion started by: radius
3 Replies

4. UNIX for Dummies Questions & Answers

Replacing all cells that have a specific value in a text file

I have a space delimited text file where I want to replace all cells that are 0 with NA. However I cannot simply use 'sed/0/NA/g' because there are other 0's in the text file that are part of numbers. Example input: 896.933464285715 0 874.691732142857 866.404660714286 Output:... (1 Reply)
Discussion started by: evelibertine
1 Replies

5. UNIX for Dummies Questions & Answers

Grep Specific String In CSV

Hi All, I have a csv file like the following: "ABCD2","EFGH2","XXXX","1" "ABCD2","EFGH2","XXXX","2" I want to grep out the row which contains the value of 2 within the 4th column, so then i can use the extracted record to cut up and store into numerous variables. Obviously when... (3 Replies)
Discussion started by: RichZR
3 Replies

6. UNIX for Dummies Questions & Answers

Using grep to remove cells instead of whole lines

I would like to use grep to remove certain strings from a text file but I can't use the grep -v option because it removes the whole line that includes the string whereas I just want to remove the string. How do I go about doing that? My input file: Magmas CEU rs12542019 CPNE1 RBM12 CEU... (2 Replies)
Discussion started by: evelibertine
2 Replies

7. UNIX Desktop Questions & Answers

Using grep to remove cells instead of lines

I would like to use grep to remove certain strings from a text file but I can't use the grep -v option because it removes the whole line that includes the string whereas I just want to remove the string. How do I go about doing that? My input file: Magmas CEU rs12542019 CPNE1 RBM12 CEU... (1 Reply)
Discussion started by: evelibertine
1 Replies

8. Programming

How to grep the specific string or user's list from the file

I have a file on UNIX system from where I want to grep the list of all users associated to the particular repository.If the user's list is in single line then I fetch all list but if it is in two separate lines it doesn't.I use the below command a=KESTREL-DEV;b=users;cat access_file|grep... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

9. UNIX for Dummies Questions & Answers

Deleting cells that contain a specific number only from a space delimited text file

I have this space delimited large text file with more than 1,000,000+ columns and about 100 rows. I want to delete all the cells that consist of just 2 (leave 2's that are not by themselves intact): File before modification aa bb cc 2 NA100 dd aa b1 c2 2 NA102 de File after modification... (1 Reply)
Discussion started by: evelibertine
1 Replies

10. Shell Programming and Scripting

grep - searching for a specific string

ppl, this is my "file" with fields orderno orderdate orderdesc telno street city 1 01/04/2006 abc 123 100 tampa 2 01/04/2006 abc 123 100 tampa 3 01/04/2006 abc 123 100 tampa 4 01/04/2006 abc ... (2 Replies)
Discussion started by: manthasirisha
2 Replies
Login or Register to Ask a Question