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?

Hanan Cohen
  • 383
  • 2
  • 15
  • possible duplicate of [split string to array using awk](http://stackoverflow.com/questions/8009664/split-string-to-array-using-awk) – Oliver Friedrich Sep 06 '15 at 09: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? – Ed Morton Sep 06 '15 at 15:05

3 Answers3

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.
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • I don't know whether it matters but order is not preserved. – karakfa Sep 07 '15 at 14:47
  • 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. – Ed Morton Sep 07 '15 at 15:04
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

Oliver Friedrich
  • 9,018
  • 9
  • 42
  • 48
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.

karakfa
  • 66,216
  • 7
  • 41
  • 56
  • 1
    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. – Ed Morton Sep 07 '15 at 15:15