0

I'm trying the following based on code samples I found:

echo "file.txt" | awk '{split($0,a,"."); print a[0]}'

But it's returning a blank output.

Any hint?

Kleber S.
  • 8,110
  • 6
  • 43
  • 69

2 Answers2

6

You code is correct however the array a starts from 1 with the split function:

$ echo "file.txt" | awk '{split($0,a,"."); print a[0]}'

$ echo "file.txt" | awk '{split($0,a,"."); print a[1]}'
file

$ echo "file.txt" | awk '{split($0,a,"."); print a[2]}'
txt

From the gawk manual:

split(string, array [, fieldsep [, seps ] ])

Divide string into pieces separated by fieldsep and store the pieces in array and the separator strings in the seps array. The first piece is stored in array[1], the second piece in array[2], and so forth.

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
  • you're impossible to beat! You must be hitting refresh with a frequency of 2Hz or something :-) – Fredrik Pihl Mar 19 '13 at 15:01
  • Thank you! didn't know about the index starting from 1. @FredrikPihl lol :P – Kleber S. Mar 19 '13 at 15:02
  • No problem, it's one of those mistake we all make. @FredrikPihl I have the fastest gun in the west! – Chris Seymour Mar 19 '13 at 15:04
  • @FredrikPihl writing a small script to check certain tags is not hard thing. when you are in break, take a look that list generated by the script, you will be the fastest! well, `F5` is another way to do, I don't believe that sudo_O keeps pressing `F5` though.. ^_^ – Kent Mar 19 '13 at 15:07
  • I just have `awk` set as a favorite tag so the question show up in yellow/gold *(no F5, no script needed)*. `awk` isn't so popular. Try answering question in `python`, `C#`, ``... – Chris Seymour Mar 19 '13 at 15:14
  • @kent - I'm just saying that between you, sudo_O, sputnick and some more, it's very hard to post something; you only have seconds before any of you post something. 4 upvotes for this answer is cheap :-) – Fredrik Pihl Mar 19 '13 at 15:33
  • @FredrikPihl yes competition is good though! Voting is completely arbitrary, I don't thing 4 is cheap, [see this](http://stackoverflow.com/questions/2096490/print-second-last-column-field-in-awk). – Chris Seymour Mar 19 '13 at 15:37
  • @sudo_O thx for the link. he used `print` as a function. `print(...)` that's why he got 44 up. you only got 4 (so far) because you didn't write `print (a[0])` :D – Kent Mar 19 '13 at 15:43
  • FWIW I'd take a point off if he DID use `print(a[0])`* :-). *print is not a function it's a buitin so using `print(foo)` is not calling a function `print()` with argument `foo`, it's calling a builtin `print` with argument `(foo)`. – Ed Morton Mar 19 '13 at 19:27
  • I'm actually surprised that the posted solution worked since the 3rd argument to split() is an RE, not a string, and "." is the RE metacharacter for "any character" so I'd have expected the string to get split into an array of N null strings where N is the number of characters in the original string plus 1. Have to think about that for a bit to figure out why it worked as it obviously does. I'd have thought you'd need /[.]/ or similar. – Ed Morton Mar 19 '13 at 19:30
  • @sudo_O Whether it's a regexp constant `/foo/` or a dynamic regexp `"foo"` or `x="foo"; ... ~ x`, it's still a regexp, only escape sequences should change between the 2 forms. I don't know why "." is working in this example. – Ed Morton Mar 19 '13 at 22:59
  • Clearer definition further down `"Modern implementations of awk, including gawk, allow the third argument to be a regexp constant (/abc/) as well as a string. (d.c.) The POSIX standard allows this as well"` – Chris Seymour Mar 19 '13 at 23:00
  • @sudo_O - again that doesn't matter, they're both regexps. I just figured it out though - it's because split() does field-splitting and there's a feature of field splitting that treats single characters uniquely in that it ignores their RE metacharacter functionality. `"FS == any other single character Fields are separated by each occurrence of the character. Multiple successive occurrences delimit empty fields, as do leading and trailing occurrences. The character can even be a regexp metacharacter; it does not need to be escaped.` – Ed Morton Mar 19 '13 at 23:03
  • @sudo_O Me too and I have a sneaking suspicion there's now things I'm learning then forgetting and then having to learn all over again! – Ed Morton Mar 20 '13 at 03:07
  • @EdMorton I submitted a bug email to bug-gawk regarding the split() last evening. you should have already seen it in that mailing list. – Kent Mar 20 '13 at 09:48
  • @Kent It's not a bug, it's a documented feature of field-splitting. split($0,a,".") behaves the same way that FS="." does. Now I wonder how to use a single char as it's RE metacharacter for FS though - I mean how do I get field-splitting with `FS=.` like I would with `split($0,a,/./)` rather than `split($0,a,".")`? I actually don't think it's possible. BTW I'm not on that mailing list. – Ed Morton Mar 20 '13 at 11:45
5

try

echo "file.txt" | awk '{split($0,a,"."); print a[1]}'  

awk array index is 1-based.

Kent
  • 189,393
  • 32
  • 233
  • 301