相关疑难解决方法(0)

如何在Bash中对齐空格分隔表的列?

我有一个文件,其中包含用空格分隔的任意数量的非对齐列.

我想对齐文件的列.

我看过col它看起来不合适的命令.

我可以写一个awk脚本,但似乎应该存在一个更明显的命令.

linux bash

27
推荐指数
1
解决办法
1万
查看次数

使用awk对齐文本文件中的列?

awk是"输入"转换为"所需的输出"有用吗?

输入

testing speed of encryption
test 0 (64 bit key, 16 byte blocks): 2250265 operations in 1 seconds (36004240 bytes)
test 1 (128 bit key, 64 byte blocks): 879149 operations in 1 seconds (56265536 bytes)
test 2 (128 bit key, 256 byte blocks): 258978 operations in 1 seconds (66298368 bytes)
test 3 (128 bit key, 1024 byte blocks): 68218 operations in 1 seconds (69855232 bytes)
test 4 (128 bit key, 8192 byte blocks): 8614 operations in 1 …
Run Code Online (Sandbox Code Playgroud)

linux bash awk

21
推荐指数
3
解决办法
2万
查看次数

AWK:两次完成文件,执行不同的任务

我正在处理一个相当大的推文集合,我想为每条推文获取其提及(其他用户的名字,前缀为一个@),如果提到的用户也在文件中:

users = new Dictionary()
for each line in file:
   username = get_username(line)
   userid   = get_userid(line)
   users.add(key = userid, value = username)
for each line in file:
   mentioned_names = get_mentioned_names(line)
   mentioned_ids = mentioned_names.map(x => if x in users: users[x] else null)
   print "$line | $mentioned_ids"
Run Code Online (Sandbox Code Playgroud)

我已经使用GAWK处理该文件,因此不再在Python或CI中再次处理它,而是决定尝试将其添加到我的AWK脚本中.但是,我无法找到一种方法来传递相同的文件,为每个文件执行不同的代码.大多数解决方案都意味着多次调用AWK,但后来我放弃了我在第一遍中创建的关联数组.

我可以用非常黑客的方式做到这一点(比如cat'将文件sed复制两次,将其传递给每个文件中的所有行添加不同的前缀cat),但我希望能在几个月内理解这些代码讨厌自己

AWK的做法是什么?

PD:

我找到的不那么可怕的方式:

function rewind(    i)
{
    # from https://www.gnu.org/software/gawk/manual/html_node/Rewind-Function.html
    # shift remaining arguments up
    for (i = ARGC; i > ARGIND; i--)
        ARGV[i] = …
Run Code Online (Sandbox Code Playgroud)

awk gawk

9
推荐指数
1
解决办法
2961
查看次数

标签 统计

awk ×2

bash ×2

linux ×2

gawk ×1