简体   繁体   English

Bash 脚本读取 txt 文件,执行 while 循环并写入 output

[英]Bash script to read txt file, do a while loop and write output

I'm fairly new to bash scripting, and I wrote the following script to read a text file, run a while loop with if statements, and write the data I want into another text file.我对 bash 脚本相当陌生,我编写了以下脚本来读取文本文件,使用 if 语句运行 while 循环,并将我想要的数据写入另一个文本文件。

#!/bin/bash
#
#
#
variable1=0
variable2=0
variable3=0
variable4=0
cat input.txt | while read Column1 Column2 Column3

do
    if ["$Column1" = variable2]; then
        if ["$Column2" > variable3 && "$Column2" < variable4]; then
            if ["$Column3" > variable4]; then
                variable4="$Column3"
                continue
            fi
        else
            echo variable1 variable2 variable3 variable4
            variable1++
            variable3="$Column2"
            variable4="$Column3"
        fi
    else
        echo variable1 variable2 variable3 variable4
        variable1++
        variable2="$Column1"
        variable3="$Column2"
        variable4="$Column3"
    fi

done>output.txt

However when I run the script I get the following error message:但是,当我运行脚本时,我收到以下错误消息:

./code.sh: line 31: syntax error near unexpected token `done'
'/code.sh: line 31: `done>output.txt

I'd really appreciate if anyone could help me with this!如果有人可以帮助我,我将不胜感激!

I don't know what you want to do but there are a lot of things one can fail in bash, i posted a tested version.我不知道你想做什么,但在 bash 中有很多事情可能会失败,我发布了一个测试版本。

这对我有用

ps I was criticized that the code is a picture:)) here you have it for copy paste ps 我被批评说代码是图片:)) 这里你有它用于复制粘贴

#!/bin/bash
let variable1=0
let variable2=0
let variable3=0
let variable4=0
cat input.txt | while read Column1 Column2 Column3


do
    if [ "x$Column1" == "x$variable2" ]; then
        if [ "x$Column2" -gt "x$variable3" ]  &&  [ "x$Column2" -lt "x$variable4" ]; then
            if [ "x$Column3" -gt "x$variable4" ]; then
                let variable4=$Column3
                continue
            fi
        else
            echo $variable1 $variable2 $variable3 $variable4
            let "variable1 += 1"
            let variable3=$Column2
            let variable4=$Column3
        fi
    else
        echo $variable1 $variable2 $variable3 $variable4
        let "variable1 += 1"
        let variable2=$Column1
        let variable3=$Column2
        let variable4=$Column3
    fi

done > output.txt

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM