Help in awk/bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in awk/bash
# 15  
Old 12-31-2012
I believe the following script does what you want:
Code:
#!/bin/ksh
no=${1:-no.txt}         # name of file for no entry if $1%100 != 0
to=${2:-trun.txt}       # name of file for truncated $1 entries
ro=${3:-ro.txt}         # name of file for rounded $1 entries
bo=${4:-tro.txt}        # name of file for both rounded& & runcated $1 entries
awk -v bo="$bo" -v no="$no" -v ro="$ro" -v to="$to" 'BEGIN {rc = 1}
FNR == NR {r[rc] = r[rc] $0 "\n"
    if($0 == "ENDMDL") rc++
    next}
{   # If we got to here, we are reading lines from the 2nd file.
    # Determine exact, truncated, and rounded entry numbers.
    if (substr($1, length($1) - 5) == "00.000") {
        # $1 ends in 00.000; no truncation or rounding needed.
        entry = substr($1, 1, length($1) - 6)
        round = trunc = 0
    } else {
        # $1 is not evenly divisible by 100; calculate rounded and truncated
        # values.
        entry = 0
        round = sprintf("%.0f", $1 / 100)
        trunc = substr($1, 1, length($1) - 6)
    }
    # Determine which markers and entries to print in each output file.
    if(entry) {
        # No rounding and no truncation involved.  Write the appropriate entry
        # to each output file.
        printf("%s", r[entry]) > bo
        printf("%s", r[entry]) > no
        printf("%s", r[entry]) > ro
        printf("%s", r[entry]) > to
    } else {
        # Rounding and truncation performed; Prepare shared markers.
        rm = sprintf("Following entry (%d) comes from %s rounded:", round, $1)
        tm = sprintf("Following entry (%d) comes from %s truncated:", trunc, $1)

        # Write appropriate markers and/or entries for each output file.
        printf("%s\n%s", tm, r[trunc]) > bo
        if(trunc != round) printf("%s\n%s", rm, r[round]) > bo
        printf("Entry skipped because %s is not evenly divisible by 100.\n",
            $1) > no 
        printf("%s\n%s", rm, r[round]) > ro 
        printf("%s\n%s", tm, r[trunc]) > to
    }
}' 11.txt o.txt

Note that it still assumes that the 1st field in o.txt is formatted as a floating point number with three digits after the radix point. If you save this script in a file (for example split4), make it executable:
Code:
chmod +x split4

