I have 135 documents stored as 135 lines (So each line is a long text) in File_A and I have 15 phrases in File_B. I need to extract a sentence and its before from File_A with a matching phrase in File_B. The extracted sentences from File_A-Line_1 should be output to a new file File_1. Similarly the extracted sentences from File_A-Line_2 should be output to a new file File_2 and so on till i extract matching sentences from all the lines. I did this with the following code
i=1
while read line; do
while read row; do
cat "$line" | sed 's/\./.\n/g' | grep -i -B 1 "$row" | tr -d '\n' | sed 's/--/\n/g' >> file_$i
done < $2
$i = $i+1;
done < $1
The problem here is, the output is being printed on to the console but not to the new file. Could some one help me in realizing my error.
Thank you
Is this clear? If not, comment on it, and I will edit it. Bash Output Redirection Example:
echo "some text" >file.txt;
#here we add on to the end of the file instead of overwriting the file
echo "some additional text" >>file.txt;
#put something in two files and output it
echo "two files and console" | tee file1.txt | tee file2.txt;
#put something in two files and output nothing
echo "just two files" | tee file1.txt >file2.txt;
tee
actually accepts multiple file arguments, it is thus as simple as:
# from file
tee 1.txt 2.txt 3.txt <0.txt
# from string
tee 1.txt 2.txt 3.txt <<<'text'
# from heredoc
tee 1.txt 2.txt 3.txt <<'EOF'
line
line
line
EOF
# from pipeline
command | tee 1.txt 2.txt 3.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With