OS Distribution: Ubuntu 22.04.3 LTS
gawk version: GNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.1.0, GNU MP 6.2.1)
I have a text file that every once in a while a line will have a significant amount of white space followed by some random text. I am using gawk to search for these rows and make a modification to the left portion of the line.
Input:
formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text
formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text
formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text
random_text
formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text
formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text
random_text
formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text
Expected Output:
formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text
formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text
formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text
WORDWRAP random_text
formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text
formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text
WORDWRAP random_text
formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text formatted_text
This command works and returns the expected output:
gawk '/^[[:space:]]{75}/ { $0 = substr($0,1,15) " WORDWRAP " substr($0,26) }1' input.txt
What I want to be able to do is assign variables to the repeat interval on the interval expression and to the start, length values on the substr function as these values can change depending on the input file.
I set three environment variables:
export PH1="75"; export PH2="15"; export PH3="26"
and then attempt to run this command:
gawk -v gph1="${PH1}" -v gph2="${PH2}" -v gph3="${PH3}" '/^[[:space:]]{gph1}/ { $0 = substr($0,1,gph2) " WORDWRAP " substr($0,gph3) }1' input.txt
which just returns the input unmodified. If I set the repeat interval to the actual value and leave the variables on the substr start and length values:
gawk -v gph2="${PH2}" -v gph3="${PH3}" '/^[[:space:]]{75}/ { $0 = substr($0,1,gph2) " WORDWRAP " substr($0,gph3) }1' input.txt
It works and returns the expected output.
I've have also tried this without success as it just returns the input unmodified:
gawk '/^[[:space:]]{ENVIRON["PH1"]}/ { $0 = substr($0,1,ENVIRON["PH2"]) " WORDWRAP " substr($0,ENVIRON["PH3"]) }1' input.txt
However, this does work when setting the repeat interval to the actual value:
gawk '/^[[:space:]]{75}/ { $0 = substr($0,1,ENVIRON["PH2"]) " WORDWRAP " substr($0,ENVIRON["PH3"]) }1' input.txt
Is there a way to use variables for the repeat intervals in an interval expression?
(Added 2023-09-30) The answer to this question is yes, but not in a regexp constant. Based on the feedback received, I ended up changing the command to this:
gawk -v gph1="${PH1}" -v gph2="${PH2}" -v gph3="${PH3}" ' $0 ~ "^[[:blank:]]{" gph1 "}" { $0 = substr($0,1,gph2) " WORDWRAP " substr($0,gph3) }1' input.txt
MCVE
on Stackoverflow. Please, read How to create a Minimal, Complete, and Verifiable Example.