Region between lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Region between lines
# 1  
Old 10-17-2012
Region between lines

How can I find the regions between specific lines?

I have a file which contains lines like this:

Code:
chr1	0	17388	0	
chr1	17388	17444	1	
chr1	17444	17599	2	
chr1	17599	17601	1	
chr1	17601	569791	0	
chr1	569791	569795	1	
chr1	569795	569808	2	
chr1	569808	569890	3	
chr1	569890	570047	4	
chr1	570047	570060	3	
chr1	570060	570061	2	
chr1	570061	713956	0

column 2 describes start, column 3 end and column 4 a value.
I want to print al lines between the lines that have a zero value PLUS have a 4 in their column 4. And only print the first start and last end.
So for this example, I would print out:

Code:
chr1    569791    570061

Because this is the only region which is between two zeros and does contain a 4.

Last edited by Scott; 10-17-2012 at 04:27 PM.. Reason: Code tags, please...
# 2  
Old 10-17-2012
Try this:
Code:
awk '$4==0{$0=""}{$1=$1}1' file | awk -vRS= '{for (i=4;i<=NF;i+=4) if ($i==4) print $1,$2,$(NF-1)}'

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 10-17-2012
Code:
awk '
rd>1 && $4 == 0 {
  if (ev==4) print col,bv,$2;
}
rd==1 { bv=$2; rd++; }
rd>1 {
  if ($4>ev) ev=$4;
}
$2 > 0 && $4==0 {
  ev=$4;
  col=$1;
  rd=1;
}
' infile

# 4  
Old 10-17-2012
Hi, Thanks so much! That works exactly how it should. Could you explain your code?
# 5  
Old 10-17-2012
Quote:
Originally Posted by bartus11
Try this:
Code:
awk '$4==0{$0=""}{$1=$1}1' file | awk -vRS= '{for (i=4;i<=NF;i+=4) if ($i==4) print $1,$2,$(NF-1)}'

nicely done, bartus11 - thanks!
# 6  
Old 10-17-2012
Quote:
Originally Posted by linseyr
Hi, Thanks so much! That works exactly how it should. Could you explain your code?
awk '$4==0{$0=""}{$1=$1}1' file replace lines with zero in fourth field with empty lines
awk -vRS= process the file using paragraphs (lines enclosed between empty lines)
'{for (i=4;i<=NF;i+=4) if ($i==4) print $1,$2,$(NF-1)}' check every fourth field of each paragraph (fourth field of each line) and if it equals "4", then print first, second and second to last field from that paragraph.

---------- Post updated at 02:55 PM ---------- Previous update was at 02:54 PM ----------

Quote:
Originally Posted by vgersh99
nicely done, bartus11 - thanks!
Thanks Smilie
# 7  
Old 10-17-2012
Thanks Bartus11!

Because Im new with this, I would like to know what the code of rdrtx1 does since that one actually does something different.

Could somebody explains his code?

Thanks1
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Mean score value by ID over a defined genomic region

Hi, I would like to know how can I get a mean score value by ID over a defined genomic region. Here it is an example: file1 12 100 103 id1 12 110 112 id1 12 200 203 id2 file2 12 100 101 1 12 101 102 0.8 12 102 103 0.7 12 110 111 2.5 12 111 112 2.8 12 200 201 10.1 12 201 202... (7 Replies)
Discussion started by: fadista
7 Replies

2. Programming

Merge two strings by overlapped region

Hello, I am trying to concatenate two strings by merging the overlapped region. E.g. Seq1=ACGTGCCC Seq2=CCCCCGTGTGTGT Seq_merged=ACGTGCCCCCGTGTGTGTFunction strcat(char *dest, char *src) appends the src string to the dest string, ignoring the overlapped parts (prefix of src and suffix of dest).... (30 Replies)
Discussion started by: yifangt
30 Replies

3. Shell Programming and Scripting

Need a command to change the port region

portsuf=25 port=20925 I need to replace 09 with 25 It should be like 22525. Can some please help with command or script. (4 Replies)
Discussion started by: bhas85
4 Replies

4. Shell Programming and Scripting

Help with underline text based on specific region

Input file 2 5 ASFGEWTEWRQWEQ 10 20 QEWIORUEIOUEWORUQWEQWRQRQWGQWGFQ 1 6 WRQTQWTQTQWTQT Desired output file 2 5 ASFGEWTEWRQWEQ 10 20 QEWIORUEIOUEWORUQWEQWRQRQWGQWGFQ 1 6 WRQTQWTQTQWTQT Column 1 is the start region of underline the text in column 3; Column 2 is the end region of... (13 Replies)
Discussion started by: cpp_beginner
13 Replies

5. AIX

Change lv REGION in HDISK1

Dears my rootvg is missed up i can not extend the /opt as soon as i try to extend the Filesystem its give me that there is not enough space . as there any way to change the REGION of the LVs in HDISK1 ? lspv -p hdisk0 hdisk0: PP RANGE STATE REGION LV NAME TYPE ... (8 Replies)
Discussion started by: thecobra151
8 Replies

6. Programming

Single semare critical region problem???

Hi guys, I hope everybody is doing fine. I have written this small program which solves the critical region problem. Only on of the two threads can make changes to a common variable called counter. I am using two semaphores, is it possible to write the same program using only one semaphore? Here... (0 Replies)
Discussion started by: gabam
0 Replies

7. UNIX for Advanced & Expert Users

Best practice - determining what region you are on

Hello all, I have a question about what you think the best practice is to determine what region you are running on when you have a system setup with a DEV/TEST, QA, and PROD regions running the same scripts in all. So, when you run in DEV, you have a different directory structure, and you... (4 Replies)
Discussion started by: Rediranch
4 Replies

8. Shell Programming and Scripting

Can sed perform editing operations ONLY in the matched region?

Hi: Let's suppose I want to replace all the | by > ONLY when | is between . Usually (and it works) I would do something like sed -e 's/\(\*\)|\(*\]\)/\1>\2/g' where I have to "save" some portions of the matched region and use them with the \n metacharacter. I was wondering if I could... (2 Replies)
Discussion started by: islegmar
2 Replies

9. Solaris

How can i take private region backup in veritas

Hello experts, I am using Veritas Volume Manager 5.0. How can i take private region backup and restoration. thanks in advance... (3 Replies)
Discussion started by: younus_syed
3 Replies

10. UNIX for Advanced & Expert Users

stack region

how can i determine that what percentage of stack region is currently is used? (i am using tru64 unix) (2 Replies)
Discussion started by: yakari
2 Replies
Login or Register to Ask a Question