smirkymonkey
smirkymonkey

Reputation: 449

What's wrong with my AWK when coping with the last column?

I write a shell script to abstract data from a file named "POSCAR". It is produced in the win10 system. It looks like this:

System
1.0
       23.0000000000         0.0000000000         0.0000000000
        0.0000000000        23.0000000000         0.0000000000
        0.0000000000         0.0000000000        17.0000000000
    C    H
   24    7
Direct

The 6th and 7th rows are element symbols and number of atoms. I want to get a string = C24H7. So I wrote the script like this:

#!/bin/bash
path=$PWD
fin="POSCAR"
e_tot=`sed -n 6p $fin   |awk '{printf "%.1d", NF }'`
echo There are $e_tot columns.
ele=""
for ii in $(seq 1 1 $e_tot)
do
  echo $ii
  aa=`sed -n 6p $fin |awk -v ll=$ii '{printf "%s", $ll}'`
  mm=`sed -n 7p $fin |awk -v ll=$ii '{printf "%d", $ll}'`
  col=$aa$mm
  ele=$ele$col
done

The output is wield for the last column. I can get C24H, but the "7" is lost. Or it just be exported to the next row. I thought it may be related to the last character of the row, which is produced by windows and not recognized by Linux, and which I don't know is what. BEGIN{FS="[ \n\t]+"} for awk does not work. Where is wrong ? THANK YOU...

Upvotes: 2

Views: 93

Answers (1)

user000001
user000001

Reputation: 33317

With awk:

awk 'NR==6{a=$1;b=$2}NR==7{print a $1 b $2}' file
C24H7

Upvotes: 2

Related Questions