简体   繁体   中英

how to grep and write in to new file

I want to read file key value from one file and grep that value from another file(file2) and write the entire content of file2 in to file3, how can I achieve it?

file1 : 12345 file2 : abc12345 abc12456

Expected out put in file3 : abc12345

This is code I am using

while IFS= read -r LINE; do echo $LINE

grep '$LINE' $FILE2 >> $FILE3


done < $FILE1

This should read space separated keys file: "file1" to grep "file2" and if some found write content of "file2" to "file3".

#!/bin/sh

IFS=' ' read -r -a FKEYS <<< `cat ./file1`

for next_key in ${FKEYS[@]}; do
    if [ "$next_key" != "" ]; then
        RESULT=`grep $next_key ./file2`
        if [ "$RESULT" != "" ]; then
            echo $RESULT > ./file3
            exit
        fi
    fi
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM