Cheatsheet of the AWK
programming language
Last active
December 20, 2024 18:00
-
-
Save tapyu/ec4d0287ada24e775d1263ffbe6d5a8b to your computer and use it in GitHub Desktop.
AWK cheatsheet
awk 'BEGIN { actions_before_processing }; condition { actions_for_matching_lines }; END { actions_after_processing }' input_file
BEGIN { actions_before_processing }
(optional): Contains actions to be executed before processing any lines from the input file.condition
: It defines a condition for which the current field processed. Examples of conditionals:- A pattern. For instance,
/pattern/ { actions_for_matching_lines }
. - A boolean conditional. For instance,
NR==1 {for(i=1;i<=NF;i++) print i}
.
- A pattern. For instance,
END { actions_after_processing }
: Contains actions to be executed after processing all lines from the input file.
CAVEAT: function arguments are preserved only within its scope. However, other variables are preserved ouside its scope.
FS
: Field Separator - This variable is used to specify the input field separator, i.e., the character or pattern that separates fields in a line. The default value for FS (Field Separator) is a space (" "
)OFS
: Output Field Separator - This variable is used to specify the output field separator, i.e., the character or pattern that separates fields in the output. The default value for OFS (Output Field Separator) is also a space (" "
).RS
: Record Separator. Determines how input records are separated. By default,RS
is set to a newline character (\n
), so each line is treated as a separate record.ORS
: Output record separator. Define which character will separate each record at the output (the default value is a newline,"\n"
).NR
: Number of the current record. We usually run it in front of the AWK command to assure that is will run on that specific line.NF
: Number of fields that the current record contains. Hence,print $NF
prints the last field of the line.$n
:n
th field of the current record.$0
get the whole record.
BEGIN { commands }
: the specialBEGIN
block, which only runs at the beginning of the file.END { commands }
: the specialEND
block, which only runs at the end of the file.next
: skips the rest of the processing for this record and moves on to the next line.getline
: read the next input record from the input stream.break
: break the loopfunction
: a function
==:
Equal to!=:
Not equal to~:
Matches a regular expression!~:
Does not match a regular expression&&
And operator||
Or operator
match
: returns the position of the first occurrence of the specified regular expression pattern within the string.index
: find the position of the first occurrence of a specified substring within a string. E.g.,index(string, substring)
. if the substring is not found, the function returns 0.split
: based on a string separator, split a scalar string into a array of strings. E.g.split(str, arr, " ")
.sub
: substitutes in-place the first occurrence of a pattern with a replacement string. E.g.,sub(/apple/, "orange", fruit)
.gsub
: substitutes all occurrences of a pattern with a replacement stringsubstr
toupper
tolower
length
printf
: print formated stringsprintf
: instead of printing out, save the formated string to a variable, e.g.,per_EbN0_dB = sprintf("per_EbN0_dB(%s) = all_monte_carlo_runs;\n", current_ebn0);
.print
: print the arguments passed to it. If no args are passed, print the whole line then.print
is usually used without parensis, e.g.,print $2
.
Define separator (Field Separator, FS) | 'BEGIN { FS = "," } ; { print $2 }' |
Define separator (Field Separator, FS) | awk -v FS=: '{print $1}' |
Define separator (Field Separator, FS) | awk '{print $1}' FS=: |
Define separator (Field Separator, FS) | awk -F: '{print $1}' |
Matching patterns (ternary operator) | '{print ($0 ~ /pattern/) ? text_for_true : text_for_false}' • $0 represents the wole line.• $0 ~ /pattern/ means "does $0 contain pattern ?" • The else statement is mandatory. |
For loop | 'for(i=1;i<=NF;i++){ loop statement }' |
Save a element for future use | 'split($n,a,"delimiter")'
• $n means the nth field • The content of the nth is stored into the array a. • The value of "delimiter" is used to split the element into an array.
|
Matching patterns (if-else statement) | '{if ($0 ~ /pattern/) {then_actions} else {else_actions}}' • The else statement if optional |
search and replace | awk '{sub(/{OLD_TERM}/,{NEW_TERM}); print}' {file} |
search and replace in-place | awk -i inplace ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
awk '{ORS=/ION/ ? "\n" : " "; print $0}' | |
# set the output record separator (ORS) to a space if the current line does not contain "ION", thus eliminating unwanted breaklines |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\newglossaryentry{gnss}{ | |
type=\acronymtype, | |
name={GNSS}, | |
description={Global navigation satellite system}, | |
first={global navigation satellite system (GNSS)}, | |
sort={GNSS}, | |
long={global navigation satellite system}, | |
short={GNSS} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\newglossaryentry{afsk}{ | |
type=\acronymtype, | |
name={AFSK}, | |
description={Audio Frequency-Shift Keying.}, | |
first={Audio Frequency-Shift Keying (AFSK)}, | |
sort={AFSK}, | |
long={audio frequency-shift keying}, | |
short={AFSK}, | |
} | |
\newglossaryentry{agc}{ | |
type=\acronymtype, | |
name={AGC}, | |
description={Automatic Gain Control.}, | |
first={Automatic Gain Control (AGC)}, | |
sort={AGC}, | |
long={automatic gain control}, | |
short={AGC}, | |
} | |
\newglossaryentry{ai}{ | |
type=\acronymtype, | |
name={AI}, | |
description={Artificial Intelligence.}, | |
first={Artificial Intelligence (AI)}, | |
sort={AI}, | |
long={artificial intelligence}, | |
plural={}, | |
short={AI}, | |
} | |
\newglossaryentry{altboc}{ | |
type=\acronymtype, | |
name={AltBOC}, | |
description={Alternative BOC.}, | |
first={AltBOC (Alternative BOC)}, | |
sort={AltBOC}, | |
long={alternative boc}, | |
short={AltBOC}, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*************************** Starting simulation for satellite 0 POPACS 2 *************************** | |
-> Running Eb/N0 = 6 dB with 10 Monte Carlo simulations | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 1th Monte Carlo simulation: | |
BER = 1.7354e-03 | |
Number of errors = 814 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 2th Monte Carlo simulation: | |
BER = 1.6927e-03 | |
Number of errors = 794 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 3th Monte Carlo simulation: | |
BER = 1.7354e-03 | |
Number of errors = 814 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 4th Monte Carlo simulation: | |
BER = 1.7908e-03 | |
Number of errors = 840 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 5th Monte Carlo simulation: | |
BER = 1.6671e-03 | |
Number of errors = 782 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 6th Monte Carlo simulation: | |
BER = 1.6885e-03 | |
Number of errors = 792 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 7th Monte Carlo simulation: | |
BER = 1.6160e-03 | |
Number of errors = 758 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 8th Monte Carlo simulation: | |
BER = 1.6927e-03 | |
Number of errors = 794 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 9th Monte Carlo simulation: | |
BER = 1.6757e-03 | |
Number of errors = 786 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 10th Monte Carlo simulation: | |
BER = 1.5989e-03 | |
Number of errors = 750 | |
Numer of bits = 469064 | |
-> Running Eb/N0 = 7 dB with 10 Monte Carlo simulations | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 1th Monte Carlo simulation: | |
BER = 4.3064e-04 | |
Number of errors = 202 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 2th Monte Carlo simulation: | |
BER = 3.7948e-04 | |
Number of errors = 178 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 3th Monte Carlo simulation: | |
BER = 5.1166e-04 | |
Number of errors = 240 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 4th Monte Carlo simulation: | |
BER = 4.4770e-04 | |
Number of errors = 210 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 5th Monte Carlo simulation: | |
BER = 4.4344e-04 | |
Number of errors = 208 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 6th Monte Carlo simulation: | |
BER = 4.3491e-04 | |
Number of errors = 204 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 7th Monte Carlo simulation: | |
BER = 3.9227e-04 | |
Number of errors = 184 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 8th Monte Carlo simulation: | |
BER = 4.3064e-04 | |
Number of errors = 202 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 9th Monte Carlo simulation: | |
BER = 4.6476e-04 | |
Number of errors = 218 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 10th Monte Carlo simulation: | |
BER = 3.9227e-04 | |
Number of errors = 184 | |
Numer of bits = 469064 | |
-> Running Eb/N0 = 8 dB with 10 Monte Carlo simulations | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 1th Monte Carlo simulation: | |
BER = 7.2485e-05 | |
Number of errors = 34 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 2th Monte Carlo simulation: | |
BER = 7.6749e-05 | |
Number of errors = 36 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 3th Monte Carlo simulation: | |
BER = 1.1512e-04 | |
Number of errors = 54 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 4th Monte Carlo simulation: | |
BER = 9.8068e-05 | |
Number of errors = 46 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 5th Monte Carlo simulation: | |
BER = 8.5276e-05 | |
Number of errors = 40 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
fl::CDefaultTerminationHandler::process_is_running. error code: 10 error message: No child processes what: waitpid: No child processes [generic:10] | |
Results of the 6th Monte Carlo simulation: | |
BER = 5.5430e-05 | |
Number of errors = 26 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 7th Monte Carlo simulation: | |
BER = 6.8221e-05 | |
Number of errors = 32 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 8th Monte Carlo simulation: | |
BER = 8.9540e-05 | |
Number of errors = 42 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 9th Monte Carlo simulation: | |
BER = 9.8068e-05 | |
Number of errors = 46 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 10th Monte Carlo simulation: | |
BER = 9.8068e-05 | |
Number of errors = 46 | |
Numer of bits = 469064 | |
-> Running Eb/N0 = 9 dB with 10 Monte Carlo simulations | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 1th Monte Carlo simulation: | |
BER = 4.2638e-06 | |
Number of errors = 2 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 2th Monte Carlo simulation: | |
BER = 1.7055e-05 | |
Number of errors = 8 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 3th Monte Carlo simulation: | |
BER = 2.1319e-05 | |
Number of errors = 10 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 4th Monte Carlo simulation: | |
BER = 8.5276e-06 | |
Number of errors = 4 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 5th Monte Carlo simulation: | |
BER = 8.5276e-06 | |
Number of errors = 4 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 6th Monte Carlo simulation: | |
BER = 4.2638e-06 | |
Number of errors = 2 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 7th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 8th Monte Carlo simulation: | |
BER = 1.2791e-05 | |
Number of errors = 6 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 9th Monte Carlo simulation: | |
BER = 1.2791e-05 | |
Number of errors = 6 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 10th Monte Carlo simulation: | |
BER = 1.2791e-05 | |
Number of errors = 6 | |
Numer of bits = 469064 | |
-> Running Eb/N0 = 10 dB with 10 Monte Carlo simulations | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 1th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 2th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 3th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 4th Monte Carlo simulation: | |
BER = 4.2638e-06 | |
Number of errors = 2 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 5th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 6th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 7th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 8th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 9th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 10th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
-> Running Eb/N0 = 11 dB with 10 Monte Carlo simulations | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 1th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 2th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 3th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 4th Monte Carlo simulation: | |
BER = 4.2638e-06 | |
Number of errors = 2 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 5th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 6th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 7th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 8th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 9th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 10th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
-> Running Eb/N0 = 12 dB with 10 Monte Carlo simulations | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 1th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 2th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 3th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 4th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 5th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 6th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 7th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 8th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 9th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 | |
The satellite name run in Simulnk model is 0 POPACS 2 | |
Results of the 10th Monte Carlo simulation: | |
BER = 0.0000e+00 | |
Number of errors = 0 | |
Numer of bits = 469064 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/awk -f | |
# run: `./sol1.awk prob1.tex` | |
# make sure `sol1.awk` is executable | |
BEGIN { | |
} | |
function capitalize(word) { | |
if (word ~ /and|of|the/) { # if `word` is one of these words, don't captalize it | |
return word | |
} else if (substr(word, 1, 1) == "(") { | |
return "(" toupper(substr(word, 2, 1)) substr(word, 3) | |
} else { | |
return toupper(substr(word, 1, 1)) substr(word, 2) | |
} | |
} | |
function join(arr, sep) { | |
if (sep == "") sep = " " | |
result = arr[1] | |
for (k = 2; k <= length(arr); k++) | |
result = result sep arr[k] | |
return result | |
} | |
function fix(word, initials) { | |
# get the first letter | |
first_letter = substr(word, 1, 1) == "(" ? substr(word, 2, 1) : substr(word, 1, 1) | |
# index returns how many times the second argument is found in the first arguments | |
if (index(initials, toupper(first_letter)) > 0) { | |
return capitalize(word) | |
} else { | |
return word | |
} | |
} | |
/^%/ { | |
print $0 | |
} | |
/^\\newglossaryentry/ { | |
print $0 | |
while (getline) { | |
if ($0 == "}") { | |
print $0 | |
break | |
} else if ($0 ~ /name/ ) { | |
match($0, /name=\{([^}]*)/, arr) | |
initials = arr[1] | |
print $0 | |
# entries that must be corrected | |
} else if ($0 ~ /description=|first=|plural=|firstplural=/){ | |
match($0, /\w+=\{([^}]*)/, arr) | |
content = arr[1] | |
split(content, arr, " ") | |
for (i=1; i<=length(arr); i++) { | |
# if it is a compound noun (e.g, `signal-to-noise`), split it up again | |
if (arr[i] ~ /-/) { | |
split(arr[i], arr1, "-") | |
for (j=1; j<=length(arr1); j++) { | |
arr1[j] = fix(arr1[j], initials) | |
} | |
arr[i] = join(arr1, "-") | |
} else { | |
arr[i] = fix(arr[i], initials) | |
} | |
} | |
final = join(arr, " ") | |
if ($0 ~ /description/ && substr(final, length(final)) != ".") { | |
final = final "." | |
} | |
match($0, /\w+=/, arr) | |
print " " arr[0] "{" final "}," | |
# entries that must not be corrected | |
} else if ($0 !~ /^\}$/) { | |
print $0 | |
# end of \newglossaryentry | |
} else { | |
break | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BEGIN { | |
# this part of the AWK script defines an associative array named `unlowable_names` and populates it with specific strings as keys. In this context, the entries are created without explicitly assigning values. When you use this syntax, AWK implicitly assigns the default value, which is an empty string, to each key. However, for the purpose of checking membership (i.e., if a string exists as a key in the array), the value is not important. | |
unlowable_names["Fourier"] | |
unlowable_names["Baysean"] | |
unlowable_names["Cramér"] | |
unlowable_names["Rao"] | |
unlowable_names["Gaussian"] | |
unlowable_names["Cornell"] | |
unlowable_names["GNSS"] | |
unlowable_names["European"] | |
unlowable_names["TEC"] | |
unlowable_names["Neyman"] | |
unlowable_names["Pearson"] | |
unlowable_names["Rayleigh"] | |
unlowable_names["Taylor"] | |
unlowable_names["BOC"] | |
} | |
function join(arr, sep) { | |
if (sep == "") sep = " " | |
result = arr[1] | |
for (k = 2; k <= length(arr); k++) | |
result = result sep arr[k] | |
return result | |
} | |
function fix(word) { | |
if (word in unlowable_names) { | |
return word | |
} else { | |
return tolower(word) | |
} | |
} | |
/^%/ { | |
print $0 | |
} | |
/^\\newglossaryentry/ { | |
print $0 | |
while(getline) { | |
if ($0 == "}") { | |
# end of the glossary entry | |
print $0 | |
break | |
} else if ($0 ~ /description=/) { | |
match($0, /\w+=\{([^}]*)\./, arr) | |
name = arr[1] # arr[0] returns the whole line | |
I = split(name, words, " ") # split names in spaces | |
for (i = 1; i <= I; i++) { | |
if (words[i] ~ /-/) { # if `words` contains `-` | |
J = split(words[i], parts, "-") | |
for (j = 1; j <= J; j++) { | |
parts[j] = fix(parts[j]) | |
} | |
words[i] = join(parts, "-") | |
} else { | |
words[i] = fix(words[i]) | |
} | |
} | |
name = join(words) | |
print " description={" toupper(substr(name, 1, 1)) substr(name, 2) ".}," | |
} else if ($0 ~ /first=/) { | |
match($0, /\w+=\{([^}]*)/, arr) | |
content = arr[1] | |
# get initials from both patterns | |
if (match(content, /^(.*) \((\w+)\)$/, arr1)) { # Global Navigation Satellite Systems (GNSS) | |
initials = arr1[2] | |
} else if (match(content, /^(\w+) \((.*)\)/, arr1)){ # GNSS (Global Navigation Satellite Systems) | |
initials = arr1[1] | |
} | |
print " first={" join(words) " (" initials ")}," | |
} else if ($0 ~ /long=/) { | |
print " long={" join(words) "}," | |
} else if ($0 ~ /longplural=/) { | |
match($0, /\w+=\{([^}]*)/, arr) | |
name = arr[1] # arr[0] returns the whole line | |
I = split(name, words, " ") # split names in spaces | |
for (i = 1; i <= I; i++) { | |
if (words[i] ~ /-/) { # if `words` contains `-` | |
J = split(words[i], parts, "-") | |
for (j = 1; j <= J; j++) { | |
parts[j] = fix(parts[j]) | |
} | |
words[i] = join(parts, "-") | |
} else { | |
words[i] = fix(words[i]) | |
} | |
} | |
name = join(words) | |
print " longplural={" toupper(substr(name, 1, 1)) substr(name, 2) "}," | |
} else { | |
print $0 | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BEGIN { | |
# Initialize the map for Eb/N0 values. | |
print "per_EbN0_dB = containers.Map('KeyType', 'double', 'ValueType', 'any');\n"; | |
} | |
/The satellite name run in Simulnk model is/ { | |
# Extract satellite name. | |
if (match($0, /The satellite name run in Simulnk model is (.+)/, arr)) { | |
sat_name = arr[1]; | |
} | |
} | |
/^-> Running Eb\/N0/ { | |
# Extract the Eb/N0 value. | |
if (match($0, /Eb\/N0 = ([0-9]+) dB/, arr)) { | |
if (current_ebn0 != "") { | |
# `current_ebn0` is not empty, then it is not the first entry, and there | |
# is a previous Eb/N0 and its BER values to be printed out | |
printf "all_monte_carlo_runs = [%s];\n", ber_values; | |
print per_EbN0_dB; | |
ber_values = ""; # Reset BER values. | |
} | |
# save the current Eb/N0 group | |
current_ebn0 = arr[1]; | |
per_EbN0_dB = sprintf("per_EbN0_dB(%s) = all_monte_carlo_runs;\n", current_ebn0); | |
} | |
} | |
/BER =/ { | |
# Extract the BER value. | |
if (match($0, /BER = ([0-9\.e\-]+)/, arr)) { | |
if (ber_values != "") { | |
ber_values = ber_values "; " arr[1]; | |
} else { | |
ber_values = arr[1]; | |
} | |
} | |
} | |
END { | |
# Finalize the last Eb/N0 group. | |
if (current_ebn0 != "") { | |
printf "all_monte_carlo_runs = [%s];\n", ber_values; | |
printf "per_EbN0_dB(%s) = all_monte_carlo_runs;\n\n", current_ebn0; | |
} | |
# Final struct for the satellite. | |
if (sat_name != "") { | |
printf "BER_per_scenario('%s') = struct(\"per_EbN0_dB\", per_EbN0_dB);\n", sat_name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment