how to write a function or awk fn ? , please help !!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to write a function or awk fn ? , please help !!
# 1  
Old 11-13-2011
how to write a function or awk fn ? , please help !!

Code:
************* fixed  ***************
Begin  

equipmentId : d9
processor : fox
number : bhhhhhh


Variable # 1: 
  
Id           : 100
Type         : 9
nType        : s
gType        : 5f
mType        : 4
LField       : England
DataField    : london
Length       : 4

 End 


Variable # 2: 
  
Id           : 200
Type         : 9
nType        : f
gType        : f
mType        : 9
LField       : England
DataField    : london
Length       : 4

 End 


Variable # 3: 
  
Id           : 400
Type         : 7
nType        : c
gType        : r
mType        : h
LField       : Egypt
DataField    : Cairo
Length       : 4

 End 

Variable # 4: 
  
Id           : 515
Type         : 7
nType        : 1
gType        : 5d
mType        : 9a
LField       : France
DataField    : Paris
Length       : 2

 End 


End of  section.

************* fixed  ***************

Begin 

equipmentId : d9
processor : fox
number : bhhhhhh


Variable # 1: 
  
Id           : 515
Type         : 9
nType        : s
gType        : 5f
mType        : 4
LField       : England
DataField    : london
Length       : 4

 End 

Variable # 2: 
  
Id           : 550
Type         : 9
nType        : f
gType        : f
mType        : 9
LField       : France
DataField    : Paris
Length       : 4

 End


Variable # 3: 
  
Id           : 600
Type         : 7
nType        : c
gType        : r
mType        : h
LField       : Egypt
DataField    : Cairo
Length       : 4

 End  
  
End of  section.

************* fixed  ***************

Begin  

equipmentId : d9
processor : fox
number : bhhhhhh


Variable # 1: 
  
Id           : 100
Type         : 9
nType        : s
gType        : 5f
mType        : 4
LField       : England
DataField    : london
Length       : 4

 End 

Variable # 2: 
  
Id           : 515
Type         : 9
nType        : f
gType        : f
mType        : 9
LField       : Egypt
DataField    : Cairo
Length       : 4

 End 

Variable # 3: 
  
Id           : 609
Type         : 7
nType        : c
gType        : r
mType        : h
LField       : usa
DataField    : NY
Length       : 4

 End
  

Variable # 4: 
  
Id           : 715
Type         : 7
nType        : 1
gType        : 5d
mType        : 9a
LField       : germany
DataField    : munich
Length       : 2

 End 

End of  section.
************* fixed  ***************

Begin  

equipmentId : d9
processor : fox
number : bhhhhhh


Variable # 1: 
  
Id           : 100
Type         : 9
nType        : s
gType        : 5f
mType        : 4
LField       : England
DataField    : london
Length       : 4

 End 

End of section.


i wanna get a column getting ( DataField under ID : 515 ) in the variable part that its not in same place every time and variable fields are not of a fixed number and if he couldnt find ID 515 return with ( not found )

to make output file >

Code:
Paris 
london 
cairo 
not found   ....... as Id 515 not found

---------- Post updated at 09:42 AM ---------- Previous update was at 08:39 AM ----------

hey couldn't any one help me in this file Smilie

Last edited by teefa; 11-13-2011 at 09:57 AM..
# 2  
Old 11-13-2011
I think this will do what you are looking for.
I assumed by "not found" you might have some 515 variable sections that don't have a datafield value.

This reads the file from stdin and allows you to set the target from the command line; 515 is the default.
Code:
#!/usr/bin/env ksh

awk -v target="${1:-515}" '
    BEGIN {
        value["DataField"] = "Not Found";
    }

    /Variable #/  { snarf = 1; next; }

    snarf > 0 && $1 == "End" {
        snarf = 0;

        if( value["Id"] == target )
            printf( "%s\n", value["DataField"] );

        delete value
        value["DataField"] = "Not Found";
        next;
    }

    snarf > 0 && $(2) == ":" {
        value[$1] = $3;
    }
'
exit $?

If you save the script in list.ksh, and your input file is input_data, you can invoke the script like this:

Code:
list.ksh 515 <input_data

You can omit the 515, or change it to a different value if you want to suss out some other section.
This User Gave Thanks to agama For This Post:
# 3  
Old 11-13-2011
MySQL

Thanks alot for your effort
SmilieSmilieSmilieSmilie
but can it be in bash

second "not found" here means that (Id = 515) not found so datafield under it wont be found also.

Last edited by Scott; 11-13-2011 at 02:50 PM.. Reason: Removed pointless QUOTE tags
# 4  
Old 11-13-2011
Should work in bash just fine. Have a go with it Smilie
This User Gave Thanks to agama For This Post:
# 5  
Old 11-13-2011
when i wrote it . this error i couldnt know how to deal with


Quote:

bash: 515: Bad file number
# 6  
Old 11-13-2011
Sounds like it might be a misplaced quote or somesuch. Can you paste your script in? It might also help if you executed your script with a -x option -- might be provide more info:

Code:
bash -x  script-filename

This User Gave Thanks to agama For This Post:
# 7  
Old 11-13-2011
Code:
bash-3.00# bash -x List.ksh 300 <list
+ awk -v target=300 '
    BEGIN {
        value["DataField"] = "Not Found";
    }

    /Variable #/  { snarf = 1; next; }

    snarf > 0 && $1 == "End" {
        snarf = 0;

        if( value["Id"] == target )
            printf( "%s\n", value["DataField"] );

        delete value
        value["DataField"] = "Not Found";
        next;
    }

    snarf > 0 && $(2) == ":" {
        value[$1] = $3;
    }
'
awk: syntax error near line 1
awk: bailing out near line 1
+ exit 2


thats what i do , see this is the output u asked me to do
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. Shell Programming and Scripting

Need help to write a function in shell scripting to execute sql files

Hi, I am new to shell scripting and i need to write a automation script to execute sql files. I need to check the table if it is there in system tables and need to write a function to call the .sql files. For ex. I have a.sql,b.sql,c.sql files, where the sql file contains DELETE and INSERT... (1 Reply)
Discussion started by: Samah
1 Replies

3. UNIX for Dummies Questions & Answers

MAN and read & write function

How to use MAN to find information about read() and write() function ? The command "man read" show some rubbish, for example "man open" show great information about function I need. (2 Replies)
Discussion started by: bbqtoss
2 Replies

4. UNIX for Dummies Questions & Answers

how to write a function to get data under specific lines ?

I have a text file called (msgz ) contains data : Subscriber Data ID = 2 Customer = 99 Data ID = 4 Customer = cf99 Data ID = 5 Customer = c99 Data ID = 11 Customer = 9n9 Subscriber Data ID = 1 Customer = 9ds9 Data ID = 2 Customer = 9sad9 Data ID = 3 Customer = f99... (3 Replies)
Discussion started by: teefa
3 Replies

5. Shell Programming and Scripting

how to write a function to get data under spesific lines ? using bash

I have a text file called ( bvhz ) contains data : Subscriber Data ID = 2 Customer = 99 Data ID = 4 Customer = cf99 Data ID = 5 Customer = c99 Data ID = 11 Customer = 9n9 Subscriber Data ID = 1 Customer = 9ds9 Data ID = 2 Customer = 9sad9 Data ID = 3 Customer = f99... (1 Reply)
Discussion started by: teefa
1 Replies

6. Homework & Coursework Questions

Write a function named isPrime

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a function named isPrime which accepts one integer parameter, say x. Have the function return TRUE if x... (4 Replies)
Discussion started by: KyleBucket
4 Replies

7. Shell Programming and Scripting

[BASH] Using a function to write data to a file

Hello, I've created a shell script, which accepts information using an input from the console. Part of the script will write a file containing this information. My code looks like (for the write) function make_file { cat <<- _EOF_ The contents of my file are here _EOF_ } ... (12 Replies)
Discussion started by: cpickering
12 Replies

8. Programming

how to write a wrapper c code to return uid using getuid() function

And how to use setuid() ? thanks (4 Replies)
Discussion started by: pwd
4 Replies

9. Shell Programming and Scripting

how to write divided function

hi iam facing problem for divided (%) arthemtic function in for condition. tell me reply and all the arthemtic function in one scripting . (2 Replies)
Discussion started by: naveeng.81
2 Replies

10. Shell Programming and Scripting

How To Write Sed Function

hi iam facing problem regarding sed function. give me reply with example of sed function. and what to meanings of $# and @ in scripting please as reply as soon as possible (1 Reply)
Discussion started by: naveeng.81
1 Replies
Login or Register to Ask a Question