Tag Info

Hot answers tagged

3

There is a way in bash 4.3+, which probably comes from ksh: echo_idx_array () # array index { local -n array=$1 # add nameref attribute local idx=$2 echo "${array[idx]}" } $ names=(one two three four) $ echo_idx_array names 2 three $ days=([monday]=eggs [tuesday]=bread [sunday]=jam) # associative array $ echo_idx_array days sunday jam ...


1

You could use sed like this: sed 'h;s/,[^|]*//g;x /,/{s/|[^,|]*,*/|-/g;H };x;s/-\([^|]\)/\1/g;P;D' It wound up being relatively simple after all. Applying that little script to your data gets: key1|0|11881|0|0|0|0|11769|0|0|0 key2|2027|345|0|1|0|2040|364|0|1|0 key2|-|712|-|-|-|-|729|-|-|- key3|0|670944|0|0|0|0|495554|0|0|0 ...


1

I would not use awk for this, because I think it needs more flexible data structures. I would use perl: perl -MList::Util=max -F'\|' -lane ' $key = shift @F; @data = map {[split /,/]} @F; do { @row = map {shift(@$_) // "-"} @data; print join("|", $key, @row); $max = max map {scalar @$_} @data; } while ($max > 0); ' ...


1

You were close, just unnecessarily complicated this task. Try awk -f script.awk main.txt index.txt (notice reverse order of files) with the following script: #!/bin/awk BEGIN { FS = "|" } ( NR == FNR ) { lookup[toupper($1)] = $0 } ( NR > FNR ) { key = toupper($1) n=split(lookup[key], replacements, "|") replacements[$2+1]=$3 for ...



Only top voted, non community-wiki answers of a minimum length are eligible