edit it to change /bin/ksh in the first line of the script to be an absolute pathname to the Korn shell on your system (if it isn't in /bin/ksh), and run it:
Code:
./split4

it will create your four output files no.txt, ro.txt, tro.txt, and trun.txt from the input files 11.txt and o.txt. Note that it will overwrite each of these four files each time you run the script; not append to them. If you want to use different file names for the output files, run it as:
Code:
./split4 no_round_or_trunc truncated rounded rounded_and_truncated

to specify alternative output file names (note that the order is important).
This User Gave Thanks to Don Cragun For This Post:
# 16  
Old 12-31-2012
Thanks. I will try to run and let you know.

Anyways, Wishing you and the members of the forum a very HAPPY NEW YEAR 2013. Smilie

---------- Post updated at 08:51 PM ---------- Previous update was at 07:48 PM ----------

I am getting error:
Code:
'for reading <No such file or directory>  'o.txt

But, I am working in the same directory where all files are there.

Also, if I include the comments in the end of script using #, then also I get errors:
Code:
'for reading <No such file or directory>  'o.txt
split4.sh: : line 45: $'\r': command not found

line 45 (line where comments start)

Last edited by Franklin52; 01-03-2013 at 03:33 AM.. Reason: Please use code tags for data and code samples
# 17  
Old 12-31-2012
Quote:
Originally Posted by bioinfo
Thanks. I will try to run and let you know.

Anyways, Wishing you and the members of the forum a very HAPPY NEW YEAR 2013. Smilie

---------- Post updated at 08:51 PM ---------- Previous update was at 07:48 PM ----------

I am getting error:
'for reading <No such file or directory> 'o.txt

But, I am working in the same directory where all files are there.

Also, if I include the comments in the end of script using #, then also I get errors:
'for reading <No such file or directory> 'o.txt
split4.sh: : line 45: $'\r': command not found

line 45 (line where comments start)
From the looks of the error message (note the single quote at the start of the line that seems like it should be at the end of the file name), it looks like you ended up with a <carriage-return> character at the end of "o.txt" in the last line of split4.sh and it is trying to open a file with the name <o><period><t><x><t><carriage-return>. To verify this try running the command:
Code:
od -c split4

and look for a \r before the \nthat should be at the end of the file. This suspicion is supported by the later error saying that the command $'\r' can't be found.

If you find one or more occurrences of \r in the output from od, run the commands:
Code:
cp split4.sh _split4.sh
tr -d '\r' < _split4.sh > split4.sh

and then try running split4 again.
This User Gave Thanks to Don Cragun For This Post:
# 18  
Old 01-01-2013
Quote:
Originally Posted by bioinfo
split4.sh: : line 45: $'\r': command not found
Errors with \r in them mean "stop editing your scripts in Microsoft Notepad".
This User Gave Thanks to Corona688 For This Post:
# 19  
Old 01-01-2013
I am using Notepad ++ Smilie
# 20  
Old 01-01-2013
Thanks a lot Don Cragun and Corona688. I edited script in vi and its working. Yippie Smilie

I have one more query. I am using the following tro.txt as my input file for further program:

Code:
Following entry (2659) comes from 265920.000 truncated:
ATOM      1  N   SER A   1     117.041 155.383 146.906  1.00  0.00           N  
ATOM      2  CA  SER A   1     115.956 155.933 147.729  1.00  0.00           C  
ATOM      3  C   SER A   1     116.331 155.850 149.194  1.00  0.00           C  
TER
ENDMDL
Following entry (2703) comes from 270330.000 rounded:
ATOM      1  N   SER A   1     122.255 148.746 136.780  1.00  0.00           N  
ATOM      2  CA  SER A   1     122.237 147.748 137.846  1.00  0.00           C  
ATOM      3  C   SER A   1     121.916 148.457 139.169  1.00  0.00           C  
TER
ENDMDL
Following entry (2703) comes from 270360.000 rounded:
..........................................................................
..........................................................................

I wish to delete all following lines in this file:

Following entry (2659) comes from 265920.000 truncated:
Following entry (2703) comes from 270330.000 rounded:
Following entry (2703) comes from 270360.000 rounded:
..........................................................................
..........................................................................

Required output:

Code:
ATOM      1  N   SER A   1     117.041 155.383 146.906  1.00  0.00           N  
ATOM      2  CA  SER A   1     115.956 155.933 147.729  1.00  0.00           C  
ATOM      3  C   SER A   1     116.331 155.850 149.194  1.00  0.00           C  
TER
ENDMDL
ATOM      1  N   SER A   1     122.255 148.746 136.780  1.00  0.00           N  
ATOM      2  CA  SER A   1     122.237 147.748 137.846  1.00  0.00           C  
ATOM      3  C   SER A   1     121.916 148.457 139.169  1.00  0.00           C  
TER
ENDML

Please guide.
Thanks.

Last edited by Scrutinizer; 01-04-2013 at 12:38 AM.. Reason: quote tags -> code tags
# 21  
Old 01-01-2013
Code:
grep -v Following < inputfile > outputfile

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

New problem with awk using bash

Hi! I have a new problem with awk, this time I think is because I'm using it in bash and I don't know how to put the valor of the variable in awk. Here is the code: #!/bin/bash for i in 1 2 3 4 5 do a=$i b=$ awk '$1>=a&&$1<=b {print $1,$2,$3}'>asdf test... (3 Replies)
Discussion started by: florpi
3 Replies

2. Shell Programming and Scripting

Returning a value from awk to bash

Hi I am a newbie starting bash and I have a simple need to return the result of an operation from awk to bash. basically I want to use awk to tell me if "#" exists in a string, and then back in bash, i want to do an IF statement on this return in order to do other things. In my bash shell I... (2 Replies)
Discussion started by: oahmad
2 Replies

3. Shell Programming and Scripting

Help in awk/bash

Hi, I have two files: atom.txt and g.txt atom.txt has multiple patterns but I am showing only two patterns each ending with ENDMDL: ATOM 1 N SER A 1 35.092 83.194 140.076 1.00 0.00 N ATOM 2 CA SER A 1 35.216 83.725 138.725 1.00 0.00 C TER ENDMDL ATOM 1 N SER A 1 35.683 81.326 139.778 1.00... (11 Replies)
Discussion started by: bioinfo
11 Replies

4. UNIX for Dummies Questions & Answers

Help in awk/bash

Hi, I am also a newbie in awk and trying to find solution of my problem. I have one reference file 1.txt with 2 columns and I want to search other 10 files (a.txt, b.txt......h.txt each with 5 columns) corresponding to the values of 2nd column from 1.txt. If the value from 2nd column from 1.txt... (0 Replies)
Discussion started by: bioinfo
0 Replies

5. Shell Programming and Scripting

AWK/Bash script

I would like to write a script to extend this command to a general case: BEGIN {s_0=0;n_0=0}{n_0++;s_0+=($51-$1)^2}END {print sqrt(s_0/n_0)} i.e. so that BEGIN {s_0=0;n_0=0}{n_0++;s_0+=($51-$1)^2}END {print sqrt(s_0/n_0)} BEGIN {s_1=0;n_1=0}{n_1++;s_1+=($51-$2)^2}END {print... (3 Replies)
Discussion started by: chrisjorg
3 Replies

6. UNIX for Dummies Questions & Answers

Help with BASH/AWK queries ....

Hi Everyone, I have an input file in the following format: score.file1.txt contig00045 length=566 numreads=19 1047 0.0 contig00055 length=524 numreads=7 793 0.0 contig00052 length=535 numreads=10 607 e-176 contig00072 length=472 numreads=46 571 e-165... (8 Replies)
Discussion started by: Fahmida
8 Replies

7. Shell Programming and Scripting

scripting help with bash and awk

I'm trying to reformat some tide information into a useable format and failing. Input file is.... 4452 CHENNAI (MADRAS) 13°06'N, 80°18'E India East Coast 01 June 2009 UT(GMT) Data Area 3. Indian Ocean (northern part) and Red Sea to Singapore 01/06/2009 00:00 0.7 m 00:20 0.7 m 00:40... (3 Replies)
Discussion started by: garethsays
3 Replies

8. Shell Programming and Scripting

awk bash help

Hi, I'm trying to read a file containing lines with spaces in them. The inputfile looks like this ------------------------------ Command1 arg1 arg2 Command2 arg5 arg6 arg7 ------------------------------- The shell code looks like this... lines=`awk '{ print }' inputfile` ... (2 Replies)
Discussion started by: a-gopal
2 Replies

9. Shell Programming and Scripting

Is there any better way for sorting in bash/awk

Hi, I have a file which is:- 1 6 4 8 2 3 2 1 9 3 2 1 3 3 5 6 3 1 4 9 7 8 2 3 I would like to sort from field $2 to field $6 for each of the line to:- 1 2 3 4 6 8 2 1 1 2 3 9 3 1 3 3 5 6 4 2 3 7 8 9 I came across this Arrays on example 26-6. But it is much complicated. I am... (7 Replies)
Discussion started by: ahjiefreak
7 Replies

10. Shell Programming and Scripting

BASH with AWK

Hello, I have a file.txt with 20000 lines and 2 columns each which consists of current_filename and new_filename . I want to create a script to find files in a directory with current_filename and move it to new folder with new_filename. Could you please help me how to do that?? ... (2 Replies)
Discussion started by: narasimhulu
2 Replies
Login or Register to Ask a Question