grep - from TextWrangler to shell

3 replies [Last post]
User offline. Last seen 5 years 8 weeks ago. Offline
Joined: 11/28/2012

Hi Steve and Casey
I am learning a lot from your book. Thank you.

I have 12 files to rename. In the future may be lots more.

to C1_R1_1.fq, C1_R1_2.fq, etc.
As shown on page 96 it is easy enough to create a script using mv command.

However, to be more general I was able to do this in TextWrangler:

How do I translate this information to create a running shell script from the terminal?
I am using iMac OS X Lion 10.7.4

User offline. Last seen 3 years 45 weeks ago. Offline
Joined: 08/15/2010
Bulk file rename

Hi Valerca --
The most efficient way to do this uses some fancy bash wildcards/substitutions.
It is probably safer to copy the files rather than move (rename) them, as well, so you won't erase your original files if there are any errors or typos.
This command will do the trick:

for F in GSM*.fq; do cp $F ${F#*_}; done

The part in curly brackets is confusing, but just means "the name without everything before the first underscore". Look at page 315 for an example that uses the opposite: "without everything after the last period". The variable F is a placeholder for the each file as you go through the loop.

You can convert this into a function using the syntax on page 314:

for F in $@
        # echo $F ${F#*_}
        cp $F ${F#*_}

Then you would run it using the command
myrename GSM*.fq

and the list of files matching GSM*.fq (or any other wildcard pattern) would be sent into the program in place of $@. (I have put GSM* here so that if you run it again on a directory with previously-renamed files, it won't re-rename them to R1_1.fq!)

I put a commented-out line with "echo" in it. If you uncomment that and comment the "cp" line, you can test your renaming function and it will just print what the names of the files will be before and after, without copying them.

Add those lines defining the function to your .bash_profile, and you can use your rename function any time you open a new terminal. If you are working on a copy of files (and the originals will be preserved in case of accident) you can put mv in place of cp to rename instead of copy.

After reading the shell chapters 4-6, you could skip ahead and read Chapter 16 for some more shell tips and tricks. The part is not as dependent on the content of the previous chapters.

Good luck!

User offline. Last seen 5 years 8 weeks ago. Offline
Joined: 11/28/2012
Bulk file rename

That works. Thanks for responding and taking the time to give a clear explanation.
I see that I was on the wrong path and I have to learn those funny symbols to work in the shell.
So the grep type commands and regex in chapters 2 and 3 can be used only in text editors?
It is hard to open vary large files in text editors.

User offline. Last seen 3 years 45 weeks ago. Offline
Joined: 08/15/2010
Grep usefulness

(See the reply to your other comment: in short, regexps are useful even w/o a text editor... The simplest solution to your question seemed to not require them....)