help with simple awk script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help with simple awk script
# 1  
Old 06-11-2010
help with simple awk script

Hi,
I just don't understand awk. I think I'm close here but can someone give me a hand to finish this little parsing routine ?

the input file is formatted like this:
Code:
District 2502110
Gsub 2384889
Gsub 1428180
District 2502220
Gsub 1466390
Gsub 1466389
Gsub 1466388
Gsub 1466386
Gsub 1466385
District 2502710

The script should make a command for each Gsub line that contains it's district.
Here's what I got but doesn't work.

Thanks !
floyd
Code:
awk '
{
  if (( $1 == "District" ))
  {
    dist=$2
    while (( $1 != "District" ))
    {
      sub=$2
      printf("dvmrgdel %s %s\n",sub,dist)
      getline
    }
   }
}' file


Last edited by Franklin52; 06-11-2010 at 09:16 AM.. Reason: Please use code tags!
# 2  
Old 06-11-2010
Post desired output from that input file. Also use code tags.
# 3  
Old 06-11-2010
the desired output is:
Code:
dvmrgdel 2384889 2502110
dvmrgdel 1428180 2502110
dvmrgdel 1466390 2502220
dvmrgdel 1466389 2502220

So the Gsubs within a district print with their respective districts.

One thing I found out so far is my use of the variable "sub" won't work because sub is a command in awk. So I changed it to subm.

Code again:
Code:
nawk '
{
  if ( $1 == "District" )
  {
    dist=$2
    while ( $1 != "District" )
    {
      subm=$2
      printf("dvmrgdel %s %s\n",subm,dist)
      getline
    }
  }
}' list


Last edited by Franklin52; 06-11-2010 at 09:22 AM.. Reason: code tags
# 4  
Old 06-11-2010
Try this:
Code:
awk '/District/{d=$2; next} {print "dvmrgdel " $2 FS d}' file

# 5  
Old 06-11-2010
That is an awesome piece of effecient code and works !!

Thank you very much !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple awk use

Hi, I have a file such that: 40454,31,48,4,1304.967741935484,1 31708,25,48,4,1268.32,1 20900,64501,671,788,0.3240259840932699,0 20137,51358,834,743,0.3920908135051988,0 I want to replace the 6th column by "ones" if it is 1, and with "zeros" if it is 0. Thanks. (6 Replies)
Discussion started by: shekhar2010us
6 Replies

2. Shell Programming and Scripting

Simple awk script needed

but I'm stumped...please help I have a file like this....... 1000 1 34 1000 10 34 1000 11 35 1000 20 35 1000 21 36 1000 30 36 2000 1 34 2000 10 34 which I would like printed out as 40 lines 1000 1 34 1000 2 34 1000 3 34 1000 4 ... (2 Replies)
Discussion started by: garethsays
2 Replies

3. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

4. UNIX for Dummies Questions & Answers

Simple awk script for positional replacement in text?

I have a string of letters. (They happen to be DNA, not that it's relevant to the question.) For analysis purposes, I need to replace the information at some of the sites. I need to do this based on their position, not the information in that position. I also need to ignore differences at other... (10 Replies)
Discussion started by: JFS
10 Replies

5. Shell Programming and Scripting

Simple AWK script problem.

Hi all, I have set up a simple awk script to calculate the average of values that are printed out a number of times per second (the number of time the printing occurs varies). The data is of the format shown below: 1 4.43 1 3.65 1 2.45 2 7.65 2 8.23 2 5.65 3 4.65 3 6.21 .. .. 120... (4 Replies)
Discussion started by: omnomtac
4 Replies

6. Shell Programming and Scripting

simple awk

when I execute this awk stmt .. awk "/log_directory/ { print $5}" /opt/dba/oraadmin/tools/tmp_purge_op.log it's returning the whole line as .. IRMD118_LISTENER1 parameter "log_directory" set to /opt/oracle/10.2/network/log/ my expected output is : /opt/oracle/10.2/network/log what... (7 Replies)
Discussion started by: talashil
7 Replies

7. Shell Programming and Scripting

Help with a simple script using awk

I need a hand with this simple script, in Unix i have a variable called portal: $ echo $portal chu0 when i use awk this variable is not recognized. How can i make awk recognize and print the value of this variable, since the output is not shown i.e. awk ' BEGIN {printf("%4s\t%14s\n",... (3 Replies)
Discussion started by: alexcol
3 Replies

8. Shell Programming and Scripting

need help with simple awk/ksh script

I need help finding out why this script wont run. The chmod is okay, but i get an error saying that I need '&&' on line 5. (18 Replies)
Discussion started by: tefflox
18 Replies

9. Shell Programming and Scripting

simple awk script...

hi everyone, i have a script that I use regulary to look through my files and directories it works fine, but I would like to add a segment to the script to make the output more readable and userfriendly.. (i am not an expert on scripts so there is no comments or status exits as of yet.. )... (3 Replies)
Discussion started by: moxxx68
3 Replies

10. Shell Programming and Scripting

Simple awk script question

Hi, I'm a total beginner at awk and hope someone can advise what I have done wrong in the following script: I have a file which (to simplify things) may be something like this Fred Smith and Sue Brown Joe Jones and Jane Watts Sally Green and Jim O? Connor Freda O? Reiley and Pat O?... (2 Replies)
Discussion started by: Bab00shka
2 Replies
Login or Register to Ask a Question