Find the answer to your Linux question:
Results 1 to 3 of 3
I'm tring to create a bash script that removes text entries from text file 1 that are in file 2 text 1 Code: abc def ghi text 2 Code: abc ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! pouar's Avatar
    Join Date
    Dec 2011
    Posts
    18

    filtering strings from text files


    I'm tring to create a bash script that removes text entries from text file 1
    that are in file 2

    text 1
    Code:
    abc
    def
    ghi
    text 2
    Code:
    abc
    ghi
    my bash script so far
    Code:
    #!/bin/bash
    cd /home/pouar/test
    index=0
    # read text 1
    while read line ; do
    	a[$index]="$line" 
    	index=$(($index+1))
    done < ./1
    
    index=0
    # read text 2
    while read line ; do
    	b[$index]="$line" 
    	index=$(($index+1))
    done < ./2
    n=$((0))
    m=$((0))
    x=${#a[*]}
    while ((n<x))
    do
    if [ "${a[$n]}"="${b[$m]}" ]; then #if string in text 1 is equal to string in text 2
    n=$(($n+1)); #go to next line in text 1
    m=$(($m+1)) #go to next line in text 2
    else # or else
    echo "${a[(($n))]}" #print string
    n=$(($n+1)) #go to next line in text 1
    fi
    done

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,391
    grep can read patterns from a file, that should do it.
    Code:
    man grep
    You must always face the curtain with a bow.

  3. #3
    Just Joined! pouar's Avatar
    Join Date
    Dec 2011
    Posts
    18
    thanks it worked

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •