I have a file in which there are more than thousand lines and in each line there is a string of 12 characters. I also have an awk
script in which there is a function. I want the file to be read every time the function is executed and the first 8 characters of each line of this file will be received as input and the function will be executed for each line of the file. 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.
this is my bash script that works as I want (but I can’t use it in awk script file):
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"
The main script is :
#!/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()
}
}
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
...