8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

给定一个文件 file.txt,转置它的内容。

你可以假设每行列数相同,并且每个字段由 ' ' 分隔。

示例:

假设file.txt有如下内容:

name age

alice 21

ryan 30

你的脚本应该显示第10行:

name alice ryan

age 21 30

有请Linux三剑客(awk,sed,grep)之一:

awk命令:

awk是一个报告生成器,它拥有强大的文本格式化的能力。它是由Alfred Aho 、Peter Weinberger 和 Brian Kernighan这三个人创造的,awk由这个三个人的姓氏的首个字母组成。

NAME

gawk - pattern scanning and processing language

SYNOPSIS

gawk [ POSIX or GNU style options ] -f program-file [ -- ]

file ...

gawk [ POSIX or GNU style options ] [ -- ] program-text

file ...

DESCRIPTION

Built-in Variables

Gawk's built-in variables are:

ARGC The number of command line arguments (does not

include options to gawk, or the program

source).

ARGIND The index in ARGV of the current file being

processed.

ARGV Array of command line arguments. The array is

indexed from 0 to ARGC - 1. Dynamically chang‐

ing the contents of ARGV can control the files

used for data.

BINMODE On non-POSIX systems, specifies use of “binary”

mode for all file I/O. Numeric values of 1, 2,

or 3, specify that input files, output files,

or all files, respectively, should use binary

I/O. String values of "r", or "w" specify that

input files, or output files, respectively,

should use binary I/O. String values of "rw"

or "wr" specify that all files should use

binary I/O. Any other string value is treated

as "rw", but generates a warning message.

CONVFMT The conversion format for numbers, "%.6g", by

default.

ENVIRON An array containing the values of the current

environment. The array is indexed by the envi‐

ronment variables, each element being the value

of that variable (e.g., ENVIRON["HOME"] might

be "/home/arnold"). Changing this array does

not affect the environment seen by programs

which gawk spawns via redirection or the sys‐

tem() function.

ERRNO If a system error occurs either doing a redi‐

rection for getline, during a read for getline,

or during a close(), then ERRNO will contain a

string describing the error. The value is sub‐

ject to translation in non-English locales.

FIELDWIDTHS A whitespace separated list of field widths.

When set, gawk parses the input into fields of

fixed width, instead of using the value of the

FS variable as the field separator. See

Fields, above.

FILENAME The name of the current input file. If no

files are specified on the command line, the

value of FILENAME is “-”. However, FILENAME is

undefined inside the BEGIN rule (unless set by

getline).

FNR The input record number in the current input

file.

FPAT A regular expression describing the contents of

the fields in a record. When set, gawk parses

the input into fields, where the fields match

the regular expression, instead of using the

value of the FS variable as the field separa‐

tor. See Fields, above.

FS The input field separator, a space by default.

See Fields, above.

FUNCTAB An array whose indices and corresponding values

are the names of all the user-defined or exten‐

sion functions in the program. NOTE: You may

not use the delete statement with the FUNCTAB

array.

IGNORECASE Controls the case-sensitivity of all regular

expression and string operations. If IGNORE‐

CASE has a non-zero value, then string compar‐

isons and pattern matching in rules, field

splitting with FS and FPAT, record separating

with RS, regular expression matching with ~ and

!~, and the gensub(), gsub(), index(), match(),

patsplit(), split(), and sub() built-in func‐

tions all ignore case when doing regular

expression operations. NOTE: Array subscript‐

ing is not affected. However, the asort() and

asorti() functions are affected.

Thus, if IGNORECASE is not equal to zero, /aB/

matches all of the strings "ab", "aB", "Ab",

and "AB". As with all AWK variables, the ini‐

tial value of IGNORECASE is zero, so all regu‐

lar expression and string operations are nor‐

mally case-sensitive.

LINT Provides dynamic control of the --lint option

from within an AWK program. When true, gawk

prints lint warnings. When false, it does not.

When assigned the string value "fatal", lint

warnings become fatal errors, exactly like

--lint=fatal. Any other true value just prints

warnings.

NF The number of fields in the current input

record.

NR The total number of input records seen so far.

OFMT The output format for numbers, "%.6g", by

default.

OFS The output field separator, a space by default.

ORS The output record separator, by default a new‐

line.

PREC The working precision of arbitrary precision

floating-point numbers, 53 by default.

awk的基本语法是awk [options] 'Pattern{Action}' file。

从一个最简单的命令作为start,省略[options]和Pattern,将Action设置成最简单的print:

$echoabc > test.txt

$awk '{print}' test.txt

abc

这就是最最简单的awk用法:使用awk命令对文本test.txt(的每一行)进行处理,处理的动作是print。

本题解答

awk '{

for (i=1;i<=NF;i++){

if (NR==1){ # 天坑!之前这里写成了NF,怎么输出每行开头都多1个空格

res[i]=$i

}

else{

res[i]=res[i]" "$i

}

}

}END{

for(j=1;j<=NF;j++){

print res[j]

}

}' file.txt

解析

awk是一行一行地处理文本文件,运行流程是:先运行BEGIN后的{Action},相当于表头

再运行{Action}中的文件处理主体命令

最后运行END后的{Action}中的命令

有几个经常用到的awk常量:NF是当前行的field字段数;NR是正在处理的当前行数。

注意到是转置,假如原始文本有m行n列(字段),那么转置后的文本应该有n行m列,即原始文本的每个字段都对应新文本的一行。我们可以用数组res来储存新文本,将新文本的每一行存为数组res的一个元素。

