如何编写脚本来实现以下逻辑?
How to write the script to implement the below logic?
我正在学习 bash 脚本,我试图编写一个脚本来解决问题,但没成功。
示例测试用例:
Input:
StoreId,Name,Type,Revenue,StoreExpenses (this line is not provided as cmd line argument)
1,RockDeptStore,stationary,100,50
2,WembleyStore,departmental,85,81
3,HealthyStore,grocery,95,97
4,Ministore,medical,60,55
Output:
1|RockDeptStore|stationary|100|50|50
4|Ministore|medical|60|55|5
2|WembleyStore|departmental|85|81|4
script.sh:
#!/bin/bash
#inputs
for record in "$@"
do
revenue=$(cut -d ',' -f 4 <<< $record)
expenses=$(cut -d ',' -f 5 <<< $record)
((profit=revenue-expenses))
if [[ profit -gt 0 ]]
then
# how to update this record with '|' and where to store this record so that I can access it later in my script for sorting.
fi
done
我需要编写一个 shell-脚本 script.sh
,它将每个 商店详细信息 的输入作为 命令行参数.
我需要使用 附加字段 profit = Revenue - StoreExpenses
打印所有商店,并且需要将分隔符从“,”更改为“|”。
并且只打印那些 profit > 0
在 降序 中它们各自 profit
的商店,如示例 输出 以上。
我们 运行 script.sh
为:
./script.sh 1,RockDeptStore,stationary,100,50 2,WembleyStore,departmental,85,81 3,HealthyStore,grocery,95,97 4,Ministore,medical,60,55
您可以使用字符串替换来替换每行中的所有逗号
模式是:${parameter//pattern/string}
参见子字符串替换 at this link
所以在你的情况下,${record//,/|}
然后,您可以将利润 > 0 的每次迭代保存到一个变量中,在末尾添加利润列。您可以使用相同的变量并每次附加一个换行符。
最后,sort
行。
-r
选项反转排序。
-t
和 -k
选项一起查找每行的第六项,其中各项由 |
分隔,并进行相应排序。
所以总的来说它可能看起来像这样:
#!/bin/bash
result=''
newline=$'\n'
#inputs
for record in "$@"
do
revenue=$(cut -d ',' -f 4 <<< $record)
expenses=$(cut -d ',' -f 5 <<< $record)
((profit=revenue-expenses))
if [[ profit -gt 0 ]]
then
newRecord=${record//,/|}
result+="${newRecord}|${profit}${newline}"
fi
done
sorted=$(sort -rt'|' -k6 <<< ${result})
printf "${sorted}"
我必须对您的脚本进行一些额外的更改才能使其适用于我:
gt
-> -gt
- 在剪切命令中添加了
<<< ${record}
您可以使用 sort
实用程序对输出进行排序。
#!/usr/bin/env bash
for element; do
IFS=, read -r _ _ _ revenue store_expenses <<< "$element"
profit=$(( revenue - store_expenses ))
if (( profit > 0 )); then
output=${element//,/|}
printf '%s|%s\n' "$output" "$profit"
fi
done | sort -rt'|' -k 6
您可以 运行 带有参数的脚本。
./script.sh 1,RockDeptStore,stationary,100,50 2,WembleyStore,departmental,85,81 3,HealthyStore,grocery,95,97 4,Ministore,medical,60,55
我正在学习 bash 脚本,我试图编写一个脚本来解决问题,但没成功。
示例测试用例:
Input:
StoreId,Name,Type,Revenue,StoreExpenses (this line is not provided as cmd line argument)
1,RockDeptStore,stationary,100,50
2,WembleyStore,departmental,85,81
3,HealthyStore,grocery,95,97
4,Ministore,medical,60,55
Output:
1|RockDeptStore|stationary|100|50|50
4|Ministore|medical|60|55|5
2|WembleyStore|departmental|85|81|4
script.sh:
#!/bin/bash
#inputs
for record in "$@"
do
revenue=$(cut -d ',' -f 4 <<< $record)
expenses=$(cut -d ',' -f 5 <<< $record)
((profit=revenue-expenses))
if [[ profit -gt 0 ]]
then
# how to update this record with '|' and where to store this record so that I can access it later in my script for sorting.
fi
done
我需要编写一个 shell-脚本 script.sh
,它将每个 商店详细信息 的输入作为 命令行参数.
我需要使用 附加字段 profit = Revenue - StoreExpenses
打印所有商店,并且需要将分隔符从“,”更改为“|”。
并且只打印那些 profit > 0
在 降序 中它们各自 profit
的商店,如示例 输出 以上。
我们 运行 script.sh
为:
./script.sh 1,RockDeptStore,stationary,100,50 2,WembleyStore,departmental,85,81 3,HealthyStore,grocery,95,97 4,Ministore,medical,60,55
您可以使用字符串替换来替换每行中的所有逗号
模式是:${parameter//pattern/string}
参见子字符串替换 at this link
所以在你的情况下,${record//,/|}
然后,您可以将利润 > 0 的每次迭代保存到一个变量中,在末尾添加利润列。您可以使用相同的变量并每次附加一个换行符。
最后,sort
行。
-r
选项反转排序。
-t
和 -k
选项一起查找每行的第六项,其中各项由 |
分隔,并进行相应排序。
所以总的来说它可能看起来像这样:
#!/bin/bash
result=''
newline=$'\n'
#inputs
for record in "$@"
do
revenue=$(cut -d ',' -f 4 <<< $record)
expenses=$(cut -d ',' -f 5 <<< $record)
((profit=revenue-expenses))
if [[ profit -gt 0 ]]
then
newRecord=${record//,/|}
result+="${newRecord}|${profit}${newline}"
fi
done
sorted=$(sort -rt'|' -k6 <<< ${result})
printf "${sorted}"
我必须对您的脚本进行一些额外的更改才能使其适用于我:
gt
->-gt
- 在剪切命令中添加了
<<< ${record}
您可以使用 sort
实用程序对输出进行排序。
#!/usr/bin/env bash
for element; do
IFS=, read -r _ _ _ revenue store_expenses <<< "$element"
profit=$(( revenue - store_expenses ))
if (( profit > 0 )); then
output=${element//,/|}
printf '%s|%s\n' "$output" "$profit"
fi
done | sort -rt'|' -k 6
您可以 运行 带有参数的脚本。
./script.sh 1,RockDeptStore,stationary,100,50 2,WembleyStore,departmental,85,81 3,HealthyStore,grocery,95,97 4,Ministore,medical,60,55