Grep the column for a row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep the column for a row
# 1  
Old 07-11-2013
Grep the column for a row

I have a problem caught and need to discuss with all you guys.

I have a file containing a rows which are separated by “~”
For eg.
Code:
Server~321~UP~Linux~Member
121213~5778~Down~Unix~Provider

I want to use the grep which search the row on the basis of columns like the grep should only check the 2nd column of a file and if it found that the whole row should be published.
# 2  
Old 07-11-2013
It's easier to use awk:

Code:
awk -F~ '$2~/grep-pattern/' file

# 3  
Old 07-11-2013
I have a lot of folders which contain all the zipped files. So basically what I doing is
Code:
find . -name "Filename*" -print | while read line
do 
gzcat $line | ?????????????????
done

# 4  
Old 07-11-2013
If you don't like Subbeh's proposal, try
Code:
grep '^[^~]*~5778' file

where 5778 is your pattern in the second column.

And - ever looked into zgrep? man zgrep:
Quote:
DESCRIPTION
Zgrep invokes grep on compressed or gzipped files.
# 5  
Old 07-12-2013
Hi. I appreciate your idea. But I want to keep my scope to second column only.
I have a case in my data which looks like
Code:
server~99234~patched~99~down~done

and suppose I grep whether second column contains 99 or not. It does not contain but also this row will be published. Because 4th column contains 99, if you know what i mean.

Regards
ADI
# 6  
Old 07-12-2013
Quote:
Originally Posted by adisky123
Hi. I appreciate your idea. But I want to keep my scope to second column only.
I have a case in my data which looks like
Code:
server~99234~patched~99~down~done

and suppose I grep whether second column contains 99 or not. It does not contain but also this row will be published. Because 4th column contains 99, if you know what i mean.

Regards
ADI
No, it doesn't. The command:
Code:
grep '^[^~]*~5778' file

will print any line where the 2nd field STARTS with 5778. If you only want it to match when the 2nd field IS 5778, try:
Code:
grep '^[^~]*~5778~' file

If you used the command:
Code:
grep '^[^~]*~99' file

it didn't match the 4th field in the line:
Code:
server~99234~patched~99~down~done

it match the 99 in red.

If you want "contains 99" instead of "is 99", try:
Code:
grep '^[^~]*~[^~]*99' file

which would match any of the lines:
Code:
server~99234~patched~12~down~done
server~29934~patched~34~down~done
server~23994~patched~56~down~done
server~23499~patched~78~down~done

# 7  
Old 07-12-2013
Quote:
Originally Posted by adisky123
Hi. I appreciate your idea. But I want to keep my scope to second column only.
I have a case in my data which looks like
Code:
server~99234~patched~99~down~done

and suppose I grep whether second column contains 99 or not. It does not contain but also this row will be published. Because 4th column contains 99, if you know what i mean.

Regards
ADI
If 99 must be the exact pattern RubiC gave you the solution, you just have to adjust it to your needs.
Code:
grep '^[^~]*~99~' file

The trailing ~ will guarantee the match against other patterns with 99 included in the "second" column.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print first row of column a, last row of column b if column a has the same value

I have a table with this structure: cola colb colc 1 19 lemon 20 31 lemon 32 100 lemon 159 205 cherries 210 500 cherries and need to parse it into this format: cola colb colc 1 100 lemon 159 500 cherries So I need the first row of cola and the last row of colb if colc has the... (3 Replies)
Discussion started by: coppuca
3 Replies

2. Shell Programming and Scripting

Search/grep on row and column wise

Hello, I have a comma seperate metadata as follows: CITY ,COUNTY,STATE,COUNTRY NEW_YORK,NYC ,NY ,USA NEWARK ,ESSEX ,NJ ,USA CHICAGO ,COOK ,IL ,USA SEATTLE ,MINER ,WA ,USA In my process, I get two key values ie CITY NAME (can be one of the... (7 Replies)
Discussion started by: calredd
7 Replies

3. Shell Programming and Scripting

Print row on 4th column to all row

Dear All, I have input : SEG901 5173 9005 5740 SEG902 5227 5284 SEG903 5284 5346 SEG904 5346 9010 SEG905 5400 5456 SEG906 5456 5511 SEG907 5511 9011 SEG908 5572 9015 SEG909 5622 9020 SEG910 5678 5739 SEG911 5739 5796 SEG912 5796 9025 ... (3 Replies)
Discussion started by: attila
3 Replies

4. Shell Programming and Scripting

Print every 5 4th column values as separate row with different first column

Hi, I have the following file, chr1 100 200 20 chr1 201 300 22 chr1 220 345 23 chr1 230 456 33.5 chr1 243 567 90 chr1 345 600 20 chr1 430 619 21.78 chr1 870 910 112.3 chr1 914 920 12 chr1 930 999 13 My output would be peak1 20 22 23 33.5 90 peak2 20 21.78 112.3 12 13 Here the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

6. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

7. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

8. Shell Programming and Scripting

row to column and position data in to fixed column width

Dear friends, Below is my program and current output. I wish to have 3 or 4 column output in order to accomodate in single page. i do have subsequent command to process after user enter the number. Program COUNT=1 for MYDIR in `ls /` do VOBS=${MYDIR} echo "${COUNT}. ${MYDIR}" ... (4 Replies)
Discussion started by: baluchen
4 Replies

9. Shell Programming and Scripting

Row to column converter using Awk or grep?

Hello, Can someone please help me on this.:confused: I have a file which has more than 1 million lines (XML file). What I need is: Search for "abcd" in the input file > output the result into a output.txt (colloum1) Search for "efghi" in the input file > output the result in to... (3 Replies)
Discussion started by: Needhelp2
3 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question