Operations on awk fields

Yes, you can perform any operations you like on the fields. For example:

$ cat file | awk -F[/.] '{n = split($(NF-1),a,/_/); print $(NF-1)" "a[n]}'
file_name_delimited_by_underscore_123 123
another_example_456 456

Of course, you don't need cat here; you could have awk read the file directly - and since the default output field separator OFS is a space, it would be more idiomatic to write the results as separate output fields instead of a string concatenation:

awk -F[/.] '{n = split($(NF-1),a,/_/); print $(NF-1), a[n]}' file

With any sed:

$ sed 's:.*/\(.*_\(.*\)\)\..*:\1 \2:' file
file_name_delimited_by_underscore_123 123
another_example_456 456

You can use sed:

$ sed -e 's;^.*/\(.*_\)\([0-9]*\)\.[^\.]*$;\1_\2 \2;' file
file_name_delimited_by_underscore_123 123
another_example_456 456

^.*/ deletes the path.

\(.*_\) captures the name until the last underscore.

\.[^\.]* removes the extension.

\1\2 \2 replace by the captured groups.

Tags:

Awk