3

How can I run the same command in mac terminal for multiple files in a folder? The files are named like 24538_7#1.fq, 24538_7#2.fq, 24538_7#3.fq, 24538_7#4.fq and so on.

The commands are:

sed -n '\|@.*/1|{N;N;N;p;}' 24538_7#2.fq > 24538_7#2_paired1.fq
sed -n '\|@.*/2|{N;N;N;p;}' 24538_7#2.fq > 24538_7#2_paired2.fq

Since filename involves a counter, so, obviously the filenames need to be changed.

I was trying to write command, but I got stuck at renaming the file. My effort for the command is below:

for file in 24538_7#*.fq; do sed -n '\|@.*/1|{N;N;N;p;}' "$file" > "${file/fq/fq}"; done

(PS- I use MacOS)

  • Also if you are putting this command in a script (or if you have interactive_comments disabled) you will need to quote the # to avoid it being interpreted as a comment. – jesse_b May 31 '18 at 18:06
  • I'm not able to think how to write it so that the output file gets renamed only without interferring with my original file. Kind of this 24538_7#2.fq > 24538_7#2_paired1.fq is what I want to accomplish but on multiple files as stated earlier. – iamakhilverma May 31 '18 at 18:10
  • Not copy the files, but run the command on original files and then their output should be made with somehow different name. Can you help me @Jesse_b – iamakhilverma May 31 '18 at 18:18
  • You want to run two separate sed commands on each file, each generating it's own output file, correct? – jesse_b May 31 '18 at 18:21
  • @Jesse_b Yes, that's the plan and if not possible, then, two commands for all files. – iamakhilverma May 31 '18 at 18:25

1 Answers1

2

Assuming that all files are named similarly you can use

for file in 24538_7#*.fq; do
    sed -n '\|@.*/1|{N;N;N;p;}' "$file" > "${file%.fq}_paired1.fq"
    sed -n '\|@.*/2|{N;N;N;p;}' "$file" > "${file%.fq}_paired2.fq"
done

or (to build on what you've already tried)

for file in 24538_7#*.fq; do
    sed -n '\|@.*/1|{N;N;N;p;}' "$file" > "${file/.fq/_paired1.fq}"
    sed -n '\|@.*/2|{N;N;N;p;}' "$file" > "${file/.fq/_paired2.fq}"
done

Warning: This will only work for your specific situation and for your specific filenames.

nohillside
  • 3,251
  • Can you suggest me some platform or some book maybe, where I can learn such things? Thanks btw:) – iamakhilverma May 31 '18 at 18:28
  • 1
    @AkhilVerma man bash, https://developer.apple.com/library/content/documentation/OpenSource/Conceptual/ShellScripting/shell_scripts/shell_scripts.html, https://www.bash.academy, http://mywiki.wooledge.org/BashFAQ. And Google of course :-) – nohillside May 31 '18 at 18:31