Search code examples
bashfileiterationcontains

Bash check if file1 line is partly contained in a line from file2


I have have a file1 with ids and a file2 that is a list with full names of all files in a folder.

Ids from file1 look like this P001A, P001I, P002A, P002I ... And the names of the files from file2 contain those ids in themselves. I want to create a new file3 that contains all full names from file2 that have the ids from file1.

File2 has like 100k lines, while file1 has 89 so there are many lines from file2 that contain the same id from a line in file1.

This is the script that I am using, but it says

FILE1: command not found FILE2: command not found -bash: ${FILE1}: ambiguous redirect

  1#!/bin/sh
  2 FILE1 ="$1"
  3 FILE2 ="$2"
  4 while read -r value1
  5 do
  6     while read -r value2
  7     do
  8         if [[ "$value1" == *"$value2"* ]]
  9             then
 10             echo $value2
 11         fi
 12     done <${FILE2}
 13 done <${FILE1} > file3.list

What is wrong here? And do you know if that script is supposed to be like that or I should make some other way.


Solution

  • I solved my problem with this script

      1#!/bin/bash
      2 for i in $(cat file1);
      3 do
      4    FILENAME=$(find /directory/ -regextype posix-egrep -regex ".*/20170001${i}[0-9]*\.wav")
      5    echo "${FILENAME}";
      6 done > file3
    

    I didn't even need the file with the file names.