Removing sections and leaving separators intact


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing sections and leaving separators intact
# 1  
Old 12-15-2009
Removing sections and leaving separators intact

I have an awk script like below

Code:
function abs(val) {
   return val > 0 ? val : -val
}

# 1. Main input loop, executed for each line of input
BEGIN { RS = ORS = ">" }

{ if ( NF > 2 )
    {
        if ( abs( $1 - $(NF-2) ) < 40 ) { print }
    }
}

The input file is something like this

Code:
>
10 0 0
10 0 0
>
13.0507 0 6.38077
12.9668 0.059253 6.16695
12.6077 0.244366 5.3721
12.2461 0.372226 4.64772
11.8815 0.447634 3.95789
11.5135 0.471699 3.2737
11.1415 0.442141 2.5654
10.7656 0.352356 1.79298
10.3858 0.188169 0.921258
10 0 0
>
13.507 0 6.38077
130.507 0 0
>

The code will sometimes remove some section enclosed within ">", otherwise leaving everything intact.

However when I run the script I am missing the first ">" sign.

Output is as

Code:
10 0 0
10 0 0
>
13.0507 0 6.38077
12.9668 0.059253 6.16695
12.6077 0.244366 5.3721
12.2461 0.372226 4.64772
11.8815 0.447634 3.95789
11.5135 0.471699 3.2737
11.1415 0.442141 2.5654
10.7656 0.352356 1.79298
10.3858 0.188169 0.921258
10 0 0
>


rather than

Code:
>
10 0 0
 10 0 0
 >
 13.0507 0 6.38077
 12.9668 0.059253 6.16695
 12.6077 0.244366 5.3721
 12.2461 0.372226 4.64772
 11.8815 0.447634 3.95789
 11.5135 0.471699 3.2737
 11.1415 0.442141 2.5654
 10.7656 0.352356 1.79298
 10.3858 0.188169 0.921258
 10 0 0
 >

# 2  
Old 12-15-2009
just put:

Code:
BEGIN{ print ">"; RS = ORS = ">" ; }

It won't print because it hasn't counted more than 2 fields yet.
You have your RECORD SEPARATOR set to >, this means that first one
has found no fields ( NF ) yet.
# 3  
Old 12-15-2009
Will try it

Can do

Code:
{ if ( NR == 1 ) print }

or

Code:
NR == 1 { print }



---------- Post updated at 02:48 PM ---------- Previous update was at 02:39 PM ----------

Works that way too. Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to control grep output intact for each matching line?

I have multiple (~80) files (some can be as big as 30GB of >1 billion of lines!) to grep on a pattern, and piped the match to a single file. I have a 96-core machine so that each grep job was sent to the background to speed up the search: file1.tab chr1A_part1 123241847 123241848... (6 Replies)
Discussion started by: yifangt
6 Replies

2. Shell Programming and Scripting

Printing the output of a gzip command intact

how can i get the printf command or the echo command to print the data that are inbetween the first and the last quotes? #!/bin/sh printf '%s\n' "^_<8b>^H^@U<8c>MX^@^Cí=ÙzÚH<97>×ð^Teìn<8c>Ób_<9d><9f>dXd<9b>^N^F7<82>8qâÎ'^K^Y^T<83>D<90>°M^Lý^Hó^Fs5w3ß|s5/ÐýbS%©<84>^DBH... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. Shell Programming and Scripting

Removing sections from listener.ora

Hi, I am trying to write a script or command to add/remove a section from listener.ora file in the following example I would like: 1. to remove sid_alias2 sections 2. to add a new alias sections called sid_alias4, it can be placed after sid_alias3 sections $ cat listener.ora ... (18 Replies)
Discussion started by: ynixon
18 Replies

4. Shell Programming and Scripting

Remove duplicates by keeping the order intact

Hello friends, I have a file with duplicate lines. I could eliminate duplicate lines by running sort <file> |uniq >uniq_file and it works fine BUT it changes the order of the entries as it we did "sort". I need to remove duplicates and also need to keep the order/sequence of entries. I... (1 Reply)
Discussion started by: magnus29
1 Replies

5. Shell Programming and Scripting

Keeping the number intact

Currently I have the following to separate the numeric values. However the decimal point get separated. ls -lrt *smp*.cmd | awk '{print $NF}' | sed 's/^.*\///' | sed 's/\(*\)/ & /g' As an example on the files n02-z30-dsr65-terr0.50-dc0.05-4x3smp.cmd... (8 Replies)
Discussion started by: kristinu
8 Replies

6. UNIX for Dummies Questions & Answers

sort by keeping the headings intact?

Hi all, I have a file with 3 columns separated by space. Each column has a heading. I want to sort according to the values in the 2nd column (ascending order). Ex. Name rank direction goory 0.05 --+ laby 0.0006 --- namy 0.31 -+- ....etc. Output should be Name rank direction laby... (3 Replies)
Discussion started by: Unilearn
3 Replies

7. Shell Programming and Scripting

awk removing sections of a file

I have a file that looks liek this (see below). can somebody provide me with and awk or sed command that can take a piece of the file starting from the time to the blank line and put in into another file. For example: How would I get the data from 10:56:11 to the blank line. Two things: ... (5 Replies)
Discussion started by: BeefStu
5 Replies

8. Shell Programming and Scripting

Removing sections

I have a file like this %( PHASES P %) %( SOURCES (10,0.0) (13,0.0) (16,0.0) (19,0.0) (22,0.0) (25,0.0) (28,0.0) (31,0.0) (34,0.0) (37,0.0) (40,0.0) (1 Reply)
Discussion started by: kristinu
1 Replies

9. Shell Programming and Scripting

Remove text between headers while leaving headers intact

Hi, I'm trying to strip all lines between two headers in a file: ### BEGIN ### Text to remove, contains all kinds of characters ... Antispyware-Downloadserver.com (Germany)=http://www.antispyware-downloadserver.c om/updates/ Antispyware-Downloadserver.com #2... (3 Replies)
Discussion started by: Trones
3 Replies

10. Shell Programming and Scripting

Grab terms leaving spacings intact

Hi All, I have an input below. I would want to print the 2nd to 5th term leaving the same number of spaces in between each term to be intact. Here in the example, there are 4 spacings between term " ABC " and " 111 ", and i tried the below awk codes but they don;t work. Can anybody give me... (3 Replies)
Discussion started by: Raynon
3 Replies
Login or Register to Ask a Question