Showing posts with label for-loop. Show all posts
Showing posts with label for-loop. Show all posts

Monday, July 18, 2016

Grep for first occurrence for multiple strings

Inspired by a post from stackexchange. I posted this as an answer there too.


 
$ cat somelog.log
ADWN    1259    11:00   B23
ADWN    3009    12:00   B19
DDWN     723    11:30   B04
ADWN    1589    14:20   B12
ADWN    1259    11:10   B23
DDWN    2534    13:00   B16
ADWN    3009    11:50   B14



Using "for" loop and "grep" we can do the following:

 
for i in $ (cut -d " " -f1 somelog.log | sort -u); do LC_ALL=C fgrep -m1 "$i" somelog.log; done 
 

Explanation:

$ cut -d " " -f1 somelog.log | sort -u # will result in unique identifiers
 
ADWN
DDWN
 
$ LC_ALL=C fgrep -m1 "$i" somelog.log # "m" option in grep will match the first pattern
 
1) for loop matches each pattern using grep and picks the first pattern. 
2) LC_ALL=C is used to make search faster 
3) fgrep is to match fixed strings instead of regular expressions. 

Thursday, July 7, 2016

Rename the fasta file with the first sequence header

Suppose there are multiple fasta files with single sequence. We want to rename the fasta file with the header of the single sequence present in the fasta file. We can run the following command.

Place all the fasta files you want to change the file names in one directory and run the following command.


for i in *; do if [ ! -f $i ]; then echo "skipping $i"; else newname=`head -1 $i | sed 's/^\s*\([a-zA-Z0-9]\+\).*$/\1/'`; [ -n "$newname" ] ; mv -i $i $newname.fasta || echo "error at: $i"; fi; done | rename s/ // *.fasta