Bash输出(修剪字符串)
有人能给我解释一下,我到底在修什么?像所有东西一样,头、切口、-d等的意思Bash输出(修剪字符串),bash,Bash,有人能给我解释一下,我到底在修什么?像所有东西一样,头、切口、-d等的意思 classnumber=$(cat "ClassTimetable.aspx?CourseId=156784&TermCode=1620" | tr '\n' '\r' | head -n 1 | cut -d '>' -f1235- | cut -d '<' -f1) classnumber=$(cat“classtimeline.aspx?CourseId=156784&TermCode=16
classnumber=$(cat "ClassTimetable.aspx?CourseId=156784&TermCode=1620" | tr '\n' '\r' | head -n 1 | cut -d '>' -f1235- | cut -d '<' -f1)
classnumber=$(cat“classtimeline.aspx?CourseId=156784&TermCode=1620”| tr'\n'\r'| head-n1 | cut-d'>'-f1235-| cut-d'您正在修剪
\n\r
一线
获取字段,1,2,3,5(直到行尾)。“>”是分隔符
从3的结果中,得到第一列(“cat”classtimeline.aspx?CourseId=156784&TermCode=1620”|\
tr'\n'\r'|#用回车符替换换行符
head-n1 |#取第一行的内容
切-d'>'-f1235-|#在1235'>'之后取所有东西,直到行尾
cut-d'-f12-| cut-d'那么你想让我们解释你的代码吗?是的,我厌倦了猜测应该使用哪些数字,因为我不理解这个概念。感谢学习为每个程序使用man
页面,即man cut
。好的手册页面在底部附近有一个“示例”部分。还要学会一次测试一个cmd,即echo a,b,c | cut-t,-f2
然后添加第二个管道程序等进行实验。祝你好运。这看起来非常像是从HTML片段中刮取单个值的无法维护的尝试。不要为此使用正则表达式或面向行的工具。OP不是修剪\n
和\r
,而是替换-
不是以字段1、2、3、5开头,而是以字段1235开头。您给我提供了一个很好的实验,我有点理解,但是为什么-f12-这个“>”介于12和13之间,而输出是12>13>?除非0是一个有效的计数数?针对我的问题,我如何定义像第235行这样的值?如果您需要获得一个特定的line如果您想要第2行,而不是head-n1
,那么您可能希望使用awk'NR==2'
作为ex。
cat "ClassTimetable.aspx?CourseId=156784&TermCode=1620" | \
tr '\n' '\r' | # replace Line Feed with Carriage Return
head -n 1 | # take what's in the first line
cut -d '>' -f1235- | # take everything after the 1235th '>' until end of line
cut -d '<' -f1) # take the first chunk before the first '<'
echo "1>2>3>4>5>6>7>8>9>10>11>12>13><14>15" | cut -d '>' -f12- | cut -d '<' -f1