简体   繁体   English

从文件中读取行并标记每一行以提取 shell 脚本中的特定单词

[英]Read lines from a file and tokenize each line to extract specific word in shell script

I wrote a shell script to read all lines from a file, tokenized each line to extract particular word.我编写了一个 shell 脚本来读取文件中的所有行,对每一行进行标记以提取特定的单词。 First line is read correct, but from the second line the expected word is not reading correctly.第一行读取正确,但从第二行开始,预期的单词读取不正确。 Looking forward for your suggestions.期待您的建议。

while read -ra line; 
    do
        netmask="${line[5]}"
        echo "netmask is $netmask" // first line reading is correct, second line onwards its showing 
                                      wrong output
        IFS='.' tokens=($netmask) 
        prefix=0
        for i in 0 1 2 3
        do
            var="${tokens[$i]}"
            # i have a logic to calculate number of 1's in the number.
        done 
done < routes.sh

Contents of route.sh are route.sh 的内容是

route add -net 10.212.220.0 netmask 255.255.255.0 gw 10.213.248.1
route add -net 10.212.220.0 netmask 255.255.128.0 gw 10.213.248.1

Actual output实际产量

netmask is 255.255.255.0
netmask is 128

Expected output预期输出

netmask is 255.255.255.0
netmask is 255.255.128.0

Extra Note:额外注意:

My objective is to (network concept)我的目标是(网络概念)

  1. Read all lines from a file (each line depicts a route)从文件中读取所有行(每行描述一条路线)
  2. From each line extract the netmask (eg: 255.255.255.255)从每一行中提取网络掩码(例如:255.255.255.255)
  3. Tokenize "255.255.255.255" with '.', so we get 4 numbers, calculate the number of 1s in each token, add them up.用“.”标记“255.255.255.255”,所以我们得到 4 个数字,计算每个标记中 1 的数量,将它们相加。 That would be prefix length for the network IP.这将是网络 IP 的前缀长度。

The problem is that you set IFS='.'问题是你设置了IFS='.' , so for the second read on the array line will be incorrectly set. ,因此对于阵列line上的第二次read将被错误设置。

One way to fix it is by saving the original IFS and then resetting it when appropriate.修复它的一种方法是保存原始IFS ,然后在适当的时候重置它。 For example,例如,

OldIFS=$IFS
while read -ra line; do
    netmask="${line[5]}"
    echo "netmask is $netmask"
    IFS='.' tokens=($netmask)
    prefix=0
    for i in 0 1 2 3; do
        var="${tokens[$i]}"
    done
    IFS=$OldIFS
done < routes.sh

You must be careful when changing IFS , otherwise weird things will happen.更改IFS时必须小心,否则会发生奇怪的事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从文件中提取特定行并将其附加到Shell脚本中的另一个现有文件中,然后从原始文件中删除? - how to extract specific lines from file and append it to another existing file in shell script and then delete from original? 使用shell脚本从文件中提取唯一的行块 - extract unique block of lines from a file using shell script 如何从文件中读取特定字符串并将整行保存在 Bash Shell 脚本中的另一个文件中 - How to read a specific string from a file and save the whole line in another file in Bash Shell script 使用 shell 脚本将特定单词从一个文件复制到另一个文件 - Copy specific word from a file to another file using shell script 脚本外壳:grep特定行-提取特定字符串-将它们放在文件中 - Script Shell: grep specific lines - extract specific strings - put them on a file Shell:如何根据每行的第一个单词提取第一个出现? - Shell : How to extract the first occurence based on the first word of each lines? 仅使用Shell脚本从文本文件获取特定行 - Get specific line from text file using just shell script Shell脚本:分别读取属性文件的第一行和其余各行 - Shell Script: Read the first line of properties file separately form the rest of the lines 如何读取文件中每一行的数据并确定其中哪一个数据对应于Shell脚本中的基准 - how to read data on each line in a file and and determine which one of the data correspond to a bench mark in a shell script 如何从特定行启动shell脚本? - How to start a shell script from a specific line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM