CLI Magic: For geek cred, try these one-liners

本文介绍了一系列实用的Linux命令行技巧,包括文件操作、进程管理、用户列表整理及邮件报告生成等,通过巧妙组合如grep、awk、sort等工具,提升系统管理效率。

CLI Magic: For geek cred, try these one-liners

By Sergio Gonzalez Duran on July 23, 2008 (9:00:00 AM)

Share    Print    Comments   

In this context, a one-liner is a set of commands normally joined through a pipe (|). When joined by a pipe, the command on the left passes its output to the command on the right. Simple or complex, you can get useful results from a single line at the bash command prompt.

For example, suppose you want to know how many files are in the current directory. You can run:

  
ls | wc -l

That's a very simple example -- you can get more elaborate. Suppose you want to know about the five processes that are consuming the most CPU time on your system:

  
ps -eo user,pcpu,pid,cmd | sort -r -k2 | head -6

The ps command's o lets you specify the columns that you want to be shown. sort -r does a reverse order sort with the second column (pcpu) as reference (k2). head gets only the first six lines from the ordered list, which includes the header line. You can place pcpu as the first column and then omit the k2 option because sort by default takes the first column to do the sort. That illustrates how you may have to try several approaches on some one-liners; different versions and ways to manipulate the options may produce different results.

A common situation for Linux administrators on servers with several users is to get quick ordered user lists. One simple way to get that is with the command:

  
cat /etc/passwd | sort

If you just need the username, the above command returns too much information. You can fix it with something like this:

  
cat /etc/passwd | sort | cut -d":" -f1

The sorted list is passed to cut, where the d option indicates the field's delimiter character. cut breaks into pieces each line, and the first field f1 is the one that you need to display. That's better; it shows only usernames now. But you may not want to see all the system usernames, like apache, bin, and lp. If you just want human users, try this:

  
cat /etc/passwd | sort | gawk '$3 >= 500 {print $1 }' FS=":"

gawk evaluates each line from the output piped to it. If the third field -- the UID -- is equal or greater than 500 (most modern distros start numbering normal users from this number) then the action is done. The action, indicated between braces, is to print the first field, which is the username. The separator for field in the gawk command is a colon, as specified by the FS option.

Now suppose you have a directory with lots of files with different extensions, and you want to back up only the .php files, calling them filename.bkp. The next one-liner should do the job:

  
for f in *.php; do cp $f $f.bkp; done

This command loops through all the files in the current directory looking for those with .php extensions. Each file's name is held in the $f variable. A simple copy command then does the backup. Notice that in this example we used a semicolon to execute the commands one after another, rather than piping output between them.

What about bulk copy? Consider this:

  
tar cf - . | (cd /usr/backups/; tar xfp -)

It creates a tar package recursevely on the current directory, then pipes this package to the next command. The parenthesis creates a temporary subshell, changes to a different directory, then extracts the content of the package, which is the whole original directory. The p option on the last tar command preserves file properties like time and permissions. After completion, the shell context will be at the original directory.

A variant on the previous one-liner lets you do the same kind of backup on a remote server:

  
tar cf - . | ssh smith@remote.server tar xfp - -C /usr/backup/smith

Here, the command establishes an SSH remote session and untars the package with the C option, which changes the directory, in this case to /usr/backup/smith, where the extraction will be made.

grep and gawk and uniq, oh my!

Text processing is a common use for one-liners. You can accomplish marvelous things with the right set of commands. In the next example, suppose you want a report on incoming email messages that look like this:

  

cat incoming_emails
2008-07-01 08:23:17 user1@example.com
2008-07-01 08:25:20 user2@someplace.com
2008-07-01 08:32:41 somebody@server.net
2008-07-01 08:35:03 spam not recived, filtered
2008-07-01 08:39:57 user1@example.com
...

You are asked for a report with an ordered list of who received incoming messages. Many recipients would be repeated in the output of the cat command. This one-liner resolves the problem:

  
grep '@' incoming_email | gawk '{print $3}' | sort | uniq

grep filters the lines that contains a @ character, which indicates an email address. Next, gawk extracts the third field, which contains the email address, and passes it to the sort command. Sorting is needed to group the same recipients together because the last command, uniq, omits repeated lines from the sorted list. The output is shown below. Most text processing one-liners use a combination of grep, sed, awk, order, tr, cut, uniq, and other related commands.

  

somebody@server.net
user1@example.com
user2@someplace.com

If you like any of these one-liners but think they're too long to type often, you can create an alias for the command and put it in your .bashrc file. When you log in your session, anything inside this file will be run, so your personal aliases would be ready at anytime.

  
alias p5="ps -eo pcpu,user,pid,cmd | sort -r | head -6"

You can certainly create better and simpler variations of all of the commands in this article, but they're a good place to start. If you are a Linux system administrator, it's good practice to collect, create, and modify your own one-liners and keep them handy; you never know when are you going to need them. If you have a good one-liner, feel free to share it with other readers in a comment below.

Sergio Gonzalez Duran is a Linux administrator, systems developer, and network security counselor who also teaches Linux courses and publishes the Spanish-oriented Linux and open source Web site linuxtotal.com.mx.

阅读(252) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
内容概要:本文档围绕“经济学期刊论文复现:数字化转型能否促进企业的高质量发展”这一核心命题,系统整合了MATLAB与Python编程实现的大量科研案例,聚焦于数字化转型对企业全要素生产率(TFP)及高质量发展影响的实证研究。文档不仅复现了高水平经济学期刊论文中的计量经济模型,如基于中国上市公司数据的数字化转型与生产率关系分析,还深度融合了工程领域的建模技术,涵盖微电网优化、负荷预测、风电光伏不确定性建模、电力系统故障仿真等。同时,提供了智能优化算法(如遗传算法、粒子群优化)、机器学习(LSTM、CNN-BiGRU-Attention)、信号处理、路径规划等多学科交叉的技术资源,构建了一个从理论推导到代码实现的完整科研支持体系,旨在帮助研究者系统掌握论文复现与实证分析的核心方法。; 适合人群:具备一定MATLAB或Python编程基础,从事经济学、管理学、能源系统、智能制造及相关交叉学科研究的研究生、科研人员及高校教师。; 使用场景及目标:①复现经济学顶刊中关于数字化转型与企业高质量发展的实证模型;②学习如何量化数字化转型并构建其对企业绩效的影响评估框架;③掌握基于真实数据的计量经济建模、场景生成与优化调度仿真技术,全面提升科研论文写作与实证研究能力。; 阅读建议:建议读者结合文中提供的代码与数据资源,重点研读“论文复现”与“创新未发表”模块,按照技术路径循序渐进地实现模型复现与拓展。推荐关注“荔枝科研社”公众号及百度网盘链接获取完整资料,系统性地开展学习与科研实践。
下载代码方式:https://pan.quark.cn/s/9de6a9d0b3d8 依据所提供的文件内容,能够推导出此段程序的核心任务在于对一个任意的三位数进行拆解,并且分别呈现该数值的百位、十位及个位部分。随后,我们将对该知识点进行进一步的深入研究。 ### 一、程序功能说明 #### 1. 接收任意一个三位数输入 程序起始阶段运用`scanf`函数来获取用户输入的一个整数。为确保输入内容确实为一个三位数,在实际应用场景中通常需要嵌入验证机制来保障输入的有效性。然而,在本示例情形下,该环节被简化处理,预设用户总会准确输入一个三位数。 #### 2. 实施数字的拆分并提取各位置数值 程序借助一系列数学计算来对三位数进行拆分,将其转化为百位、十位和个位三个独立的构成部分。具体而言,通过除法和取模运算完成了这一过程。 #### 3. 展示各位置上的数值 程序运用`printf`函数来输出原始数值以及各个位上的数值。需要留意的是,代码中的输出部分似乎存在一些混淆,存在语法上的错误,例如多余的`printf`语句和乱码字符等问题。 ### 二、核心代码分析 #### 1. 数字拆分逻辑 ```c a[0] = n / 1000; // 提取千位数,但鉴于题目要求是三位数,此处应为百位数 a[1] = n % 1000 / 100; // 提取百位数 a[2] = n % 1000 % 100 / 10; // 提取十位数 a[3] = n % 1000 % 100 % 10; // 提取个位数 ``` 这段代码通过一连串的除法和取模运算,成功地将输入的数字n拆分为百位、十位和个位三个独立的构成部分,...
内容概要:本文提出了一种基于CNN-BiGRU-Attention混合神经网络模型的风电功率预测方法,采用多变量输入实现单步预测,并通过Matlab进行代码实现与验证。该模型融合卷积神经网络(CNN)以提取输入数据的局部时空特征,利用双向门控循环单元(BiGRU)充分捕捉风速、温度、湿度等多源气象与运行变量的时间序列前后依赖关系,并引入注意力机制(Attention)动态加权关键时间步的特征信息,有效提升模型对风电功率波动性和不确定性的建模能力,显著增强了预测的准确性与鲁棒性。; 适合人群:具备一定机器学习与深度学习理论基础,熟悉Matlab编程环境,从事新能源发电预测、电力系统调度、智能电网优化等相关领域的科研人员、工程技术人员及高校研究生。; 使用场景及目标:①应用于实际风电场功率预测系统,为电网调度、电力市场交易与可再生能源消纳提供高精度数据支撑;②作为深度学习在能源时序预测领域的典型案例,用于科研项目开发、学术论文复现与技术创新;③深入理解多变量时间序列预测中特征融合、序列建模与注意力权重分配的协同机制,掌握先进神经网络架构的设计与优化方法。; 阅读建议:建议结合提供的Matlab代码进行实践操作,重点剖析数据预处理流程、模型网络结构搭建、训练参数调优及注意力权重可视化等关键环节,鼓励尝试替换不同特征输入、调整网络深度或引入其他优化算法(如贝叶斯优化、粒子群优化等)以进一步提升模型性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值