2

Below command to split files is working fine when i provided absolute path for the output files "print > "/tmp/outputfile.txt"nfile}'" :

awk -v size=$(wc -l < inputfile.txt) -v perc=0.2 '{nfile = int(NR/(size*perc)); if(nfile >= 1/perc){nfile--;  } print > "/tmp/outputfile.txt"nfile}' inputfile.txt

But when i removed absolute path with a variable it is not working, I have tried below commands -

printenv |grep tempdir
tempdir=/tmp

 awk -v size=$(wc -l < inputfile.txt) -v perc=0.2 '{nfile = int(NR/(size*perc)); if(nfile >= 1/perc){nfile--;  } print > ENVIRON["tempdir"]"outputfile.txt"nfile}' inputfile.txt
awk -v size=$(wc -l < inputfile.txt) -v perc=0.2 -v tempdir="/tmp" '{nfile = int(NR/(size*perc)); if(nfile >= 1/perc){nfile--;  } print > "tempdir/outputfile.txt"nfile}' inputfile.txt
awk -v size=$(wc -l < inputfile.txt) -v perc=0.2 '{nfile = int(NR/(size*perc)); if(nfile >= 1/perc){nfile--;  } print > "$tempdir/outputfile.txt"nfile}' inputfile.txt

3 Answers 3

2

ENVIRON only works on variables that are exported or set on the command line. In any case, just use -v to init an awk variable named tempdir with the value of the shell variable of the same name:

awk -v tempdir="$tempdir" ... '{... print > (tempdir"outputfile.txt"nfile)}' inputfile.txt

You had created the variable before but then stuck it inside of a string so it was no longer a variable but literal text.

I added parens around the concatenation that produces your output file name as parenthesizing any expression on the right side of output redirection is required for portability across all awk versions.

Sign up to request clarification or add additional context in comments.

2 Comments

Working fine but when i used below command it worked fine - awk -v size=$(wc -l < inputfile.txt) -v perc=0.2 -v tempdir="/tmp/output.txt" '{nfile = int(NR/(size*perc)); if(nfile >= 1/perc){nfile--; } print > tempdir}' inputfile.txt tempdir variable inside print worked fine without bracket, means whenever we need to use concatenate the string with print we have to use bracket.
Again - parenthesizing any expression on the right side of output redirection is required for portability across all awk versions.. In other words it will work without them in some awks but not all.
1

Better you make use of awk -v somevar="someval"'{....}', meanwhile you can access ENVIRON like below

$ tempdir="/tmp/somefolder" awk 'BEGIN{print ENVIRON["tempdir"]}'
/tmp/somefolder

ENVIRON is associative array that holds all exported environment variables, for example if you want to see what all variables exported in your system, you may use below command, but in your current context -v tempdir="somedir/somepath" suits best.

$ awk 'BEGIN{for (i in ENVIRON)print i,ENVIRON[i]}'
IM_CONFIG_PHASE 1
DBUS_SESSION_BUS_ADDRESS unix:abstract=/tmp/dbus-5xGteFNyU3
SHLVL 1
XDG_SESSION_PATH /org/freedesktop/DisplayManager/Session0
GNOME_DESKTOP_SESSION_ID this-is-deprecated
PWD /home/akshay
...
...
...
CLUTTER_IM_MODULE = xim
XDG_SEAT = seat0
XMODIFIERS = @im=ibus
WINDOWID = 69206026

3 Comments

I tried ENVIRON["tempdir"] also in my solution but it didn't work in my case, learning from Ed Morton solution is - I need to use bracket if we are using concatenation with path variable.
@VIPINKUMAR: Whether this tempdir="/tmp/somefolder" awk 'BEGIN{print ENVIRON["tempdir"]}'` is working for you ?
Yes - it is working for me, looks like we need to declare variable and then use ENVIRON also but from awk man page - ENVIRON use for environment variable that is why i didn't declare variable but tried to use ENVIRON in print stmt.
0

Initial solution is working now(small mistake of quotes)

print > "tempdir/outputfile.txt"nfile ####old
print > tempdir"/outputfile.txt"nfile ####new

   awk -v size=$(wc -l < inputfile.txt) -v perc=0.2 -v tempdir="/tmp" '{nfile = int(NR/(size*perc)); if(nfile >= 1/perc){nfile--;  } print > tempdir"/outputfile.txt"nfile}' inputfile.txt

Another solution after taking idea from Akshay Hegde solution -

awk -v size=$(wc -l < inputfile.txt) -v perc=0.2 -v tempdir="/tmp/" '{nfile = int(NR/(size*perc)); if(nfile >= 1/perc){nfile--;  } print > ENVIRON["tempdir"]"outputfile.txt"nfile}' inputfile.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.