Code help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Code help
# 1  
Old 05-30-2013
Code help

can any body tell me

what is the meaning of the following statements

Code:
 
$0 ~ S {
                V = $0
                getline
                if ( $0 == 199 )
                {
                        getline
                        if ( $0 == 225 )
                        {
                                print V RS 258
                                next
                        }
$0 !~ S {
                print $0
                F = 0
        }


Thanks
Khaled
# 2  
Old 05-30-2013
If you are referring to this post by me, here is the explanation:
Code:
SEQ=45654

awk -v S="$SEQ" '                                       # Set S = $SEQ
        $0 == S {                                       # Check if current record $0 == S
                V = $0                                  # Set V = $0 ( current record )
                getline                                 # Get next record
                if ( $0 == 199 )                        # Check if current record $0 == 199
                {
                        getline                         # Get next record
                        if ( $0 == 225 )                # Check if current record $0 == 225
                        {
                                print V RS "258"        # Print V RS (special awk variable - newline by default) "258"
                                next                    # Skip processing current record
                        }
                        else                            # If current record $0 != 255
                        {
                                print V RS "199" RS $0  # Print V RS "199" RS $0
                                next                    # Skip processing current record
                        }
                }
                else                                    # If current record $0 != 199
                {
                        print V RS $0                   # Print V RS $0
                        next                            # Skip processing current record
                }
        }
        $0 != S {                                       # Check if current record $0 != $0
                print $0                                # Print $0 ( current record )
        }
' file

# 3  
Old 05-30-2013
Dear Yoda
what is the expression if want to check for the previous record of the current record not the next record as getline does ?

Thanks
# 4  
Old 05-30-2013
There is no statement in awk which can help get previous record.

Programmers usually use variables to store the data from current record if that is required for any further operation once awk has read the next record.

The use of variable V in the code that I posted is a good example for this usage.
# 5  
Old 05-30-2013
Thanks Yoda

in this case

I need to use the following code which is easier to access pervious or next record as using
Code:
A[I] , A[i+1] and A[i-1]

as following


Code:
 
./ascii <$1 >"$1"ascii 
SEQ=200
awk -v S="$SEQ" '
        {
                A[++c] = $1
        }
        END {
                for ( i = 1; i <= c; i++ )
                {
                        if ( A[i] == S )
                        {
                                C[A[i]]++
                                V[A[i]] = "the letter is " A[i] 
                        }
                }
                for ( k in V )
                {
                        print C[k] " times"
                        print V[k]
                }
        }
' "$1"ascii>200search


But it wont work for large files as it shows memory allocation errors

any advice ?

Thanks
# 6  
Old 05-30-2013
Yes, this will not work for large files because your code is trying to load all records from your input file to an Indexed Array!

Instead you should follow the approach used in previous code. In that one, current record is stored in a variable, getline statement is called to fetch next record and then further comparison is performed. Thus the code is not memory intensive and will work for large files.
This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Linux

Go to a line of code , skip few lines of code

Hi , I have a code where i am using a infinite while loop . some thing like below while do if then #go to line 20 fi command 1; command 2; #line 20: sleep 34; (5 Replies)
Discussion started by: Paarth
5 Replies

2. Shell Programming and Scripting

Block of code replacement in Java source code through Unix script

Hi, I want to remove the following code from Source files (or replace the code with empty.) from all the source files in given directory. finally { if (null != hibernateSession && hibernateSession.isOpen()) { //hibernateSession.close(); } } It would be great if the script has... (2 Replies)
Discussion started by: hareeshram
2 Replies

3. UNIX for Dummies Questions & Answers

If ‘922’ Code does not exist on ‘03’ Record, ‘901’ Code will be there instead, move ‘03’ R

01,011600033,011600033,110516,0834,2,90,,2/ 02,011600033,011103093,1,110317,0834,,2/ 03,105581,,015,+00000416418,,,901,+00000000148,,,922,+000000 00354,,/ 03,113806,,015,+00000559618,,,901,+00000000096,,,922,+000000 00621,,/ 88,902,+0000000025218,,/... (1 Reply)
Discussion started by: sgoud
1 Replies

4. Shell Programming and Scripting

translate ksh code to csh code

hi all, Can any 1 help me translate this korn shell code to C shell code : email=$(grep "^$1" $folder/config_2.txt | awk '{print $2'}) In config_2.txt the content is : which mean in korn shell , $1=groupname and $2=email address. Now i need to write in C shell script,when i set the... (2 Replies)
Discussion started by: proghack
2 Replies

5. Programming

how i prepare a c++ code(c code) for implementing my own protocol format

helo my protocol format is given below { destno,mode,no.of packet,pktsize,,pktno,textsize,CRC} description:- { is starting flag destno - 4bytes mode - 1 byte no.of pkt - 4byes pktsize - 6 bytes ... (1 Reply)
Discussion started by: amitpansuria
1 Replies

6. UNIX for Advanced & Expert Users

Return code from PL/SQL Code

Hi Guys, I was just wondering if anybody can help me with this problem. OK, how we can get a value back from PL/SQL Script (not stored procedure/function) See the below example: (for example aaa.sh) #!/bin/ksh VALUE=`sqlplus -s user/password@test_id <<EOF @xxx.sq EOF` echo $VALUE ... (7 Replies)
Discussion started by: Shaz
7 Replies
Login or Register to Ask a Question