awkスクリプトを使用して列を変更する

awkスクリプトを使用して列を変更する

別のスクリプトAを使用して特定の行の特定の列を変更するスクリプトBを作成しようとしています。ただし、スクリプトA自体を実行すると正常に実行されます。ただし、スクリプトB(別のディレクトリでスクリプトAを実行できます)を使用しようとすると、特定の列の代わりに行全体が変更されます。

問題は何ですか?

スクリプト A( 0.MOF.run):

#!bin/sh
echo $pot
mkdir u$pot  
cp u6000/towhee_input u$pot 
cd u$pot
mv towhee_input 1
awk < 1 -v k="-$pot.0" 'NR==12 {$2=k}{print}' > towhee_input   
cd ..

スクリプトB:

#!bin/sh
echo -n "Please Incert the Number of Potencials: "
read c
declare -i b
b=$c+1
rm pot_pid
a=1

while [ $a -lt $b ]
do
   pot=$(awk < data -v k="$a" 'NR==k ')

   . 0.MOF.mkdir
   . 1.MOF.run

   a=`expr $a + 1`   
done

ベストアンサー1

コメントで述べたように、問題を再現することはできません。テストに必要なデータを提供していただくと更新します。これまでに持っているデータを簡単に作成できる方法は次のとおりです。

#!/usr/bin/env bash

## Write a function instead of calling
## an external script. You could also make
## 0.MOF.mkdir a function.
MOF_run() {
    echo "a.sh pot is $pot"
    ## The -p will cause mkdir to fail
    ## silently if the directory exists.
    mkdir -p u$pot  

    ## Run the awk on the target file, no need to cd or copy
    awk < u6000/towhee_input -v k="-$pot.0" 'NR==12 {$2=k}{print}' > u$pot/towhee_input   
}


echo -n "Please Insert the Number of Potentials: "
read c
## Make sure the user entered a number
## and not anything else or an empty string.
while [[ -z $c || ! $c =~ ^[0-9]*$ ]]; do
    echo "Please enter a number."
    echo -n "Please Insert the Number of Potentials: "
    read c
done;
## increase c by one
let c++

rm pot_pid
a=1

while [ $a -lt $c ]
do
   pot=$(awk < data -v k="$a" 'NR==k ')
   echo "b.sh pot is $pot";
   . 0.MOF.mkdir
   ## Call the MOF_run function
   MOF_run

   ## increment $a
   let a++;
done

おすすめ記事