Using the field splitting function of awk. This assumes that formatting is the same for all files. The advantage is that you do not need any regex that alters the fields directly (like deleting numbers or characters) but you simply split them by their common delimiter _ and then select those you want to build the final file name.
for i in *.fastq
do
mv $i $(echo $i | awk '{split($1,a,/_/); print a[3]"_"a[5]"_"a[6]"_"a[7]}')
done
Lots of solution from others work, but I'd like to recommend a safer solution
of mine (brename),
in case you overwrite files with others by accident, which is common in batch renaming files using regular expression.
brename checks all operations before execution for safety.
$ brename --include-filters '.fastq$' --ignore-ext \
-p 'GEN191010_N_(.+?_).+(R[12])' -r '$1$2' --dry-run
[INFO] main options:
[INFO] ignore case: false
[INFO] search pattern: GEN191010_N_(.+?_).+(R[12])
[INFO] include filters: .fastq$
[INFO] search paths: ./
[INFO]
[INFO] checking: [ ok ] 'GEN191010_N_NBS0_lib94256_1700_1_R1.fastq' -> 'NBS0_R1.fastq'
[INFO] checking: [ ok ] 'GEN191010_N_NBS0_lib94256_1700_1_R2.fastq' -> 'NBS0_R2.fastq'
[INFO] checking: [ ok ] 'GEN191010_N_NBXBS10_lib94257_1700_1_R1.fastq' -> 'NBXBS10_R1.fastq'
[INFO] checking: [ ok ] 'GEN191010_N_NBXBS10_lib94257_1700_1_R2.fastq' -> 'NBXBS10_R2.fastq'
[INFO] 4 path(s) to be renamed
Below is a shell script that replaces defined strings inside a group of files with the same extension. It is probably an overkill in your case since you can simply enter 4 mv commands instead of 2 needed with this script. First save the script as fix-name.com and make it executable (chmod +x fix-name.com). You also need to have a (t)csh installed, which I guess is not a given these days. I am sure someone will come up with a better bash script in no time.
Hi @Ole - I thought the Q() would allow the recipe to work even if filename had whitespace in them but I guess that was unneeded over-protection... yes?
helloooooooo newbie. What have you tried ?
We need to know more about your files. Are the strings you want removed the same in all cases as this suggests?
If not, do they follow a regular pattern?
Thank you all for your suggestions. I used that of Mensur and it work just fine. Much appreciated.