Reputation: 8798
Using bash, how can I create a hashmap so that I could read and write lines based on the key?
Some pseudo code:
i = 0
while read text1:
echo $line1 of text1
line2 = text2[i % 3]
echo $line2
## so text2 has 3 lines, I'd like to create hashmap so that I could get the line from text2 based on the line number.
Sample input:
text1:
111111
222222
333333
444444
555555
666666
text2:
AAAAAA
BBBBBB
CCCCCC
And expected output:
111111
AAAAAA
222222
BBBBBB
333333
CCCCCC
444444
AAAAAA
555555
BBBBBB
666666
CCCCCC
Thanks
Upvotes: 1
Views: 85
Reputation: 203129
This is how to really do what you're trying to do in UNIX:
$ awk 'NR==FNR{a[++c]=$0;next} {print $0 ORS a[(FNR-1)%c+1]}' text2 text1
111111
AAAAAA
222222
BBBBBB
333333
CCCCCC
444444
AAAAAA
555555
BBBBBB
666666
CCCCCC
A shell is an environment from which to call tools with a language to sequence those calls, it's not a tool to manipulate text. The people who invented shell also invented awk for shell to call to manipulate text so just do it and avoid all the headaches you'll face if you try to work around it. See why-is-using-a-shell-loop-to-process-text-considered-bad-practice for some of the issues.
Upvotes: 2