Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

text mark-up

Status
Not open for further replies.

jupiler

Technical User
Oct 1, 2002
14
BE
Hi all,

Could s.o. help me in finding a script for the following task: given the two input-files 'list.txt' and 'text.txt', I need to have a script that looks for all the words (listed in the list.txt file) in the text.txt file and places them between §.

So, if list.txt is for instance:

cat
something moving in the garden
walking with
was feeding

And text.txt is for instance:

John was walking with Mary
John was feeding his cat
John saw something moving in the garden

The output should be:

John was §walking with§ Mary
John §was feeding§ his §cat§
John saw §something moving in the garden§

I hope that this example is clear enough. Thanks in advance,

J
 
You can try this.

script.sh :

awk '{
#Read the pattern file
substfile = "'$1'"
while( getline inputPattern < substfile ){
gsub(inputPattern,&quot;§&quot;inputPattern&quot;§&quot;,$0)
}
close(substfile)
print $0

}'

and you launch with :
script.sh list.txt < text.txt

@+
Lolo
 
The script does not work. Nothing happens. I'm using gawk. Could that be the reason why?

Cheers,

J
 
This does what you need if there is only one matching
record in line. Otherwise use gsub.
Code:
BEGIN {
    a=1 ; var = sprintf(&quot;%c&quot;, 167)
    printf &quot;List File to read from: &quot;
    getline name < &quot;-&quot;
    
        while ((getline array[a] < name) > 0) {
                a++
        }
        close(name)
     printf &quot;%d records read from %s\n&quot;, a, name
}

 {
   for (i=1 ; i <= a ; i++) {
        if (array[i] && $0 ~ array[i]) {
            sub(array[i],var array[i] var,$0)
        }
    }
    print
  }

OUTPUT from your sample:
> awk -f presto.awk scrap.txt
List File to read from: list.txt
5 records read from list.txt
John was §walking with§ Mary
John §was feeding§ his §cat§
John saw §something moving in the garden§
 
Hello,
Is the script executable ? (chmod +x script.sh)
some awk don't recognize the getline subfunction try other version of awk. Look at this file for example and check with your OS :




PS :

hey marsd, I USE gsub ;-)

@+
Lolo

John was §walking with§ Tom and saw Mary who §was feeding§ the §cat§. The §cat§ saw §something moving in the garden§ and ran away. There was an other §cat§ approching Mary's §cat§
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top