0

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?

0

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do it with bash:

a="\nFIRST_ITEM\nSECOND_ITEM\nTHIRD_ITEM\n"
a=( ${a//\\n/ } )

It replaces each \n with a space.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.