awk question: replacing "%d%s" by "%d %s"

Tom Limoncelli tal at whatexit.org
Thu Jan 13 06:31:29 UTC 2011


On Thu, Jan 13, 2011 at 12:28 AM, Polytropon <freebsd at edvax.de> wrote:
> I have strings of the form either "<number(s)>" or
> "<number(s)><letter>". I catch them with
...

> where "nr" is the name of the string. What I need
> is a simple space between <number(s)> and <letter>,
> so for example "12a" would get "12 a", "6d" would
> get "6 d", and "58" would stay unchanged. I've tried

This feels like it could be faster, but is reasonable:

$ cat data.txt
1
12
3
1d
1dc
12d
12dc
123d
123dc
123dcb
$ cat control.txt
1
12
3
1 d
1 dc
12 d
12 dc
123 d
123 dc
123 dcb
$ awk < data.txt > experiment.txt '{ num = $1 ; sub(/[^0-9]+$/, "",
num) ; lets = $1 ; sub(/^[0-9]+/, "", lets); print num " " lets }' ;
diff -cw control.txt experiment.txt
$  # The above puts a space at the end of the first 3 lines.  If that
is bad, try:
$ awk < data.txt > experiment.txt '{ num = $1 ; sub(/[^0-9]+$/, "",
num) ; lets = $1 ; sub(/^[0-9]+/, "", lets); if (length(lets)) { print
num " " lets } else { print num } }' ; diff control.txt experiment.txt
$

Tom

-- 
http://EverythingSysadmin.com  -- my blog (new posts Mon and Wed)
http://www.TomOnTime.com -- my advice (more videos coming soon)


More information about the freebsd-questions mailing list