写一些“if-statement”

时间:2017-12-07 22:16:45

标签: bash shell if-statement grep

我想在“if-statement”中编写一个脚本

./simulations/文件夹中有很多.html文件,并在html文件中写入结束余额,如下所示:

'结束余额:1000.00000000(0.00%)'

'结束余额:19.21977440(-98.08%)'

'结束余额:1135.80974233(13.58%)'

我只想找到'结束余额:.......(...%)'

如果结束余额为1000或更低回声'结束余额100 0r lwss',如果它更高,'回声结束余额大于1000'。

像这样:

@RestController @RequestMapping("/api") public class PersonResource { @GetMapping("/persons/{name}") @Timed public ResponseEntity<List<Person>> getPersons(@ApiParam Pageable pageable, @PathVariable String name) { log.debug("REST request to get Person by : {}", name); Person person = new Person(); person.setFirstname(name); Page<Person> page = personRepository.findAll(Example.of(person), pageable); HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/persons"); return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK); }

3 个答案:

答案 0 :(得分:0)

如果你这样做:

egrep -n 'end balance: [0-9.]' <<< 'end balance: 1135.80974233 (13.58%)'

你会得到:

1:end balance: 1135.80974233 (13.58%)

要比较数字,你必须左右两侧有数字,所以我建议在if-statement:

下面
if (( $(grep -o 'end balance: [0-9]\{1,\}' ./simulations/*.html | awk '{print $3}') > 1000 ))

如果要对./simulations/中的每个文件执行此操作,请使用循环:

IFS=$'\n'
for i in "$( ls -1 ./simulations/ | grep html$ )"
do
    if (( $(grep -o 'end balance: [0-9]\{1,\}' "$i" | awk '{print $3}') > 1000 ))
    then
        echo "end balance bigger than 1000"
    else
        echo "end balance less than 1000"
    fi
done

命令下方:

grep -o 'end balance: [0-9]\{1,\}' <<< 'end balance: 1135.80974233 (13.58%)' | awk '{print $3}

现在打印: 1135

答案 1 :(得分:0)

您的条件失败,因为命令替换的输出不是整数。它也没有在每个文件的基础上应用。

在元代码中,您正在寻找的内容可能更多:

for every HTML file, loop
  Check the balance at the end of the file.
    If it's greater than 1000, do one thing,
    Otherwise, do something else.
end loop

在使用grep的bash中,这可能类似于:

for f in simulations/*.html; do
    if egrep -q 'end balance: (1000|[0-9]{3})\>' "$f"; then
        echo "$f: end balance > 1000"
    else
        echo "$f: end balance <= 1000 or missing"
    fi
done

如果你想从 numeric 比较文件中提取数字而不是使用正则表达式,那么这可能会有效:

for f in simulations/*.html; do
    n="$(egrep -o 'end balance: [0-9]+' "$f" | grep -o '[0-9]*')"
    if [[ -z "$n" ]]; then
        echo "$f: no balance found"
    elif [[ $n -gt 1000 ]]; then
        echo "$f: end balance > 1000"
    else
        echo "$f: end balance <= 1000"
    fi
done

在这两种情况下,您将为每个处理的HTML文件获取一行输出。

答案 2 :(得分:0)

感谢您回复主人

EB=$(grep 'end balance:' ./simulations/*.html) if [[ $(cut -d. -f1 <<<"$EB" | tr -cd [0-9]) -gt 1000 ]]; then echo "end balance bigger than 1000" else echo "end balance less than 1000" fi

ı使用此