簡體   English   中英

文件中的變量

[英]Variables from file

文本文件具有以下結構:

paa pee pii poo puu
baa bee bii boo buu
gaa gee gii goo guu
maa mee mii moo muu

在腳本中逐行讀取它是通過

while read LINE; do 
    ACTION
done < FILE

我需要將每行的參數3和4放入ACTION的變量中。 如果這是手動輸入,則$ 3和$ 4可以解決問題。 我以為awk是工具,但我只是無法將語法包住。

read做就好了。 向其傳遞多個變量,它將在$IFS上拆分為多個字段。

while read -r one two three four five; do
    action "$three" "$four"
done <file

我添加了-r選項,因為通常這就是您想要的。 默認行為是使用受限的遺留問題。

謝謝三胞胎。 同時,我管理了一個適當的通用解決方案:

#!/bin/sh

if [ ! $1 ]; then
    echo "Which inputfile?"
    exit 
elif [ ! $2 -o ! $3 ]; then
    echo "Two position parameters required"
    exit
fi

if [ -f outfile ]; then
    mv outfile outfile.old
fi

while read -a LINE; do
    STRING="${LINE[@]}"
    if [ ${LINE[$2-1]} == ${LINE[$3-1]} ]; then # remove comment for strings
#   if [ ${LINE[$(($2-1))]} -eq ${LINE[$(($3-1))]} ]; then # remove comment for integers
        echo $STRING >> outfile
    fi
done < $1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM