AWK blows me away
How did I not know this about awk
!? Don’t get me wrong, I’m no awk
expert; I’m always using some of the most simple and obvious features it has. But I almost always use the -F
option to specify the field separator. Until today, I thought you could only give it either a single character or a string literal. Go ahead, laugh, I’ll wait.
From StackOverflow:
1
2
3
4
|
$ echo '"School","College","City"'|awk -F'","|^"|"$' '{for(i=1;i< =NF;i++) {if($i)print $i}}'
School
College
City
|
I just used this to help parse the output of drush pml
. Since it uses more than one space to separate fields, I used
1 |
awk -F' +' '{print $2"\t"$4"\t"$5}'
|
(that’s two spaces and a plus sign for the field separator). I initially tried using
1 |
' {2,}' |
but apparently that’s some kind of super advanced regex that awk doesn’t understand.
Another cool thing about the StackOverflow page is that one person shows how you can parse a CSV with embedded / escaped commas if you have gawk
4 installed:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
% cat infile
"School",College: "My College","City, I"
% awk '{
for (i = 0; ++i < = NF;)
print i, substr($i, 1, 1) == "\042" ?
substr($i, 2, length($i) - 2) : $i
}' FPAT='([^,]+)|(\"[^\"]+\")' infile
1 School
2 College: "My College"
3 City, I
|