0

I have this (demo) text in the variable ArtTEXT.

1|Reporting Problems and Bugs. 
2|Other freely available awk implementations. 
5|Summary of installation. 
8|How to disable certain gawk extensions. 
3|Making Additions To gawk. 
7|Accessing the Git repository. 

It is a one variable where the lines are delimited with a known string.

I want to split it into an array where the number at the beginning of the line will be the index of the array line using one command, without looping through the lines.

The result should be:

arr[1] => Reporting Problems and Bugs. 
arr[2] => Other freely available awk implementations. 
arr[5] => Summary of installation. 
arr[8] => How to disable certain gawk extensions. 
arr[3] => Making Additions To gawk. 
arr[7] => Accessing the Git repository. 

Is this possible?

2
  • possible duplicate of split string to array using awk Commented Sep 6, 2015 at 9:11
  • 1
    Don't you think it'd make it easier for us to help you if you told us what this "known string" that delimits the lines is? Commented Sep 6, 2015 at 15:05

3 Answers 3

2

No, it's not possible. Here's how to populate an array from the variable as you want (assuming the "known string" that separates lines is a newline):

$ awk -v ArtTEXT='1|Reporting Problems and Bugs.
2|Other freely available awk implementations.
5|Summary of installation.
8|How to disable certain gawk extensions.
3|Making Additions To gawk.
7|Accessing the Git repository.' '
BEGIN {
    split(ArtTEXT,lines,/\n/)
    for (lineNr in lines) {
        split(lines[lineNr],flds,/\|/)
        arr[flds[1]] = flds[2]
    }

    for (i in arr) {
        printf "arr[%d] => %s\n", i, arr[i]
    }
}
'
arr[1] => Reporting Problems and Bugs.
arr[2] => Other freely available awk implementations.
arr[3] => Making Additions To gawk.
arr[5] => Summary of installation.
arr[7] => Accessing the Git repository.
arr[8] => How to disable certain gawk extensions.
Sign up to request clarification or add additional context in comments.

2 Comments

I don't know whether it matters but order is not preserved.
Output order? It doesn't matter. If the OP wanted to output the lines in the order read it'd be a MUCH simpler script. Instead he wants to populate an array and needs the indices to be provided by the first input value so he's planning to do something other than traverse the array in the order read otherwise he'd want it indexed by lineNr or similar. Obviously if he DID want to process the array in the order read he'd just need a secondary array like idx[lineNr]=flds[1] populated in the first loop to start the indexing from.
1

There is not need to work with arrays as you can just change the field-seperator.

awk 'BEGIN{FS="|"}{print $1, $2}' input.txt

$0 contains the whole line $1 contains the index $2 the sentence

Comments

0

If the record separator is newline, you can echo the variable with quote expansion and use awk as usual, such as

echo "$ArtTEXT" | awk -F"|" '{ix[NR]=$1;arr[$1]=$2} END{for(i=1;i<=NR;i++) print "arr[" ix[i] "] => " arr[ix[i]]}' 

arr[1] => Reporting Problems and Bugs. 
arr[2] => Other freely available awk implementations. 
arr[5] => Summary of installation. 
arr[8] => How to disable certain gawk extensions. 
arr[3] => Making Additions To gawk. 
arr[7] => Accessing the Git repository.

Since awk arrays are not preserving the order you need to keep a separate index for that.

1 Comment

You're assuming the OP meant that ArtTEXT is a SHELL variable. He didn't mention shell anywhere in his question so I'm assuming it's an AWK variable.

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.