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.
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.
Subbeh
July 11, 2013, 10:00am
2
It's easier to use awk:
awk -F~ '$2~/grep-pattern/' file
I have a lot of folders which contain all the zipped files. So basically what I doing is
find . -name "Filename*" -print | while read line
do
gzcat $line | ?????????????????
done
RudiC
July 11, 2013, 5:12pm
4
If you don't like Subbeh's proposal, try
grep '^[^~]*~5778' file
where 5778 is your pattern in the second column.
And - ever looked into zgrep? man zgrep
:
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
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
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
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:
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:
grep '^[^~]*~5778~' file
If you used the command:
grep '^[^~]*~99' file
it didn't match the 4th field in the line:
server~99234~patched~99~down~done
it match the 99 in red.
If you want "contains 99" instead of "is 99", try:
grep '^[^~]*~[^~]*99' file
which would match any of the lines:
server~99234~patched~12~down~done
server~29934~patched~34~down~done
server~23994~patched~56~down~done
server~23499~patched~78~down~done
Aia
July 12, 2013, 12:08am
7
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
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.
grep '^[^~]*~99~' file
The trailing ~ will guarantee the match against other patterns with 99 included in the "second" column.