LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   running series of awk commands from an awk file (http://www.linuxquestions.org/questions/programming-9/running-series-of-awk-commands-from-an-awk-file-944875/)

oreka18 05-14-2012 08:50 AM

running series of awk commands from an awk file
 
i wrote such below awk commands:
Code:

awk '/^ *200?/{x=$1$2;y="? ? ?";while($1!~/STAT/)getline;next}!/^ *$/{sub(/^./,"",$3);print $1,y,$3,"?",x,$5,$6;}/^ *$/{print}' igut > 1
i want to change these commands to scripts, write them to a file and then run it, but how?

colucix 05-14-2012 09:10 AM

You can either write down a pure awk script or a shell script specifying awk as interpreter. In the first case you need only the code inside single quotes:
Code:

/^ *200?/ {
  x = $1$2
  y = "? ? ?"
  while ( $1 !~ /STAT/ )
    getline
  next
}

!/^ *$/ {
  sub(/^./,"",$3)
  print $1,y,$3,"?",x,$5,$6
}

/^ *$/ {
  print
}

The spacing and indentation are only a matter of style. Suppose you have the code above written in a script called test.awk: you can run it by means of:
Code:

awk -f test.awk input_file > output_file
In the second case you can write down the same code using a proper sha-bang to specify awk as interpreter of the statements inside the script:
Code:

#!/bin/awk -f
/^ *200?/ {
  x = $1$2
  y = "? ? ?"
  while ( $1 !~ /STAT/ )
    getline
  next
}

!/^ *$/ {
  sub(/^./,"",$3)
  print $1,y,$3,"?",x,$5,$6
}

/^ *$/ {
  print
}

Suppose you call it my_test: you can make it executable and run as
Code:

./my_test input_file > output_file
The chapter 1.1 of the official GNU awk guide explains it all.

oreka18 05-15-2012 11:04 AM

merg with another file
 
thanks for answer completely my threat, i have one more ask, how can i use an out put of an awk file with another one and write one more script to use the output file? for example in last section there is one output file named "output_file", now i want use of this output for another awk script on that awk file.

Code:

[desired file]:
awk script (1) > out_file
awk script (2) < out_file > final_file


rohit_k 05-16-2012 01:13 AM

No need of out_file; try giving the output of awk_script_1 to awk_script_2 as input.
awk_script_1 | awk_script_2 > final_out_file


All times are GMT -5. The time now is 06:37 AM.