Skip to content

Instantly share code, notes, and snippets.

@chenzx
Created March 6, 2013 04:51
Show Gist options
  • Save chenzx/5096815 to your computer and use it in GitHub Desktop.
Save chenzx/5096815 to your computer and use it in GitHub Desktop.
Cut out specific fields of each line, and re-puts out. Similar to awk's function
FIELD_SPLITTER = ' ' #change to '\t', ',', ':' as you need;
FIELD_CUT_INDEXS = [0] #Start from 0;
File.open(ARGV[0]).each { |line|
fields = line.strip.split(FIELD_SPLITTER)
cut_fields = []
FIELD_CUT_INDEXS.each{ |index|
field = fields[index]
cut_fields.push( field )
}
puts cut_fields.join(FIELD_SPLITTER)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment