The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Create a script TimHortons UNIX for Dummies Questions & Answers 1 01-27-2009 09:08 AM
using todays date to create a report using grep MBN Shell Programming and Scripting 3 11-02-2008 04:14 AM
create a shell script that calls another script and and an awk script magikminox Shell Programming and Scripting 0 06-26-2008 03:50 AM
can we create a GUI by shall script johnray31 Shell Programming and Scripting 1 07-24-2007 11:47 AM
how do I create an ftp script leonard905 UNIX for Dummies Questions & Answers 2 01-14-2005 06:25 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-09-2009
repinementer repinementer is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 158
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 (permalink)  
Old 05-09-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,553
wow. that's ridiculous...
describe in detail what you are doing...provide sample input files if any.
  #3 (permalink)  
Old 05-09-2009
repinementer repinementer is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 158
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 (permalink)  
Old 05-09-2009
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 536
Quote:
Originally Posted by repinementer View Post
...
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 (permalink)  
Old 05-09-2009
stateperl stateperl is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 13
Thanx

Hey Thanx for your time and replies.
I will try out now. Your codes are comparatively very simple.
Thanx to you and AWK
  #6 (permalink)  
Old 05-09-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,553
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 (permalink)  
Old 05-09-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514
Try this:

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

cheers,
Devaraj Takhellambam
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0