BASH-随机播放文件中字符串中的字符 [英] BASH - Shuffle characters in strings from file
本文介绍了BASH-随机播放文件中字符串中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个具有以下结构的文件(filename.txt
):
I have a file (filename.txt
) with the following structure:
>line1
ABC
>line2
DEF
>line3
GHI
>line4
JKL
我想对不以>
开头的字符串中的字符进行混洗.输出(例如)如下所示:
I would like to shuffle the characters in the strings that do not start wit >
. The output would (for example) look like the following:
>line1
BCA
>line2
DFE
>line3
IHG
>line4
KLJ
这是我尝试将字符串中的字符随机播放的内容:sed 's/./&\n/' | shuf | tr -d "\n"
.看起来好像可行,但没有考虑换行符.而且,它对所有数据执行命令,不仅对不以>
开头的行执行命令.
This is what I tried to shuffle the characters in a string: sed 's/./&\n/' | shuf | tr -d "\n"
. It looks like it works but it does not take into account newlines. Moreover it executes the command on all data and not only on lines that do not start with >
.
推荐答案
使用perl
和ruby
$ # split// to get individual characters
$ # join "" to join characters with empty string
$ # if !/^>/ to apply the change only for lines not starting with >
$ # alternate: perl -MList::Util=shuffle -lne 'print /^>/ ? $_ : shuffle split//'
$ perl -MList::Util=shuffle -lpe '$_=join "", shuffle split// if !/^>/' ip.txt
>line1
CBA
>line2
FED
>line3
IHG
>line4
JKL
$ # $_.chars to get individual characters
$ # * "" to join array elements with empty string
$ ruby -lpe '$_ = $_.chars.shuffle * "" if !/^>/' ip.txt
>line1
BAC
>line2
EDF
>line3
GHI
>line4
JKL
这篇关于BASH-随机播放文件中字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文