Shell 命令 - tr,sort,paste,nl
Shell command - tr,sort,paste,nl
我有这个作业
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
------------------
GNU awk 的替代解决方案:
awk '{ gsub(/[[:digit:]]/,"",[=10=]); # Strip out the numbers
map[tolower([=10=])]="" # 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 "%s\t",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:]]/,"",[=11=]);map[tolower([=11=])]="" } END { PROCINFO["sorted_in"]="@ind_str_asc";for ( i in map ) { printf "%s\t",i;cnt++;if (cnt==3 || cnt==6 || cnt==9) { printf "\n" } if (cnt==10) { printf "\n";exit } } }' file
您可以使用 pr
来排列列。不需要中间文件:
tr [:upper:] [:lower:] < "${FILE}" \
| tr -d [:digit:] \
| sort \
| pr -t3 \
| nl
或一行:
tr [:upper:] [:lower:] < "${FILE}" | tr -d [:digit:] | sort | pr -t3 | nl
我有这个作业
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
------------------
GNU awk 的替代解决方案:
awk '{ gsub(/[[:digit:]]/,"",[=10=]); # Strip out the numbers
map[tolower([=10=])]="" # 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 "%s\t",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:]]/,"",[=11=]);map[tolower([=11=])]="" } END { PROCINFO["sorted_in"]="@ind_str_asc";for ( i in map ) { printf "%s\t",i;cnt++;if (cnt==3 || cnt==6 || cnt==9) { printf "\n" } if (cnt==10) { printf "\n";exit } } }' file
您可以使用 pr
来排列列。不需要中间文件:
tr [:upper:] [:lower:] < "${FILE}" \
| tr -d [:digit:] \
| sort \
| pr -t3 \
| nl
或一行:
tr [:upper:] [:lower:] < "${FILE}" | tr -d [:digit:] | sort | pr -t3 | nl