LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-14-2012, 08:50 AM   #1
oreka18
LQ Newbie
 
Registered: May 2012
Posts: 7

Rep: Reputation: Disabled
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?
 
Old 05-14-2012, 09:10 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
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.
 
1 members found this post helpful.
Old 05-15-2012, 11:04 AM   #3
oreka18
LQ Newbie
 
Registered: May 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
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
 
Old 05-16-2012, 01:13 AM   #4
rohit_k
LQ Newbie
 
Registered: Oct 2007
Location: Pune
Distribution: RHEL, SOLARIS, SUSE
Posts: 18

Rep: Reputation: 1
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
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
awk error awk: line 2: missing } near end of file boscop Linux - Networking 2 04-08-2012 10:49 AM
AWK/BASH: get nth line from a file by getline feed to actions in a same awk line cristalp Programming 3 11-23-2011 11:38 AM
parsing a text file - to awk or not to awk ? rollyah Programming 9 08-18-2011 02:20 PM
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 09:06 PM
Using awk to grab first number series buee Linux - Newbie 1 02-03-2010 04:08 PM


All times are GMT -5. The time now is 04:51 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration