我创建了一个简单的bash脚本,要求用户输入AccountName、DOB、用户名、密码和电子邮件地址,这些详细信息将被重定向到一个名为Register.txt的文本文件中,从这里开始,我试着根据用户名按升序对Register.txt文件的内容进行排序。我尝试了各种方法试图解决这个问题,但我运气不好。我们将非常感谢在这一问题上提供的任何帮助和指导,因为我已经没有任何想法了。
这是我为了解这些细节而编写的bash脚本。
#!/bin/bash
clear
echo "Welcome, This Is The Registration Form Associated With Option 1"
echo "--------------------------------------------"
echo "Please Enter In The Requsted Details Below"
echo "-------------------------------------------- n"
#Taking in the users Registration Information
read -p "Please Enter Your Full name (Forename and Surname): " AccountName
read -p "Please Enter Your Date of Birth in the format (DD/MM/YYY): " DOB
read -p "Please Enter The Username You Would Like To Have While Using The System: " Username
read -p "Please Enter The Password You Want To Associate With This Account: " Password
read -p "Please Enter The Email Address You Want To Associate With This Account: " Email
#Redirecting The Users Account Registration Information to a text file called Register.txt
echo "Full Name: "$AccountName"n""Date Of Birth: "$DOB"n""Account Username:
"$Username"n""Account Password: "$Password"n""Registered Email Address: "$Email >>
Register.txt
#Sorting contents of Regsiter.txt in ascending order by Username
# Code here? #
以下是Register.txt文件的示例内容:
Account Name: Joe Bloggs
Date Of Birth: 31/03/1987
Account Username: JoeyB
Account Password: Test0
Registered Email Address: joebloggs@gmail.com
Account Name: Anna Bloggs
Date Of Birth: 17/11/1975
Account Username: AnnaTheBlogga
Account Password: Test1
Registered Email Address: annaB@hotmail.com
Account Name: Faith Bloggs
Date Of Birth: 01/04/2000
Account Username: FaithThePreacher
Account Password: Test2
Registered Email Address: Faith2000@gmail.com
建议awk
脚本对Register.txt
进行排序
awk '{print $2, $4, $6, $8, $10}' RS="" FS=": |\n" OFS="|" Register.txt | sort
输出:
Anna Bloggs |17/11/1975|AnnaTheBlogga|Test1|annaB@hotmail.com
Faith Bloggs|01/04/2000|FaithThePreacher|Test2|Faith2000@gmail.com
Joe Bloggs|31/03/1987|JoeyB|Test0|joebloggs@gmail.com
说明:
RS=""
将awk
记录分隔符设置为空行。
FS=": |\n"
将awk
字段分隔符设置为newline
或:
。
OFS="|"
将awk
输出字段分隔符设置为|
。
{print $2, $4, $6, $8, $10}
输出偶数awk
字段(2,4,6,8,10(。