`

One line Ruby code

    博客分类:
  • Ruby
阅读更多
经常使用perl来代替一行的sed awk,主要是想使用perl更强大的正则:
echo "Hello 123 world" | perl -ne 's/\s+\d+\s+/,/g;print'

ruby也提供了和perl一样强大的一行代码功能:
引用

$ruby --help
  -a              autosplit mode with -n or -p (splits $_ into $F)
  -l              enable line ending processing
  -n              assume 'while gets(); ... end' loop around your script
  -p              assume loop like -n but print line also like sed
  -l              automatic call to .chomp on input

上面几个选项经常在一行代码中使用:
echo "Hello 123 world" | ruby -ne 'puts $_.gsub(/\s+\d+\s+/,",")'

#print with line number
ruby -ne 'print($., ": ", $_)' < /etc/irbrc

# Print each line reversed:
ruby -lne 'puts $_.reverse' < /etc/irbrc

# Print the second column from an input
ruby -ane 'puts $F[1]' < /etc/irbrc

# Print lines that contain "eat"
ruby -ne 'puts $_ if /eat/i' < /etc/irbrc

# Same as above:
ruby -pe 'next unless /eat/i' < /etc/irbrc

# Pass-through (like cat, but with possible line-end munging):
ruby -pe '' < /etc/irbrc

# Uppercase all input:
ruby -pe '$_.upcase!' < /etc/irbrc

# Same as above, but actually write to the input file, and make a backup first with extension .bak - Notice that inplace edit REQUIRES input files, not an input STDIN:

ruby -i.bak -p -e '$_.upcase!' /etc/irbrc
分享到:
评论

相关推荐

    单线串口(One_line)模式应用手册3

    《XY-V17B 单线串口(One_line)模式应用手册3》是一份详细指导如何使用XY-V17B芯片进行单线串口通信的文档,适用于嵌入式系统设计者和MCU开发者。该手册涵盖了硬件连接、通信协议、协议时序、协议约定以及通信指令...

    One-line总线协议&DS18B20使用简介.docx

    单总线协议是一种独特的通信协议,它允许主机与多个设备在一个单一的信号线上进行双向通信。这种协议在微控制器和传感器等嵌入式系统中广泛应用,例如与DS18B20数字温度传感器的交互。DS18B20是一款著名的单总线设备...

    ruby经典

    Ruby是一种面向对象的、动态类型的编程语言,以其简洁、优雅的语法著称,深受开发者喜爱。"Ruby经典"这本书作为入门级别的读物,旨在帮助初学者快速理解和掌握Ruby语言的基本概念和核心特性。 1. **面向对象编程**...

    codeline(代码行计算工具)

    **codeline:代码行计算工具** 在软件开发过程中,度量代码行数(LOC,Lines of Code)是一项常见的质量评估指标。它可以帮助开发者理解项目的规模、估算工作量以及跟踪项目进度。`codeline`是一款命令行工具,专门...

    CodeLine

    《CodeLine:一款基于C#的代码行数统计与分析工具》 CodeLine是一款小巧而功能强大的代码统计工具,专为开发者设计,用于快速、准确地统计项目中的代码行数。该工具支持自定义文件过滤器,允许用户根据不同的编程...

    Code Line Counter(计算代码行数工具) v1.7 破解版

    Code Line Counter是一款计算代码行数的小工具。支持C, C++, C#, Java, Delphi/Pascal, COBOL, VB, PHP, ASP, XML, Perl, Fortran, SQL Script等语言。

    OneLine-crx插件

    OneLine突出显示一行以聚焦您的阅读。 您是否曾经在网上阅读时迷失了语言? OneLine是网络的阅读辅助工具,通过突出显示一行单词来提高您的注意力和阅读效率。 OneLine可以在任何网站上使用。 如果您在阅读时遇到...

    One Line Puzzle一行拼图unity益智游戏项目源码

    One Line Puzzle一行拼图 UNITY2018.1.3f1及以上 通过我们最新的益智游戏 - One-Line Connect 体验终极大脑训练。通过这款连接方块风格的游戏,磨练你的思维并挑战自己,你必须仅用一根线填满所有方块。 拥有 300 个...

    Build.Awesome.Command-Line.Applications.in.Ruby

    Build.Awesome.Command-Line.Applications.in.Ruby

    Build Awesome Command-Line Applications in Ruby 2

    With this book, you’ll learn specific ways to write command-line applications that are easy to use, deploy, and maintain, using a set of clear best practices and the Ruby programming language....

    OneLine:OneLine是抽象的标准化重新定义框架

    OneLine(开发版,即将推出测试版) OneLine是抽象的标准化重新定义框架。一般技术特点 高度抽象 泛型 标准化的 多事的 可听的 链式 可覆写 可回收 脚手架工具(即将上市) RAD(快速应用程序开发)和原型设计 更多...

    Build Awesome Command-Line Applications in Ruby

    #### 标签解读:“ruby command line” - **Ruby**:一种动态的面向对象编程语言,以其简洁优雅的语法著称。 - **Command Line**:指的是用户通过文本命令而非图形界面与计算机进行交互的方式。 #### 部分内容解析...

    codeline

    可以统计delphi或C#程序的代码行数

    Ruby Under a Microscope

    just a matter of typing ruby at the command line and pressing enter, and your Ruby script is running. However, Ruby’s syntax is deceptively simple; in fact, Ruby employs sophisticated ideas from ...

    编码约定(code guide line)

    ### 编码约定(Code Guide Line):Java Sun OpenSSO 编码规范 #### 概述 本篇文章介绍了一套针对 Java Sun OpenSSO 项目的编码规范,旨在提高代码的可读性、简化代码维护过程并减少缺陷。该文档由 Sun Microsystems...

    oneline-server:Oneline框架

    【标题】"oneline-server:Oneline框架"指的是一个基于Java开发的在线服务框架,它为开发者提供了一种便捷的方式来构建和运行Web应用程序。Oneline框架可能是为了简化开发流程、提高开发效率而设计的,其核心特性可能...

    代码行计数工具 source code line counter

    This little tool can sum all code lines of specified file extension name in some directory, and it can subtract the line count of the specified source files. [中文] 这也是我写的一个工具。 它可以计数...

    UVa Online Judge 10944 Accepted Code

    UVa Online Judge 10944 Accepted Code

    VC_read_file_line_code.rar_VC readli_the code_vc read file_读取文件

    "VC_read_file_line_code.rar"这个压缩包提供的经典代码着重于如何在VC++中实现逐行读取文件的功能。这是一个基础但非常实用的技巧,尤其在处理大量文本数据时。下面将详细介绍如何在VC++中实现这一功能。 首先,...

Global site tag (gtag.js) - Google Analytics