awk or sed help for munging first line of a CSV or TSV file
awk, bash, sed
Solution
Code for GNU sed
sed -r '1 {s/.*/\L&/;s/\b\s\b/_/g}' infile>outfile
$ echo Cancer Type, Assembly Version, Chromosome, Chromosome start, Chromosome end|sed -r '1 {s/.*/\L&/;s/\b\s\b/_/g}'
cancer_type, assembly_version, chromosome, chromosome_start, chromosome_end
Problem
I was messing around with awk because I think it's far simpler to munge the header of a tab delimited or csv file with this tool.. I have two types of files (either comma, or tab delimited) and all I would like to do is to modify the header (NR =1) to: - lowers the case of all the words - replace any spaces with underscores for each field name.. Ex. changing Cancer Type bellow to *cancer_type* Cancer Type, Assembly Version, Chromosome, Chromosome start, Chromosome end All I've managed to do so far is to list the first line awk 'NR == 1' test2.csv Well I'm at a loss. In any case I'll probably run this script (sed or awk) prior to doing some downstream modifications. Any help (or pointing me to a good tutorial/one liners) would be much appreciated. EDIT Hi I should edit to clarify this. I will be taking starting with a file, and ending with the same file but with the header changed. I could get two versions of the file. The CSV Cancer Type, Assembly Version, Chromosome, Chromosome start, Chromosome end After: cancer_type, assembly_version, chromosome, chromosome_start, chromosome_end The TSV Cancer Type\t Assembly Version\t Chromosome\t Chromosome start\t Chromosome end After: cancer_type\t assembly_version\t chromosome\t chromosome_start\t chromosome_end Having said that I think approaches are almost working.. EDIT 2 The os is OS X 10.7.+