1

I am splitting up a file with awk command, I want to name the file using a variable however I have not had much luck. Here is the line:

awk '/STX/ {f="$tp"++i;} {print > f}' $tp.mixed

this just creates files with $tp# as name.

I read the post "How to use shell variables in awk script" but was unable to figure out how to apply that to me question.

Brian Duffy
  • 112
  • 8
  • 2
    Possible duplicate of [How to use shell variables in awk script](http://stackoverflow.com/questions/19075671/how-to-use-shell-variables-in-awk-script) – Etan Reisner Oct 15 '15 at 19:19
  • I saw this before I posted, could not figure out to apply it to my particular question. Thank you – Brian Duffy Oct 15 '15 at 19:22
  • 4
    You don't expand the shell variable **in** the awk script. You use `-v awkvar="$tp"`` to create an awk variable with the value you need and then use the variable `awkvar` in the awk script. `f=awkvar(++i)` or similar. – Etan Reisner Oct 15 '15 at 19:23
  • so like this: awk -v awkvar="$tp" '/STX/ {f=awkvar(++i);} {print > f}' $tp.mixed – Brian Duffy Oct 15 '15 at 19:27
  • I have: awk -v tp=$tp '/STX/ {f=tp++i;} {print > f}' $tp.mixed and I get 0,1,2,3,4,etc, if I use tp(++1) I receive error, function tp not defined. – Brian Duffy Oct 15 '15 at 19:36
  • 1
    `awk -v tp="$tp" '/STX/ {if (f) close(f); f = tp (++i)} {print > f} END{if (f) close(f)}' $tp.mixed` – anubhava Oct 15 '15 at 19:40
  • 1
    Thank you anubhava!! that worked. Question, why do you have to have the if (f) close (f) and END{if (f) close(f)? thank you, trying to understand shell scripting and awk better. – Brian Duffy Oct 15 '15 at 19:44
  • 2
    @BrianDuffy There is a limit on the number of open files you can have. If you explicitly use `close` every time you are done with a file here, you will never reach that limit. – John1024 Oct 15 '15 at 19:50
  • 1
    thank you for explanation @John1024, anabhava, can you add as an answer so I can mark this solved? – Brian Duffy Oct 15 '15 at 19:55

2 Answers2

3

You can use this awk command to pass variable from command line:

awk -v tp="$tp" '/STX/{close(f); f=tp (++i)} f{print > f} END{close(f)}' "$tp.mixed"

Also important is to close the files you're opening for writing output. By calling close we are avoiding memory leak due to large number of opened files.

anubhava
  • 761,203
  • 64
  • 569
  • 643
0

anubhava answered my question in the comments:

awk -v tp="$tp" '/STX/ {if (f) close(f); f = tp (++i)} {print > f} END{if (f) close(f)}' $tp.mixed

works

thank you everyone for your help

Brian Duffy
  • 112
  • 8