Home > Net >  How to print the digit in the second decimal place for the numbers in column 2. UNIX
How to print the digit in the second decimal place for the numbers in column 2. UNIX

Time:11-18

Okay, so im new to Unix and I am trying to learn the basics of Unix. I have a file called adafile that im trying to configure and just toy around with. I basically want to print the digits in the second decimal place for the numbers in column 2 (which is 1,3,5,6,4,6,2,4).

File Output:
3.0 .91 7 36 January JAN Wayne Smith
2.8 .83 4 17 February FEB Eric Kittle
5.1 .95 8 15 March MAR Morgan Leach
9.0 .76 7 19 April APR Jane Holder
3.5 .84 4 43 October OCT Victor Carter
5.2 .98 6 13 November NOV Michael John
4.5 .86 2 8 December DEC Harry Kaka
5.7 .96 4 13 September SEP Tim Simple

How do I go about getting this done without the use of scripts? I tried using the command: nawk '{print $2}' adafile

Output:
.91
.83
.95
.76
.84
.98
.86
.96
  • but I know it would show all the content in the 2nd column.

Should I be using the nawk command or is there a better command for this?

If I could get any help I much appreciate it!

CodePudding user response:

gawk '$_ = substr($(_ =  _),_ --_,_--)'
nawk '$-__= substr($(_=NF),    _,!__)' FS='^.*[.]'
mawk '$!NF = substr($(_=NF),  _,!__)'  FS='^.*[.]'   
1
3
5
6
4
8
6
6

CodePudding user response:

I would harness GNU AWK following way, let file.txt content be

File Output:
3.0 .91 7 36 January JAN Wayne Smith
2.8 .83 4 17 February FEB Eric Kittle
5.1 .95 8 15 March MAR Morgan Leach
9.0 .76 7 19 April APR Jane Holder
3.5 .84 4 43 October OCT Victor Carter
5.2 .98 6 13 November NOV Michael John
4.5 .86 2 8 December DEC Harry Kaka
5.7 .96 4 13 September SEP Tim Simple

then

awk 'NR>1{print $2*100}' file.txt

gives output

1
3
5
6
4
8
6
6

Explanation: for lines beyond 1st (NR>1) multiply 2nd field value by 100 then take remainder of division (%) by 10.

(tested in GNU Awk 5.0.1)

  • Related