wiki.getshifting.com

--- Sjoerd Hooft's InFormation Technology ---

User Tools

Site Tools


scriptgrepusefulllinesasmatchingpattern

Bash: Use Full Lines From Inputfile as Matching Pattern

Summary: How to use an inputfile from which the full line is used as a search pattern in other files. The other files all start with SHIFT. The inputfile lines contain whilespaces.
Date: Around 2014
Refactor: 6 April 2025: Checked links and formatting.

inputfile=/tmp/inputfile.txt

nlines=`cat $inputfile | wc -l`
teller=1
while [[ $teller -lt $nlines ]]; do
        match=`head -$teller $inputfile | tail -1`
        grep "$match" SHIFT* > /dev/null
        if [ $? -eq 0 ]; then
                echo $match still exists!
        fi
        teller=$(($teller+1))
done

Explained code:

inputfile=/tmp/inputfile.txt

# Count the number of lines in the inputfile. This is the number of times the loop will be gone through.
nlines=`cat $inputfile | wc -l`
teller=1
while [[ $teller -lt $nlines ]]; do
        # Use head and tail to set a variable with a complete line from the inputfile
        match=`head -$teller $inputfile | tail -1`
        grep "$match" SHIFT* > /dev/null
        if [ $? -eq 0 ]; then
                echo $match still exists!
        fi
        teller=$(($teller+1))
done
scriptgrepusefulllinesasmatchingpattern.txt · Last modified: by 127.0.0.1