有没有更好的解决方案来反转uniq count

时间:2018-01-02 04:15:55

标签: bash count reverse uniq

我想要反转uniq -c输出:

  1 hi
  2 test
  3 try

到:

hi
test  
test  
try  
try  
try

我现在的解决方案是使用循环:

while read a b; do yes $b |head -n $a ;done <test.txt

我想知道是否有更简单的命令来实现它?

4 个答案:

答案 0 :(得分:3)

另一个awk

awk '{while ($1--) print $2}' file

答案 1 :(得分:2)

这是另一个解决方案

echo "\
1 hi
2 test
3 try" | awk '{for(i=1;i<=$1;i++){print($2)}}'

<强>输出

hi
test
test
try
try
try

这将以相同的方式工作,

awk '{for(i=1;i<=$1;i++){print($2)}}' uniq_counts.txt

脚本的核心,当然

 { for (i=1;i<=$1;i++) { print $2 }

其中awk已将输入解析为2个字段,$1为数字(1,2,3),$ 2为值,(hi,test,try)。

条件i<=$1测试计数器i未超过字段$ 1中提供的计数,print $2每次i<=$1条件为if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { //Show Information about why you need the permission AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Need Storage Permission"); builder.setMessage("This app needs storage permission."); builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION_CONSTANT); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.show(); } else if (permissionStatus.getBoolean(Manifest.permission.WRITE_EXTERNAL_STORAGE,false)) { //Previously Permission Request was cancelled with 'Dont Ask Again', // Redirect to Settings after showing Information about why you need the permission AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Need Storage Permission"); builder.setMessage("This app needs storage permission."); builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); sentToSettings = true; Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); Uri uri = Uri.fromParts("package", getPackageName(), null); intent.setData(uri); startActivityForResult(intent, REQUEST_PERMISSION_SETTING); Toast.makeText(getBaseContext(), "Go to Permissions to Grant Storage", Toast.LENGTH_LONG).show(); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.show(); } else { //just request the permission ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION_CONSTANT); } SharedPreferences.Editor editor = permissionStatus.edit(); editor.putBoolean(Manifest.permission.WRITE_EXTERNAL_STORAGE,true); editor.commit(); } else { //You already have the permission, just go ahead. proceedAfterPermission(); } 时打印值真。

IHTH

答案 2 :(得分:2)

您不需要awk或任何其他命令,您可以完全在bash

中完成
while read n s
do
    for ((i=0; i<n; i++))
    do
        echo $s;
    done
done < test.txt

答案 3 :(得分:2)

此处我的解决方案使用bash brace expansionprintf内部命令。

while read a b
do
     eval printf "'%.${#b}s\n'" "'$b'"{1..$a}
done <test.txt

以下简单示例

printf '%s\n' test{1..2}

打印两行,其中包含字符串test,后跟一个数字:

test1
test2

但我们可以使用printf命令的precision field指定要打印的确切字符数:

printf '%.4s\n' test{1..2}

显示:

test
test

要打印的字符长度由要打印的文本的长度(${#b})给出。

最后,必须在其他命令中使用eval命令来使用大括号扩展中的变量。