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

관련 정보