2024-09-27 22:48:43 发布
网友
如果我的文件包含不可预测的元素,并用空格隔开,例如:
ABC123 ABC124 ABC125 ABC321 ABC222 ABC111 ABC333 ABC069 ABC450 ABC595
我怎样才能把它们分别打印在一行呢?(python或grep/awk等)
使用拆分:
a = '''ABC123 ABC124 ABC125 ABC321 ABC222 ABC111 ABC333 ABC069 ABC450 ABC595''' for item in a.split(): print(item)
给出:
试试这个:
如果只有空间:
tr -s ' ' '\n' < file
如果您有空格和制表符,那么按照@mklement0的建议:
或者
awk '{for (i=1;i<=NF;i++) print $i}' file
egrep -o 'ABC[[:digit:]]{3}' file
egrep -o '[[:alnum:]]{6}' file
指令方法:
sed -E 's/\s+/\n/g' testfile
-E选项,允许扩展正则表达式
-E
awk方法:
gsub(regexp, replacement [, target]) Search target for all of the longest, leftmost, nonoverlapping matching substrings it can find and replace them with replacement. The ‘g’ in gsub() stands for “global,” which means replace everywhere.
grep方法:
grep -o '\S*' testfile
\S*-匹配所有非空白序列
\S*
使用拆分:
给出:
^{pr2}$试试这个:
如果只有空间:
如果您有空格和制表符,那么按照@mklement0的建议:
^{pr2}$或者
或者
或者
指令方法:
-E
选项,允许扩展正则表达式awk方法:
^{pr2}$grep方法:
\S*
-匹配所有非空白序列相关问题 更多 >
编程相关推荐