Is there a way in awk—gawk most likely—to set the record separator RS to empty value to process each character of a string as a separate record? Kind of like setting the FS to empty to separate each character in its own field:
$ echo abc | awk -F '' '{print $2}'
b
but to separate them each as a separate record, like:
$ echo abc | awk -v RS='?' '{print $0}'
a
b
c
The most obvious one:
$ echo abc | awk -v RS='' '{print $0}'
abc
didn't award me (as that one was apparently meant for something else per GNU awk documentation).
Am I basically stuck using for etc.?
EDIT:
@xhienne's answer was what I was looking for but even using that (20 chars and a questionable variable A :):
$ echo abc | awk -v A="\n" -v RS='(.)' -v ORS="" '{print(RT==A?NR:RT)}'
abc4
wouldn't help me shorten my earlier code using length. Then again, how could I win the Pyth code: +Qfql+Q :D.
If you just want to print one character per line, @klashxx's answer is OK. But a
sed 's/./&\n/g'would be shorter since you are golfing.If you truly want a separate record for each character, the best approaching solution I have found for you is:
(use
gawk; your input character is inRT, not$1)[update] If
RSis set to the null string, it means toawkthat records are separated by blank lines. If I had just definedRS='.', the record separator would have been a mere dot (i.e. a fixed string). But if its length is more than one character, one feature ofgawkis to considerRSas a regex. So, what I did here is to givegawka regex meaning "each character" as a record separator. And I use another feature ofgawk: to retrieve the string that matched the regex in the special variableRT(record terminator)Here is the relevant parts of the
gwakmanual: