LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 05-31-2017, 04:05 AM   #1
wedtorque
LQ Newbie
 
Registered: Apr 2017
Posts: 6

Rep: Reputation: Disabled
Question find the pattern in a file where both pattern and file have differnt number of lines and space


I have a variable that store a string.I am taking a worst case here

Code:
 var='this is 
    my 
          name'
how to find whether this variable is present in a file.txt or not

This is my file.txt

Code:
hi all
    this 
           is my 
              name
    but no one likes it
What i have tried

Code:
if grep -Fwq "$var" file.txt 
    then
    echo "Duplicate"
    fi
but this work only when both var and file.txt have common space and newline format
 
Old 05-31-2017, 06:32 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,604

Rep: Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604
Quote:
Originally Posted by wedtorque View Post
I have a variable that store a string.I am taking a worst case here

Code:
 var='this is 
    my 
          name'
which language is it at all?

Quote:
Originally Posted by wedtorque View Post
how to find whether this variable is present in a file.txt or not

This is my file.txt

Code:
hi all
    this 
           is my 
              name
    but no one likes it
I would read that var into a local variable and read that file into another one (keeping spaces, newline, whatever) and now I only need to check if var2 contains var2.
That can be easily solved using perl/python/c/whatever, probably awk too.
Quote:
Originally Posted by wedtorque View Post
but this work only when both var and file.txt have common space and newline format
I do not know if you want to preserve those spaces or what to do with them?
 
Old 05-31-2017, 09:02 AM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
are you just trying to find it or fix it??

to find it I figured this out --
Code:
#!/bin/bash


subString="this is my name"

while read FILE
do
{

FILE2="$(awk '{printf("%s",$0)}' | fmt -u)"

echo "FILE2: $FILE2"


if [[ "$FILE2" =~ "$subString" ]] ;
then

echo "got it"

fi

}
done <matchTestFIle
test file is yours.
Code:
hi all
    this
           is my
              name
    but no one likes it
results are this
Code:
userx%slackwhere ⚡ testing ⚡> ./findmatches 
FILE2:     this is my name but no one likes it
got it
userx%slackwhere ⚡ testing ⚡>
 
  


Reply

Tags
awk regex, grep forloop, sed bash


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
[SOLVED] How to delete everything from a file, once a particular pattern is found, including the pattern ansh007 Linux - General 1 03-07-2017 08:15 AM
[SOLVED] Search File For Each Occurrence of Pattern Less Than X Lines Apart RandyTech Linux - Newbie 20 10-06-2016 01:25 AM
[SOLVED] Need help inserting 2 lines into a file following a specific pattern rmori Linux - Newbie 6 10-06-2014 08:23 PM
[SOLVED] Finding largest file number in the pattern rpd25 Linux - Newbie 6 08-04-2013 12:27 PM
sed: delete lines after last occurrence of a pattern in a file zugvogel Programming 4 11-17-2009 01:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:28 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