Turn: igawk in zookeeper gawk

Source: Internet
Author: User

Http://bbs.chinaunix.net/thread-1450345-1-1.html

In many cases, we recommend that you write some common data records.
For example, my $ home/lib/awk stores some function numbers. How can I use these function numbers,
If I fail to use it once, I just need to upload it to the textbook. How can I use it in the Command column?

Due to this demand, GNU awk provides users with a variable and a command that can contain your own number of letters,
They are awkpath and igawk.

Awkpath indicates the variable path. The answer is:/usr/local/share/awk.
Http://www.gnu.org/manual/gawk/html_node/AWKPATH-Variable.html
But my cygwin is.:/usr/share/awk
According to the root operator, you can get the path when using environ ["awkpath "].

Gawk 'in in {print environ ["awkpath"]}'

It can be displayed. However, there are a lot of phone numbers that can be used easily.
$ Home/. bash_profile

Export awkpath = $ awkpath:/path/where/you/store/Function
Awkpath = $ awkpath: $ home/lib/awk

Source ~ /. Bash_profile.

User @ user-PC ~
$ Ls lib/awk
Cap. awk commas. awk hex2dec. awk passgen. awk reverse. awk swapcase. awk whois. awk

I have seven function numbers here, so they can be used in the Command column.

User @ user-PC ~
$ Echo-1234567 | gawk-F commas. awk -- source '{print commas ($0 )}'
-1,234,567

This is the case.

But it is not used in the life column. What should I do if I put it in the life column? Do not perform this operation in your local region,

#! /Usr/bin/gawk-F commas. awk -- Source

That's not the case. The gawk hacker wrote a command, which is also a shell script named
Igawk. When you need to use the function data, igawk is used. The format is as follows:

#! /Usr/bin/igawk-F
@ Include commas. awk
@ Include hex2dec. awk
.
.
@ Include N letter data records

The following shows how to restore your token. :). For example:

#! /Usr/bin/igawk-F
@ Include commas. awk
@ Include swapcase. awk

{
If ($0 ~ /^ -? [0-9] + $ /){
Print commas ($0)
} Else if ($0 ~ /[A-Za-Z] + /){
Print swapcase ($0)
}
}

User @ user-PC ~
$ Echo 12345 |./igawk_test
12,345

User @ user-PC ~
$ Echo root |./igawk_test
Root

This is the case. If the website is incorrect, ask the Moderator to correct it. The last question is the location.
Write the corresponding data

# Format the number, such as 1234 to 1,234
Function commas (X, Y, Z, J, Len, flag, out ){

If (x ~ /^ -/){
X = substr (x, 2)
J = "-"
}
If (MATCH (x ,/\./)){
Flag = "float"
Y = substr (x, rstart)
Z = substr (x, 1, rstart-1)
X = z
}
Else {
Flag = "int"
}
Len = length (X)

While (LEN> 3 ){
Len-= 3
X = substr (x, 1, Len) "," substr (x, Len + 1)
}

If (flag = "int "){
Out = x
}
Else {
Out = x y
}
If (j = "-") out = J out
Return out
}

# End of Function

# Escape uppercase letters, lowercase letters, and lowercase letters
Function swapcase (input, output, step, WLEN, char ){
Output = ""
Step = 1
WLEN = length (input)
While (Step <= WLEN ){
Char = substr (input, step, 1)
If (char ~ /[A-Z]/) {
Char = tolower (char)
} Else if (char ~ /[A-Z]/) {
Char = toupper (char)
} Else {
Char = char
}
Output = output char
Step ++
}
Return output
}
# End of Function

# Translate hex number to Dec number
# This is not my own
Function hex2dec (H, I, X, v ){
H = tolower (h); sub (/^ 0x/, "", H)
For (I = 1; I <= length (h); ++ I ){
X = indexes ("0123456789 abcdef", substr (H, I, 1 ))
If (! X) Return "Nan"
V = (16 * V) + X-1
}
Return v
}

# End of Function

# Reverse the word
Function reverse (x, output, WLEN, char ){
Output = ""
WLEN = length (X)
While (WLEN> 0 ){
Char = substr (x, WLEN, 1)
Output = output char
WLEN-= 1
}

Return output
}
# End of Function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.