我需要使用脚本将xxx#yyy@zzz
转换为ZZZ xxx-yyy
。
我已经在脚本中使用了case语句,所以我应该在case语句之前使用if语句还是应该继续case语句?
这是我目前的剧本
function usage
{
echo "usage: $0 arguments ..."
if [ $# -eq 1 ]
then echo "ERROR: $1"
fi
}
# Script begins here
if [ $# -gt 0 ]
then
echo "Joe Fosteson"
date
echo
for arg
do
case $arg in
rootjobs)
echo "Root is running $(ps -U root -u root u | wc -l) processes on $(una
me)."
;;
student_accounts)
echo "There are $(ls -1 /home/student/ | wc -l) student accounts on $(ho
stname)."
;;
*)
usage "Do not know what to with $arg"
;;
esac
echo "*****"
done
else
usage
fi
这是我使用的sed命令,但格式不正确........
if [ $# -eq 0 ]
then echo "Please enter a file name"
else echo
if test -f $1
then
#s1.sh
starting_data=$1
sed 's/ //' $starting_data > raw_dataz
sed "/^id/ d" raw_dataz > new
cut -f1 -d, new > id1
sed 's/ //' id1 > id2
cut -f2 -d, new > lastname1
sed 's/ //' lastname1 > lastname2
cut -f3 -d, new > firstname1
sed 's/ //' firstname1 > firstname2
awk '{print $0":"}' lastname2 > lastname3
sed -e 's/-//g' id2 > id3
paste -d\ firstname2 lastname3 id3 > final
cat final
else echo "$1 cannot be found"
fi
fi
答案 0 :(得分:0)
要在bash中将xxx#yyy@zzz
之类的字符串转换为ZZZ xxx-yyy
:
str='xxx#yyy@zzz'
if [[ "$str" =~ (.+)#(.+)@(.+) ]]; then
str2="${BASH_REMATCH[3]^^} ${BASH_REMATCH[1]}-${BASH_REMATCH[2]}"
echo "$str2"
else
echo "Bad string format"
fi
在sed
sed -r 's/(.+)#(.+)@(.+)/\U\3\E \1-\2' <<<"$str"