Reputation: 5
I have a string of the form:
"\nFIRST_ITEM\nSECOND_ITEM\nTHIRD_ITEM\n"
When I try to use awk to split it into an array like so,
echo "\nFIRST_ITEM\nSECOND_ITEM\nTHIRD_ITEM\n" | awk '{split($0,a,"\n")}'
The whole string just gets stored as is into a[1]. Could someone please explain why this is happening and how to fix it?
Upvotes: 0
Views: 672
Reputation: 203149
It's not clear from your question but this MAY be what you're looking for:
$ echo "\nFIRST_ITEM\nSECOND_ITEM\nTHIRD_ITEM\n" |
awk '{split($0,a,/\\n/); for (i=1;i in a;i++) print i, "<" a[i] ">"}'
1 <>
2 <FIRST_ITEM>
3 <SECOND_ITEM>
4 <THIRD_ITEM>
5 <>
assuming your echo outputs \n
as the string \n
and not a newline character.
Upvotes: 1
Reputation: 8521
You can do it with bash
:
a="\nFIRST_ITEM\nSECOND_ITEM\nTHIRD_ITEM\n"
a=( ${a//\\n/ } )
It replaces each \n
with a space.
Upvotes: 0