LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-31-2014, 05:59 AM   #1
NdAd
LQ Newbie
 
Registered: Jul 2010
Posts: 18

Rep: Reputation: 0
Grep var content from a file


Hi Guys

I'm trying to grep variable content from file but for some reason it doesn't work.

I have a file , let say with sourceIP%ID,port destinationIP%ID,port :
Code:
$ cat file 
1.1.1.1%70:49513     2.2.2.2%40:6400
3.3.3.3%60:51420     4.4.4.4%30:5421
5.5.5.5%50:35784     6.6.6.6%20:5443
7.7.7.7%40:51110     8.8.8.8%10:6420

$ echo $ID
%50

$ grep "$ID" file 
$
I just want to grep the $ID from file. (I'm getting the $ID content by some other manipulation)
The thing is, when I'm doing simple assignment of ID="%50" all working properly as below.

Code:
$ ID="%50"
$ grep $ID file 
5.5.5.5%50:35784     6.6.6.6%20:5443
can someone help here please ?
 
Old 12-31-2014, 06:46 AM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With InFile1 ...
Code:
1.1.1.1%70:49513     2.2.2.2%40:6400
3.3.3.3%60:51420     4.4.4.4%30:5421
5.5.5.5%50:35784     6.6.6.6%20:5443
7.7.7.7%40:51110     8.8.8.8%10:6420
... and InFile2 ...
Code:
%50
... this grep ...
Code:
grep -f $InFile2 <$InFile1 >$OutFile
... produced this OutFile ...
Code:
5.5.5.5%50:35784     6.6.6.6%20:5443
Daniel B. Martin

Last edited by danielbmartin; 12-31-2014 at 06:47 AM. Reason: Cosmetic improvement, no change to code.
 
Old 12-31-2014, 06:46 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Then the content of $ID is not what you think/see. Use program od to find out:
Code:
 printf '%s' "$ID" | od -tx1

Last edited by NevemTeve; 12-31-2014 at 06:47 AM.
 
Old 12-31-2014, 08:13 AM   #4
NdAd
LQ Newbie
 
Registered: Jul 2010
Posts: 18

Original Poster
Rep: Reputation: 0
Talking

Quote:
Originally Posted by NevemTeve View Post
Then the content of $ID is not what you think/see. Use program od to find out:
Code:
 printf '%s' "$ID" | od -tx1

Nice to know this. I compared the output from this tool for both options what seems different.

Anyway I digged a little deeper and found the problem.
the file I used for grep the number (50) into the var $ID contained some newlines characters CRLF. that's what mess it apparently.
I remove all these CRLF characters and now it works.

Thank you guys
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep some content from a large file simhumcon Linux - Newbie 1 07-03-2014 08:29 AM
[SOLVED] Using master file for grep content keif Programming 3 05-06-2013 09:48 AM
How do I grep my /var/log/secure file for the past 7 days or so many days? johnmccarthy Linux - Newbie 5 01-04-2013 09:43 PM
[SOLVED] how to compare $var with content of file Basher52 Linux - Software 21 05-20-2011 06:06 PM
Content Filtering - using grep hiteshthappa Linux - Newbie 2 07-15-2008 04:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration