This is a good task for Python.
$ cat input.txt a b c A alpha beta 0 B 0 gamma zeta C alpha phi omega D kappa 0 delta $ ./replace_nonzero.py < input.txt a b c A 1 1 0 B 0 1 1 C 1 1 1 D 1 0 1
And here's the replace_nonzero.py itself:
#!/usr/bin/env python from __future__ import print_function import sys for index,line in enumerate(sys.stdin): if index == 0 : print(line.strip()) continue words = line.strip().split() print(words[0],end="\t") new_line = [] for word in words[1:]: if word.isdigit() and int(word) == 0: new_line.append('0') else: new_line.append('1') print("\t".join(new_line))
The way this works is simple: we skip first line since it's special case, and break down all other lines into words, and examine those starting from position 1. The logic here works from the opposite assumption - if the word that we got is a digit and is zero, then we append a string '0' into new list (which is a list of words per each line), otherwise - we append string '1'. Finally we take the new list , and print it out, again - tab-delimited.
000,0.0,-0,0x0,+0e20?