使用sed命令转换数据
我需要转换使用sed命令转换数据,sed,formatting,Sed,Formatting,我需要转换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 "
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
转换字符串,如
xxx#yyy@zzz
到bash中的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"
sed-r的/(.+)#(.+)@(.+)/\U\3\E\1-\2'你能给我一个case语句的例子吗?#脚本从这里开始,如果[$#-gt 0],那么在rootjobs中回显arg do case$arg的“Joe Fosteson”日期回显)回显“Root正在$(una me)上运行$(ps-U Root-U Root-U Root-U| wc-l)进程”;;学生账户)echo“在$(主机名)上有$(ls-1/home/student/|wc-l)学生账户。”;;transfordata)xxx#yyy@zzz*)用法“不知道如何使用$arg”;;esac echo“******”完成整个sed+cut+awk+paste命令和中间文件都可以使用单个awk命令而不使用临时文件简洁地完成。如果您发布一些示例输入(可能的起始数据值)和预期输出(final
给定输入的内容),我们可以帮助您。