Ruby one-liners (file manipulation)

Posted by root Fri, 20 Jul 2007 17:11:00 GMT


# number each line of a file
ruby -ne 'puts "#{$.}\t#{$_}"' file.txt

# print all non-blank lines
ruby -pe '$_.chomp.empty? and next' file.txt

# number and print all non-blank lines
ruby -ne '$_.chomp.empty? or print $.,"\t", $_' file.txt

# number and print each blank line
ruby -ne 'puts $. if $_.chomp.empty?' file.txt

# reverse order of lines (`tac` style)
ruby -e 'puts File.open($<.filename).readlines.reverse' file.txt

# print matched string from lines, matching the pattern
ruby -ne 'puts $_.scan(/^\w+/)' /etc/passwd

# triple space a file and reverse order of lines
ruby -e '$,="\n\n\n"; puts File.readlines($<.filename).reverse.join' file.txt

# print first line of a file (emulate 'head -1')
ruby -ne 'puts $_; break' file.txt
ruby -pe '$. == 1 or break' file.txt

# print last line of a file (emulates 'tail -1')
ruby -ne 'END{puts $_}' file.txt

# print last line number (emulates 'wc -l')
ruby -e 'loop{gets or break}; puts $.' file.txt

# print only lines that match a regular expression (emulates 'grep')
ruby -pe 'next if not /regex/' file.txt

# print only lines that do not match a regular expression (emulates 'grep -v')
ruby -pe 'next if /regex/' file.txt

# print section of file between two regular expressions, /^root/ and /^nobody/
ruby -ne 'puts $_ if /^root/../^nobody/' file.txt

# print file and remove duplicate, consecutive lines from a file (emulates 'uniq')
ruby -ne '$_.eql? $; or puts $_;$; = $_;' file.txt

# print file except for blank lines
ruby -pe 'next if $_.chomp.empty?' file.txt
ruby -pe 'next if /^\s*$/' file.txt
ruby -pe 'next if $_.split(/\S+/).size < 2' file.txt

# print file except for lines, starting with digit (unclear and inefficient)
ruby -pe 'next if (48..57).to_a.include?($_.split(//)[0][0])' file.txt

# delete all leading blank lines at top of file
ruby -pe '$,="$." if not $_.chomp.empty?; $, or next' file.txt

# print section of file from regex to end of file
ruby -pe '$,="$." if /regex/; $, or next' file.txt

# delete leading and trailing whitespace from each line
ruby -pe '$_.strip!.sub!(/$/, "\n")' file.txt
ruby -ne 'puts $_.strip! + $/' file.txt

# delete leading whitespace from the beginning of each line
ruby -ne 'puts $_.lstrip! || $_' file.txt

# convert DOS newlines (CR/LF) to Unix format (LF)
ruby -i -pe 'sub(/\r\n/, "\n")' file.txt

Posted in ,  | Tags ,  | 4 comments

Comments

  1. Peter Bailey said about 1 month later:

    I get the following error when I try to convert a Windows font file to the same, with Unix line endings.

    ruby -i -pe ‘gsub!(/\r\n/, ”\n”)’ nbr_.afm

    -e:1: Can’t do inplace edit without backup (fatal)

  2. Vidul said about 1 month later:
    Saving the original file will solve the problem:
    ruby -i.org -pe ‘gsub!(/\r\n/, ”\n”)’ nbr_.afm
    
    where “org” will serve as extention to the original file, which will stay untouched.
  3. Eric B. said 3 months later:

    Stolen from http://fepus.net/ruby1line.txt

  4. Vidul said 3 months later:

    Not really stolen – read the code more carefully – each of my one-liners differs from the older ones.

Comments are disabled