LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-01-2016, 10:21 AM   #1
MrLinuxDonnelly
LQ Newbie
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 23

Rep: Reputation: Disabled
fgrep of string not working


Hey,

So once again im looking for the help of the expression guru's

Ive got a string in a file i wish to see exists in another file

so an example of the string im looking for looks like the below :

<alias id="10.10.10.10/25600" alias="10.10.10.10/24000" />

ive tried numerous times to get this right
- escaped the additional " in the string

i believe it has something to do with the space between the 2 parts as if i simply look for either section i can find the entry without a problem but combined the 2 of them and its a no go.

Any help would be appreciated and workings so i can take something away rather than just see the end result
 
Old 02-01-2016, 10:37 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Xubuntu, Manjaro
Posts: 745
Blog Entries: 3

Rep: Reputation: 347Reputation: 347Reputation: 347Reputation: 347
Hi!

No problems in my end, but I (almos always) use egrep, like so:
Code:
$ echo "foo 
<alias id=\"10.10.10.10/25600\" alias=\"10.10.10.10/24000\" />
bar" | egrep '<alias id="10.10.10.10/25600" alias="10.10.10.10/24000" />'
<alias id="10.10.10.10/25600" alias="10.10.10.10/24000" />
Best regards,
HMW
 
Old 02-01-2016, 10:46 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,253

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
As the example is from a file I see why we need fgrep instead, but it seams to work just fine for me:
Code:
$ cat file_with_line
<alias id="10.10.10.10/25600" alias="10.10.10.10/24000" />
$ cat file_with_data
foo 
<alias id="10.10.10.10/25600" alias="10.10.10.10/24000" />
bar
$ grep -f file_with_line file_with_data
<alias id="10.10.10.10/25600" alias="10.10.10.10/24000" />
Is it possible that between the 2 files there is perhaps more whitespace in one than the other?
 
Old 02-02-2016, 03:05 AM   #4
MrLinuxDonnelly
LQ Newbie
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 23

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
As the example is from a file I see why we need fgrep instead, but it seams to work just fine for me:
Code:
$ cat file_with_line
<alias id="10.10.10.10/25600" alias="10.10.10.10/24000" />
$ cat file_with_data
foo 
<alias id="10.10.10.10/25600" alias="10.10.10.10/24000" />
bar
$ grep -f file_with_line file_with_data
<alias id="10.10.10.10/25600" alias="10.10.10.10/24000" />
Is it possible that between the 2 files there is perhaps more whitespace in one than the other?


So yes it appears when i copied into this forum it actually removed the amount of white spaces between the 2 entries.

So it should actually look like the below

[rdonnelly]$ head test.txt
<alias id="172.23.4.130/25600" alias="172.23.4.130/24000" /> ( hopefully forum doesn't remove the white spaces )

I think the problem is the fact there is white spaces before the entry and then white space's between the entry.

Is there a way not to care how many white spaces are between the entries ? i tried the usual .* but it doesnt like it

Last edited by MrLinuxDonnelly; 02-02-2016 at 03:07 AM.
 
Old 02-02-2016, 03:29 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,122

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
you should use [code]here comes your sample[/code] to keep formatting. fgrep does not understand .*, you need to use grep or egrep. Usually additional spaces before and/or after the string does not influence grep at all.
So would be nice to see a much better example what have you tried exactly, what went wrong....
 
Old 02-02-2016, 04:43 AM   #6
MrLinuxDonnelly
LQ Newbie
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 23

Original Poster
Rep: Reputation: Disabled
ahh i found it, was due to some lines having a space and some being tab separated so when i was doing the grep for some reason it was picking up the ^I.

basically file formatting was bad.

Thanks all for your help
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
fgrep help please bloodstreetboy Linux - Newbie 5 04-22-2013 01:08 AM
[SOLVED] fgrep pipes rahularjun86 Linux - Newbie 4 12-20-2011 05:26 PM
[SOLVED] Using sed to remove text in fgrep string manwithaplan Linux - General 6 10-11-2009 02:00 AM
fgrep error pmelnikov General 1 04-16-2009 11:17 AM
fgrep or find command question. swamprat Linux - Newbie 2 02-13-2008 02:26 PM


All times are GMT -5. The time now is 12:47 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration