Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I output null-terminated strings in Awk?

Tags:

I'm working on a shell script that will be used by others, and may ingest suspect strings. It's based around awk, so as a basic resiliency measure, I want to have awk output null-terminated strings - the commands that will receive data from awk can thus avoid a certain amount of breakage from strings that contain spaces or not-often-found-in-English characters.

Unfortunately, from the basic awk documentation, I'm not getting how to tell awk to print a string terminated by an ASCII null instead of by a newline. How can I tell awk that I want null-terminated strings?


Versions of awk that might be used:

[user@server1]$ awk --version
awk version 20070501

[user@server2]$ awk -W version
mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan

[user@server3]$ awk -W version
GNU Awk 3.1.7

So pretty much the whole family of awk versions. If we have to consolidate on a version, it'll probably be GNU Awk, but answers for all versions are welcome since I might have to make it work across all of these awks. Oh, legacy scripts.

like image 843
Brighid McDonnell Avatar asked Feb 03 '12 18:02

Brighid McDonnell


People also ask

How do you create a null-terminated string?

To create a null-terminated string, code: String Y delimited by size X'00' delimited by size into N. To concatenate two null-terminated strings, code: String L delimited by x'00' M delimited by x'00' X'00' delimited by size into N.

Are strings terminated by null?

All character strings are terminated with a null character. The null character indicates the end of the string. Such strings are called null-terminated strings. The null terminator of a multibyte string consists of one byte whose value is 0.

What is awk '{ print $2 }'?

awk '{ print $2; }' prints the second field of each line. This field happens to be the process ID from the ps aux output. xargs kill -${2:-'TERM'} takes the process IDs from the selected sidekiq processes and feeds them as arguments to a kill command.

What is a null-terminated byte string?

A null-terminated byte string (NTBS) is a sequence of nonzero bytes followed by a byte with value zero (the terminating null character). Each byte in a byte string encodes one character of some character set.


1 Answers

Alright, I've got it.

awk '{printf "%s\0", $0}'

Or, using ORS,

awk -vORS=$'\0' //
like image 114
Kevin Avatar answered Sep 20 '22 17:09

Kevin