I have a log file that I process it (by using an awk script, called main.awk) every day and extract some characters, then I write the output of that processes to new file in which the other team members can use that.
In the other hand, I have another file (that I call it input.txt
) that in which there are more than thousand lines and in each line there is a string of 12 characters.
I want to use the first 8 character of input.txt
as an input to one of the functions in my main.awk
.
main.awk
is here:
#!/usr/local/bin/gawk -f
@include "home/user/functions.awk"
{
STRType=substr($0,27+23,2);
TYPEOUT=substr($0,28,3);
if(STRType == "O1")
{
filterTxtforfunc($0,"PRH1DCHK");
}
else
if(STRType=="H6")
{
print substr($0,28)
fflush()
}
else
if(STRType=="J7" && TYPEOUT=="000")
{
print substr($0,28)
fflush()
}
}
I’d checked similar topics but they didn’t help me. I don’t have the possibility to use Python, Jason and other languages and libraries except shell script. I wrote it in bash script , but now I need to use getline
because its not possible to use bash script in awk file.
previously, I wrote bash script just for testing and it works as I want (but I can’t use it in main.awk. I brought here just for clearance. ):
filterTxtforfunc() {
echo does something
}
input_file="input.txt"
while IFS= read -r line; do
# Extract the first 8 characters from the line
code="${line:0:8}"
# Dynamically build the command and execute it
filterTxtforfunc "$0" "$code"
done < "$input_file"
What I’ve used and doesn’t work is:
BEGIN {
input_file="input.txt"
while ( (input_file | getline line) > 0 ) {
ARGV[ARGC++] = line
}
close(input_file)
}
input.txt is like this:
WRM3BHIZ0004
NRB1SAPB0122
LRT1SYGM0114
KRF1LAEI0451
JRU1TEEE0764
ZRQ1LQWS0666
PRH1DCHK0904
...
20
Perhaps something like:
awk '
# ...
function processfile (file, e,line,code) {
for (;;) {
e = getline line <file
if (e<0) exit 1
if (e==0) break
code = substr(line,1,8)
processline(line,code)
}
close(file)
}
# ...
'
Define processline
function. Invoke processfile
with a string containing the name of the file to process.