Friday, July 7, 2017

Copy files using regular expressions

If you need to copy or move only a portion of your files based on some rule for their name, use regular expressions.

For example, these are all fastqc.zip files in a folder:

ls | grep fastqc.zip$
071_2_E7_24h_TAG_fastqc.zip
071_2_E7_72h_TAG_fastqc.zip
071_2_E8_24h_TAG_fastqc.zip
071_2_E8_72h_TAG_fastqc.zip
334_1_E7_24h_TAG_fastqc.zip
334_1_E7_72h_TAG_fastqc.zip
334_1_E8_24h_TAG_fastqc.zip
334_1_E8_72h_TAG_fastqc.zip
756_3_E7_24h_TAG_fastqc.zip
756_3_E7_72h_TAG_fastqc.zip
756_3_E8_24h_TAG_fastqc.zip
756_3_E8_72h_TAG_fastqc.zip
835_1_E7_24h_TAG_fastqc.zip
835_1_E7_72h_TAG_fastqc.zip
835_1_E8_24h_TAG_fastqc.zip
835_1_E8_72h_TAG_fastqc.zip
H1_E7_24h_TAG_fastqc.zip
H1_E7_72h_TAG_fastqc.zip
H1_E8_24h_TAG_fastqc.zip
H1_E8_72h_TAG_fastqc.zip
H7_E7_24h_TAG_fastqc.zip
H7_E7_72h_TAG_fastqc.zip
H7_E8_24h_TAG_fastqc.zip
H7_E8_72h_TAG_fastqc.zip
If you need to find files with specific number, lets say those with _1_ or _2_ in the name of the file, use the following regex:


ls | grep .*_[1-2]_.*_fastqc.zip$
071_2_E7_24h_TAG_fastqc.zip
071_2_E7_72h_TAG_fastqc.zip
071_2_E8_24h_TAG_fastqc.zip
071_2_E8_72h_TAG_fastqc.zip
334_1_E7_24h_TAG_fastqc.zip
334_1_E7_72h_TAG_fastqc.zip
334_1_E8_24h_TAG_fastqc.zip
334_1_E8_72h_TAG_fastqc.zip
835_1_E7_24h_TAG_fastqc.zip
835_1_E7_72h_TAG_fastqc.zip
835_1_E8_24h_TAG_fastqc.zip
835_1_E8_72h_TAG_fastqc.zip
Now simply to copy those files capture the output of the command:

cp $(ls | grep .*_[1-2]_.*_fastqc.zip$) test/
cd test/
ls -ltrh
total 10648
-rw-r--r--  1 milospjanic  staff   443K Jul  7 01:45 835_1_E7_72h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   439K Jul  7 01:45 835_1_E7_24h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   438K Jul  7 01:45 334_1_E8_72h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   443K Jul  7 01:45 334_1_E8_24h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   446K Jul  7 01:45 334_1_E7_72h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   436K Jul  7 01:45 334_1_E7_24h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   441K Jul  7 01:45 071_2_E8_72h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   441K Jul  7 01:45 071_2_E8_24h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   444K Jul  7 01:45 071_2_E7_72h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   438K Jul  7 01:45 071_2_E7_24h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   447K Jul  7 01:45 835_1_E8_72h_TAG_fastqc.zip
-rw-r--r--  1 milospjanic  staff   443K Jul  7 01:45 835_1_E8_24h_TAG_fastqc.zip

Wednesday, July 5, 2017

Grep extract before and after a line

If you need to extract lines before and after a line with a string you search for using grep use -B flag for before and -A for after to get lines wanted:

grep -A3 -B2 1998 libtool 
# NOTE: Changes made to this file will be lost: look at ltmain.sh.
#
# Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
# Free Software Foundation, Inc.
#
# This file is part of GNU Libtool:
If you need same number of lines before and after use -C flag

grep -C3 1998 libtool 
# Generated automatically by  (GNU  )
# NOTE: Changes made to this file will be lost: look at ltmain.sh.
#
# Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
# Free Software Foundation, Inc.
#
# This file is part of GNU Libtool: