Create a script for grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create a script for grep
# 1  
Old 05-09-2009
Create a script for grep

Could any one know how to write a perl script for multiple grep functions like the following. Help would be appreciated.

Quote:
grep -w chr1 BED1 >>f1.txt | grep –w chr2 BED1 >>f2.txt | grep -w chr3 BED1 >>f3.txt | grep -w chr4 BED1 >>f4.txt | grep -w chr5 BED1 >>f5.txt | grep -w chr6 BED1 >>f6.txt | grep -w chr7 BED1 >>f7.txt | grep -w chr8 BED1 >>f8.txt | grep -w chr9 BED1 >>f9.txt | grep -w chr10 BED1 >>f10.txt | grep -w chr11 BED1 >>f11.txt | grep -w chr12 BED1 >>f12.txt | grep -w chr13 BED1 >>f13.txt | grep -w chr14 BED1 >>f14.txt | grep -w chr15 BED1 >>f15.txt | grep -w chr16 BED1 >>f16.txt | grep -w chr17 BED1 >>f17.txt | grep -w chr18 BED1 >>f19.txt | grep -w chr20 BED1 >>f20.txt | grep -w chr21 BED1 >>f21.txt | grep -w chr22 BED1 >>f22.txt | grep -w chrX BED1 >>fX.txt | grep -w chrY BED1 >>fY.txt
# 2  
Old 05-09-2009
wow. that's ridiculous...
describe in detail what you are doing...provide sample input files if any.
# 3  
Old 05-09-2009
Atually it's easy
I'm grepping specific lines contain specic words like chr1, chr2......chrx and chr y from a single file by using the same grep command multiple times with the help of a pipe symbol.

input file
chr1 tab 19u384763
chr1 tab 24275y745
chr21 tab 2527855235
output
all chr1 into 0ne file and all chr2 to another vice versa
# 4  
Old 05-09-2009
Quote:
Originally Posted by repinementer
...
output
all chr1 into 0ne file and all chr2 to another vice versa
Something like this:

Code:
$
$ cat input.txt
chr1 tab 19u384763
chr21 tab 2527855236
chr1 tab 24275y745
chr21 tab 2527855235
chrX tab 273849202
chrX tab 3893402u7
$
$
$ sort -k1,1 input.txt | perl -ne '{split;
  if ($_[0] eq $lastread) {$lastread=$_[0]; print OUT $_}
  else {$lastread=$_[0]; $_[0] =~ s/^chr//; open(OUT, ">>f$_[0].txt"); print OUT $_;}
}'
$
$ cat f1.txt
chr1 tab 19u384763
chr1 tab 24275y745
$
$ cat f21.txt
chr21 tab 2527855235
chr21 tab 2527855236
$
$ cat fX.txt
chrX tab 273849202
chrX tab 3893402u7
$
$

tyler_durden

________________________________________________________________________
"With a gun barrel between your teeth, you speak only in vowels."
# 5  
Old 05-09-2009
Thanx

Hey Thanx for your time and replies.
I will try out now. Your codes are comparatively very simple.
Thanx to you and AWKSmilieSmilie
# 6  
Old 05-09-2009
if your file is sorted and Perl is not a must to use
Code:
awk 'p!=$1{i++}{p=$1;print $0>"file_"i}' file

# 7  
Old 05-09-2009
Try this:

Code:
sort -k 1,1 file| awk '{s=substr($1,4,length($1));print $0 > "f"s}'


cheers,
Devaraj Takhellambam
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to create runtime variables based on the number of parameters passed in the script

Hi All, I have a script which intends to create as many variables at runtime, as the number of parameters passed to it. The script needs to save these parameter values in the variables created and print them abc.sh ---------- export Numbr_Parms=$# export a=1 while do export... (3 Replies)
Discussion started by: dev.devil.1983
3 Replies

2. Programming

CGI Perl script to execute bash script- unable to create folder

Hi I have a bash script which takes parameters sh /tmp/gdg.sh -b BASE-NAME -n 1 -s /source/data -p /dest/data/archive -m ARC gdg.sh will scan the /source/data and will move the contents to /dest/data/archive after passing through some filters. Its working superb from bash I have... (0 Replies)
Discussion started by: rakeshkumar
0 Replies

3. Shell Programming and Scripting

Help Create dynamic ksh script from a script

I am currently running 2 scripts to gather data for a 3rd script and would like to combine the 2 scripts into one. Having issues with the final output format. Note cannot post URL so replaced the http stuff with (name) in the examples All scripts contain #!/bin/ksh OS = Red Hat Enterprise... (0 Replies)
Discussion started by: pcpinkerton
0 Replies

4. Shell Programming and Scripting

Direction to create website to process grep and SED requests

hi I am seeking to create a cgi-bin type creation that will allow users browsing the site to be able to run searches that would be a grep command or SED in the backround. I am not sure how to go about this, if you would give me a pointer or direction about what technology i could inform myself... (0 Replies)
Discussion started by: cdc01
0 Replies

5. Homework & Coursework Questions

Create script to add user and create directory

first off let me introduce myself. My name is Eric and I am new to linux, I am taking an advanced linux administration class and we are tasked with creating a script to add new users that anyone can run, has to check for the existence of a directory. if the directory does not exist then it has... (12 Replies)
Discussion started by: pbhound
12 Replies

6. Shell Programming and Scripting

script use min resource ( grep grep)

Hi i wrote script use it as watchdog ( i mean it check another program (pooya) whenever that was killed (closed or crashed) it run another script (pooya_start.sh) to start it, this script work fine and do the job for me , i need help of an expert to tell me (exact command) how to change this... (8 Replies)
Discussion started by: pooyair
8 Replies

7. Shell Programming and Scripting

Bash: create a report with grep output?

Greetings. I need to generate a simple report via Bash (Korn?) with this raw data Test_Version=V2.5.2 Test_Version=V2.6.3 Test_Version=V2.4.7 Test_Version=V2.5.2 Test_Version=V2.5.2 Test_Version=V2.5.1 Test_Version=V2.5.0 Test_Version=V2.3.9 ... (3 Replies)
Discussion started by: alan
3 Replies

8. UNIX for Dummies Questions & Answers

Create a script

How to I put my find command string into a script. It is currently to long to be entered manually at command line. for FNAME in `find /unixsxxx/interface/x.x/xxxxxx -type f \( -name '*.KSH' -o -name '*.sh' -o -name '*.sql' -o -name '*.ksh' \) -exec grep -il xxx.xxx.xxx.xxx {} \;`; do C=`grep -c... (1 Reply)
Discussion started by: TimHortons
1 Replies

9. Shell Programming and Scripting

using todays date to create a report using grep

What i'm trying to do is to use grep to search through a few files for a selected daemon and only report on today's date. I think I got it sorted apart from in the txt file the date has 2 gaps between the month and the day, and the way I have the date format only puts in one gap any help to get... (3 Replies)
Discussion started by: MBN
3 Replies

10. Shell Programming and Scripting

create a shell script that calls another script and and an awk script

Hi guys I have a shell script that executes sql statemets and sends the output to a file.the script takes in parameters executes sql and sends the result to an output file. #!/bin/sh echo " $2 $3 $4 $5 $6 $7 isql -w400 -U$2 -S$5 -P$3 << xxx use $4 go print"**Changes to the table... (0 Replies)
Discussion started by: magikminox
0 Replies
Login or Register to Ask a Question