我有这个任务
FILE变量是一个正好包含30行的文本文件的名称。每行只包含一个单词,由大小写字母和数字组成。
更改此文件,使所有数字从单词中消失,所有字母变为小写,修改后的单词按字母顺序升序排列。修改后的文件将具有三列独立表的格式,其中第一列将包含排序后的前10个单词,第二列为第二个10,其余为第三个。对文件的行进行编号。
input:
hUeY65u
9sjKdfh
A56oi4
22cfe4HW
Gws6K
zh71p6t
...
output:
1 aoi gwsk sjkdfh
2 cfehw hueyu zhpt
...
我创建了这个脚本,它对我有效,但不在学校服务器上。你不知道问题出在哪里。谢谢
cat $FILE | tr '[:upper:]' '[:lower:]' | tr -d "[0-9]" | sort >file1.txt;
split -l 10 file1.txt; paste xaa xab xac >file2.txt; nl file2.txt;
Stderr mismatch:
-----expected-----
--------got-------
/home/blxrpcdcgb/Student: line 1: $FILE: ambiguous redirect
paste: xaa: No such file or directory
------------------
'"$HOME"/file1.txt' is excessive.
'"$HOME"/file2.txt' is excessive.
'"$HOME"/kfekd jojny' has incorrect content:
-----expected-----
1 azkxbj ikrmp rynlv
2 bledfoa jttknol sipei
3 cdquayr jxphn tatuqun
4 csglb llnsly vjyqfl
5 dwuzei loxrha vpjjhqf
6 egs moxia wbbvk
7 enzvxg ozaww wmiik
8 fuvks ph wpy
9 gnbjr phcupa xsovhv
10 hdjufth pisza zuumxy
--------got-------
1sipei
Vpjjhqf
moxIA
wmiik96
9ph32
ozaWW
egs30
Pisza09
jttknol
hdjufTH
ZuumXY
llnsLY
Tatuqun
ikrMP
loxrHA
dwuzei
wbbvk
Xsovhv
PhcuPA
2fuvKS
Wpy09
jxpHN
Bledfoa
VjyqFL
gnbjr
Cdquayr
csglb
8rynLV
azkxBJ
0enzvxg
------------------
您可以使用pr
来排列列。不需要中间文件:
tr [:upper:] [:lower:] < "${FILE}"
| tr -d [:digit:]
| sort
| pr -t3
| nl
或者在一行中:
tr [:upper:] [:lower:] < "${FILE}" | tr -d [:digit:] | sort | pr -t3 | nl
请参阅:https://linux.die.net/man/1/pr
GNU awk的替代解决方案:
awk '{ gsub(/[[:digit:]]/,"",$0); # Strip out the numbers
map[tolower($0)]="" # Create an array with the line at the index in lower case
}
END { PROCINFO["sorted_in"]="@ind_str_asc"; # Set the array sorting order
for ( i in map ) {
printf "%st",i;cnt++; # Loop the array and print the entries. Also increment a count variable
if (cnt==3 || cnt==6 || cnt==9) { printf "n" # When the count variable is 3,6 or 9 print a new line
}
if (cnt==10) { printf "n";exit # If the count is 10 exit
}
}
}' file
一个衬垫:
awk '{ gsub(/[[:digit:]]/,"",$0);map[tolower($0)]="" } END { PROCINFO["sorted_in"]="@ind_str_asc";for ( i in map ) { printf "%st",i;cnt++;if (cnt==3 || cnt==6 || cnt==9) { printf "n" } if (cnt==10) { printf "n";exit } } }' file
我认为这部分基本上是正确的,但我写了自己的解决方案:
$ cat hash.txt | tr -d '[0-9]' | tr [:upper:] [:lower:] | sort | pr -t3 | nl
1 aoi gwsk sjkdfh
2 cfehw hueyu zhpt