Loop grep, outputs in files

Hi, I try to create a simply code but I have problems...

Ubuntum, Bash version: 4.3.46 Bash
INPUT file (csv):
TS133_29BC,xx2274305
TS133_29BC,rps4576240
av137_37BC,wfs2274305
av137_37BC,ftrs4576240
T1S138_30BC,rfss2255526
T1S138_30BC,rgas2274305
OUTPUT files (csv):
File TS133_29BC
TS133_29BC,xx2274305
TS133_29BC,rps4576240

File av137_37BC
av137_37BC,wfs2274305
av137_37BC,ftrs4576240

File T1S138_30BC
T1S138_30BC,rfss2255526
T1S138_30BC,rgas2274305

My code is:

for ((b=$1; b<=96; b++))

    do

dddd=*"_"${b}"BC"

grep -w "$dddd" > $dddd

    done

find . -size 0 -delete

Where is the error?

for((b=$1 ...

$1 is not the numeral one, but the script's first commandline parameter. Try it without the dollar sign.

Are you trying to simultaneously read from and write to the same file? That won't work, that will destroy your data.

WHAT is the error?

Some comments:

  • There is no file that grep can work upon.
  • In regexes, the star * is a repetition indicator for the preceding atom ( man regex ) which you do not specify when defining dddd .
  • You seem to grep for a structure like _nnBC . Will nn always be two digits, also below 10, e.g. 03? Then, extra care must be taken. Or do you start from the first positional parameter ( $1 )?

Hi!

I delete $ ... from $1 to 1
I add the input file name (inputfile)

new version of the code...

for ((b=1; b<=96; b++))      do  dddd=*"_"${b}"BC"  grep -w "$dddd" inputfile > $dddd      done

The variable "b" start from 1 and finisch to 96 ... no 01,02...

I take a look in the "man regex" and I found that:

$ man regex

REG_BADRPT
              Invalid use of repetition operators such as using '*' as the first character.

How can I build my variable dddd?

dddd="_"${b}"BC :doesn't work... How can I replace "" with an other simbol with the same function...?

I found:

. Matches any single character
? The preceding item is optional and will be matched, at most, once
* The preceding item will be matched zero or more times.

My variable would also be:

dddd=....."_"${b}"BC"

... ehm!?

I'm confuse!

You don't need the * . As the regex is not anchored (at begin-of-line), grep will match anywhere in the string. For the leading zeroes, try either a printf "%02d" , or for b in {01..96} (recent shells only).

Another bash-4 construct gives you two digit values

echo {01..96}
for b in {01..96}
do
  ...
done

Maybe you want to cycle through all the numbers in the file?
And is it sorted?
Then consider this one

while IFS=, read left right
do
  if [[ $left == *[0-9][0-9]BC ]]
  then
    if [[ $left != $prev ]]
    then
      exec 3> "$left".csv
      prev=$left
    fi
    echo "$left,$right" >&3
  fi
done < inputfile.csv
1 Like

Ohhhhhhhhhhhhhhhh Yessssssssssssssssssssss !!!

It works !!!

Many thanks at all !!!

1 Like