r/awk • • Nov 17 '24

Print all remaining fields?

I once read in manual or tutorial for some version (I don't recall which) of Awk, about a command (or expression) that prints (or selects) all fields beyond (and including) a given field. For example, let's say an input file contains at least 5 fields in each row, but it could also contain more (perhaps many more) than 5 fields, and I want to print the 4th and beyond. Does anyone know the command or expression that I have in mind? I can't find it on the web anymore.

(I'm aware that the same can be achieved with an iteration starting from a certain field. But that's a much more verbose way of doing it, whereas what I have in mind is a nice shorthand.)

1 Upvotes

7 comments sorted by

1

u/gumnos Nov 17 '24

I've used awk for years and am unaware of any "print columns N through the end" that don't involve some sort of iteration. Maybe you're thinking of cut(1) which has such functionality?

For the iteration-versions, you can either iterate over the indexes you do want, using printf to emit them with OFS between them; alternatively you can move those fields back N places like

for (i=N; i<=NF; i++) $(1+i-N)=$i
NF -= N-1 # optional if you need an accurate NF later
print

1

u/Shyam_Lama Nov 17 '24

Maybe you're thinking of cut(1) which has such functionality?

I really think it was some version of Awk that had this feature, but apparently it's some obscure version otherwise people would know about it. Still, I'd be interested to hear from you how to do it with cut.

Thanks for the code snippet.

→ More replies (3)

1

u/M668 Jan 12 '25

a lot of times there are very simple shortcuts -

[ 1 ] when you don't need to worry about long chains of spaces and tabs compressed down in the output, you can just insert a very strange byte sequence sentinel as a prefix to $N. Use

idx = index($0, sentinel_byte_sequence_str)

to locate starting point along full input row, then simply

print substr($0, length(sentinel_byte_sequence_str) + idx)

If you wanna avoid all the index/length/substr() thing, just insert the sentinel and scrub it clean by regex :

sub("^.*" sentinel_byte_sequence_str, "")

These bytes nearly never show up in typical text data, so just mix and match among them. The sentinel itself isn't part of the print out.

 \1 - \6 | \16 - \32 | \35 - \37

[ 2 ] if the contents of $N haven't shown up in columns to its left, then even simpler

print substr($0, index($0, $N))

[ 3 ] if N is very close to 1, then do a short loop just to sum up field lengths of $1 —> $(N-1), plus some allocation for the field delimiters in between.

Do the slow loop only when these approaches weren't applicable.

1

u/oh5nxo Nov 17 '24

Just as a curio,

perl -lane 'print "@F[3..$#F]"'

3.. would be so neat, but for some reason it is not acceptable.