在END之前我们遍历file.txt的每一行,并做一个判断:在第一行时,每碰到一个字段就将其按顺序放在res数组中;从第二行开始起,每碰到一个字段就将其追加到对应元素的末尾(中间添加一个空格)。

文本处理完了,最后需要输出。在END后遍历数组,输出每一行。注意printf不会自动换行,而print会自动换行。

linux转置的命令,转置文件(awk)相关推荐

  1. linux ftp 查找文件,Linux shell ftp命令根据文件日期下载文件的方法

    需求:ftp获取远程数据的文件,根据文件的创建时间点下载文件. 可以自行扩展根据文件的大小等其他需求. 知识点总结: 1.获取文件的时间: ls -lrt|awk '{print $6" & ...

  2. Linux miny选择字体,linux下sed命令对文件执行文本替换

    让我们看一下 sed 最有用的命令之一,替换命令.使用该命令,可以将特定字符串或匹配的规则表达式用另一个字符串替换.下面是该命令最基本用法的示例: $ sed -e 's/foo/bar/' myfi ...

  3. linux cp复制文件夹下的软连接,Linux培训:cp命令复制文件和目录

    Linux培训:cp命令复制文件和目录 更新时间:2019年04月02日16时56分 来源:传智播客linux培训 浏览次数: cp 是用于复制的命令,其基本信息如下: 命令名称:cp: 英文原意:c ...

  4. Linux命令gitview,使用linux的gitview命令查看文件内容

    使用linux的gitview命令查看文件内容 发布时间:2020-07-22 10:23:42 来源:亿速云 阅读:99 作者:清晨 栏目:服务器 这篇文章将为大家详细讲解有关使用linux的git ...

  5. linux sed替换文件,linux的sed命令替换文件

    linux下的sed是一个强大的编辑器工具,下面由学习啦小编为大家整理了linux的sed命令替换文件的相关知识,希望对大家有帮助! linux的sed命令替换文件 sed在Linux下是个强大的工具 ...

  6. Linux:grep命令检索文件内容详解

    前言 Linux系统中搜索.查找文件中的内容,一般最常用的是grep命令,另外还有egrep命令,同时vi命令也支持文件内容检索.下面来一起看看Linux利用grep命令检索文件内容的详细介绍. 方法 ...

  7. touch服务器端文件夹,使用linux的touch命令创建文件

    使用linux的touch命令创建文件 发布时间:2020-07-22 11:33:24 来源:亿速云 阅读:99 作者:清晨 栏目:服务器 小编给大家分享一下使用linux的touch命令创建文件, ...

  8. linux删除文件不需要确定,linux的rm命令-删除文件或目录

    linux的rm命令-删除文件或目录 英文原意:remove 命令所在路径:/bin/rm 语法:rm -r 文件或目录,为目录时要加-r,且删除需要用户确认,如果不需要用户确认则 语法为:rm -r ...

  9. linux如何改文件内容,linux下用命令修改文件内容

    linux下vi命令修改文件及保存的使用方法 进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi n filename :打开文件,并将光标置于第n行首 vi fil ...

  10. linux vim无法编辑文件内容,Linux 使用vim命令编辑文件内容

    在终端可以使用vim命令来直接编辑文件内容. vim,也可以叫做vi. vim有三种模式:命令模式.输入模式.底线命令模式. 命令模式 vim  文件名   进入命令模式,vim也可以写成vi. 如果 ...

最新文章

  1. spring 使用@Async注解实现异步调用
  2. Creating a Jabber Client using the agsXMPP Library
  3. HDFS【2.5.1】系列1:HDFS的核心数据结构---元数据
  4. SAP Hybris Enterprise Commerce Platform ECP架构综述
  5. python与sqlite3_sqlite3与python2.5,pysqlite和apsw有什么区别
  6. hdu 2149 巴什博弈
  7. mybatis-plus的 mapper.xml 路径配置的坑
  8. CentOs基础操作指令(文件所属管理和权限管理)
  9. pytorch 中的数据类型,tensor的创建
  10. mysql存储日期 jsp_JSP+MySql的时间处理
  11. 前景检测算法(十七)--基于光流算法
  12. 一个小白对卷积神经网络的理解
  13. 关于ftp 服务器搭建的200错误与527错误
  14. 青出于蓝胜于蓝 dfs+树状数组
  15. 中科大网上财务报销填写流程
  16. 在linux上运行python脚本(安装pytorch踩坑记录,pyinstaller使用方式,构建docker镜像)
  17. 手写签名更改为透明背景png图片
  18. Go Http 解析 text/plain
  19. 全球数字高程数据:ASTER GDEM
  20. 用户留存率分析 表设计

热门文章

  1. 旅行青蛙南の旅旅行券_旅行时查找WiFi
  2. 推荐一款制作H5页面的可视化工具
  3. 《SysML精粹》学习记录--第八章
  4. Logic Synthesis And Verification Algorithms Gary D. Hachtel Fabio Somenzi 第十章
  5. android穿山甲主题冲突,Flutter 接头条穿山甲广告 Android 总述篇
  6. 2020年9月六级翻译题目:西游记
  7. 树莓派c语言百度语音识别,树莓派语音识别
  8. 初次使用Pikachu漏洞平台进行测试实验
  9. 《数据安全法》实施一周年,企业和个人发生哪些转变?|上云那些事
  10. 浅谈JdbcDaoSupport