LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 09-10-2020, 12:28 PM   #1
mierdatuti
Member
 
Registered: Aug 2008
Posts: 64

Rep: Reputation: 15
how to check if I find a pattern with sed in loop


Hi,
I have these code, to find values of a array in a file. The script only search in some lines number that the are in other file

Code:
  


while read -r Line; do
        
        echo "Line $Line"


      for i in "${patrones[@]}"
          do
        


            if [[ -n $(sed "${Line}!d;/'"${i}"'/I!d" $rutaFich/titles) ]]; then
                    echo "find it pattern $i en la linea $Line"
                   exit 0
            else
                   echo "No encontrado el patron $i en linea $Line"
             fi

       done

done < $rutaFich/differences
But not works. If find a pattern always goes to the else clause.
Any help please?
Thanks
 
Old 09-10-2020, 12:32 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
since we have no any idea about the content of $rutaFich/differences or "${patrones[@]}" or $rutaFich/titles hard to say anything.
would be also nice to know what should be the condition to that if.
 
Old 09-10-2020, 03:16 PM   #3
mierdatuti
Member
 
Registered: Aug 2008
Posts: 64

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by pan64 View Post
since we have no any idea about the content of $rutaFich/differences or "${patrones[@]}" or $rutaFich/titles hard to say anything.
would be also nice to know what should be the condition to that if.
Sorry. I'm trying to explain better.

$rutaFich/differences have line number where I search with sed (I get these line numbers of other code). For example

cat $rutaFich/differences
4

"${patrones[@]}" is array with all paterns that I must find with sed

Titles is text file where I must find (in these example at line 4).

So, in this example I must find in line number 4, all patterns of my array and if I find some pattern I say : "find it pattern $i en la linea $Line"


Thanks!
 
Old 09-10-2020, 03:57 PM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
This command
Code:
sed "${Line}!d;/'"${i}"'/I!d"
first outputs line $Line, then outputs lines that contain $i enclosed in single quotes. E.g. if $i has the value mypattern, the line is printed if it contains 'mypattern', with quote characters. Is that the intention?

A potential problem is the fact that ! is a special character. You should escape it with a backslash \!.

A simpler(?) solution:
Code:
sed -n ${Line}p $rutaFich/titles | grep -i "'$i'"
or, if the single quotes are not desired
Code:
sed -n ${Line}p $rutaFich/titles | grep -i "$i"
and to test for presence
Code:
if sed -n ${Line}p $rutaFich/titles | grep -i -q "'$i'"
then echo pattern \'$i\' is in line $Line
else echo not found
fi

Last edited by berndbausch; 09-10-2020 at 04:05 PM.
 
Old 09-11-2020, 02:45 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
yes, you can try to print the result of both:
Code:
sed "${Line}!d" $rutaFich/titles
sed "/'"${i}"'/I!d" $rutaFich/titles
to see what are they doing exactly. I would say the two sed commands will delete everything, most probably because of the pattern or the second sed is not exactly what you want (see post #4).
From the other hand running the first sed in a loop is just waste of time and cpu, but probably it is not that important in your case.

For readability probably I would use awk
Code:
while read -r Line; do
        
        echo "Line $Line"


      for i in "${patrones[@]}"
          do
            found=$(awk -v Line=$Line -v pattern="$i" ' NR != Line { next } $0~pattern ' $rutaFich/titles)
            if [[ -n $found ]]; then
                    echo "find it pattern $i en la linea $Line"
                   exit 0
            else
                   echo "No encontrado el patron $i en linea $Line"
             fi

       done

done < $rutaFich/differences
Not tested, just an idea. It will still read the file $rutaFich/titles in loop, so still better:
Code:
while read -r Line; do
        
        echo "Line $Line"
        data="$(sed -n ${Line}p $rutaFich/titles)"

      for i in "${patrones[@]}"
          do
            if [[ $data =~ $i ]]; then
                    echo "find it pattern $i en la linea $Line"
                   exit 0
            else
                   echo "No encontrado el patron $i en linea $Line"
             fi

       done

done < $rutaFich/differences
or something similar
 
  


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
[SOLVED] Loop , use sed to remove and update loop final count pedropt Programming 9 10-16-2019 12:52 PM
sed delete lines with pattern to pattern (exluding the second) Jykke Linux - Software 10 07-23-2018 02:43 AM
find the pattern in a file where both pattern and file have differnt number of lines and space wedtorque Linux - Newbie 2 05-31-2017 09:02 AM
[SOLVED] use sed to find string pattern and delete subsequent characters jigg_fly Programming 16 10-10-2013 08:34 AM
[SOLVED] sed: Find pattern and delete 5 lines after it supersoni3 Programming 4 03-24-2010 07:00 AM

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

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