shell - how to format simultaneously a string and floating number in awk? -


i have column follows:

ifile.txt 1.25 2.78 ? ? 5.6 3.4 

i format floating points decimal , skipping strings is.

ofile.txt 1 3 ? ? 6 3 

walter a, f. knorr , janez kuhar suggested nice scripts per question , need of command

awk '{printf "%d%s\n", $1}' ifile.txt 

again, found have number of columns, however, other columns don't need formatting. have use above command in form of like:

awk '{printf "%5s %d%s %5s %5s %5s\n", $1, $2, $3, $4, $5}' ifile.txt 

for example:

ifile.txt 1  1.25   23.2    34  3.4 2  2.78   22.0    23  1.2 3     ?      ?     ?  4.3 4     ?      ?     ?  6.5 5   5.6   45.0     5  2.4 6   3.4   43.0    23  5.6 

i used following command again suggested f. knorr in answer,

awk '$2~/^[0-9]+\.?[0-9]*$/{$2=int($2+0.5)}1' ifile.txt > ofile.txt  ofile.txt 1  1 23.2    34  3.4 2  3 22.0    23  1.2 3  ?      ?     ?  4.3 4  ?      ?     ?  6.5 5  6 45.0     5  2.4 6  3 43.0    23  5.6 

it works fine, need format it.

ofile.txt 1  1   23.2    34  3.4 2  3   22.0    23  1.2 3  ?      ?     ?  4.3 4  ?      ?     ?  6.5 5  6   45.0     5  2.4 6  3   43.0    23  5.6 

you first check whether column contains number (via regex) , handle printing accordingly:

 awk '$1~/^[0-9]+\.?[0-9]*$/{printf "%i\n",$1+0.5; next}1' test.txt 

update: if n-th column needs formatted described above (and no other formatting in other columns), replace $1 $n in following command:

awk '$1~/^[0-9]+\.?[0-9]*$/{$1=int($1+0.5)}1' test.txt 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -