I had 5000 text files in a directory, each text file name starts with a prefix OG00* (ex - OG0017774.log)
The .log files which contains asterisk (*) inside the file, need to be copied into a new directory.
The content of the file -
cat OG0017774.log
M0~b904dbe442e0eb658d229076cacb9ef6 M1~9edeedcb1f4f315c4689bacd8075222f 0.000035**
M0~b904dbe442e0eb658d229076cacb9ef6 M2~aeba83b564ee32e0ef1a8321c8d930f4 0.000671**
M0~b904dbe442e0eb658d229076cacb9ef6 M3~006a376da2fba16185ce424bf4cba983 0.000055**
M0~b904dbe442e0eb658d229076cacb9ef6 M4~e564dbfbbbe8d1f7d9d8c8e4da202943 0.000015**
M0~b904dbe442e0eb658d229076cacb9ef6 M5~2abe603e8fee2fcb08b7fb818957e0aa 0.000006**
Suggestions appreciated.
I tried this code, it copies all the files in the current directory to a new directory.
I want to copy those files which had * inside the each text file.
#!/bin/bash
KEYWORD_PATTERN='*'
find . -type f |
while read FNAME
do
if grep -Ew -q "$KEYWORD_PATTERN" $FNAME
then
KEYWORD=$(grep -Ew -o "$KEYWORD_PATTERN" $FNAME)
cp -r $FNAME keywords/$KEYWORD
fi
done
mv
command. Do you get error messages too? Do you also want to give the files some special name in thekeywords
directory? If so, what should happen if you have name collisions? How should the new name be chosen? What's the reasoning behind usingfind
if you know all files matchOG00*
? – Kusalananda Aug 01 '22 at 20:54*
by itself is an invalid extended regex, and even though I tried to figure it out, I'm not even sure whatgrep -Ew '*'
does on my Debian. With-o
, it doesn't print anything anyway. So that might be a problem. I'm also not exactly sure what you expect to get with the$(grep -Ew -o "$KEYWORD_PATTERN" $FNAME)
, as if you're trying to match against a literal*
, the matched string would always be just*
. And if$KEYWORD
is*
then you really better start quoting the rest of those variable expansions. – ilkkachu Aug 01 '22 at 21:25*
on any line shall be copied ? – Paul_Pedant Aug 01 '22 at 22:44*
usingtr
(which is ignorant of patterns) andwc
:tr -cd '*' < $"{fname}" | wc -c
. Then just test for non-zero. Also note that uppercase variable names are likely to clash with those in your environment. – Paul_Pedant Aug 01 '22 at 22:51