Grep a variable length file in UNIX based on tilde delimiter

25 pts.
Tags:
delimited
delimiter
Grep
Unix
Hello: I need to grep a variable length UNIX file. This is a tilde delimited, 21 field file and I need to search for specific values in the 5th field. Please help. My UNIX knowledge is not very extensive. Thanks
ASKED: June 5, 2008  7:27 PM
UPDATED: June 6, 2008  4:41 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.

This depends on whether you want just that matching 5th field returned or the whole line that goes with it. To get just that fifth field assuming your filename is foo and you are looking for bar in the 5th field
<pre>cut -d~ -f5 foo | grep bar</pre>
To get the whole corresponding line:
<pre>egrep ‘*~*~*~*~*bar*~’ foo</pre>

Hmmm… the regexp needs some tuning.
Say your file has lines like:
<pre>aaa~bbb~cccc~dd~this~www</pre>
where “aaa”, “bbb” … are anything w/o “~”s.
You want to find lines with “this” then the regexp for four “anything” and a “~” is (untested):
<pre>[^~]*~\{4}</pre>
The breakdown: if
<pre>[abc] </pre>matches a single character a, b or c
<pre>[^abc] </pre>is any single character but a, b or c
then
<pre>[^~] </pre>is “not a tilde”
<pre>[^~]* </pre>is it zero or more times
<pre>[^~]*~ </pre>is it plus a tilde
<pre>[^~]*~\{4} </pre>is it four times

Now, you said “specific valueS” in col #5, and the regexp looks only for “this”.
To look for “this”, “that” and “what” the regexp is:
<pre>\(this\|that\|what\)</pre>
The escaped parenthesis is to make it atomic, a single nugget, not sure if they are needed, not sure if your program will need the escaping slashes.
The escaped “|” are to separate alternatives. Again, your grep might not need the escaping slashes.

All together now:
<pre>[^~]*~\{4}\(this\|that\|what\)~</pre>
Will look for “this”, “that” and “what” in the 5th element and a ~ closing this element if it’s not the last one.
This is to avoid selecting, for example, “thisRexExpSucks”.

Juan Lanus

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following