bash 연관 배열이고 문자열인자를 썼을때 인자 추출법 질문
연관 배열(associative array)로 key, value 처럼 쓰려는데
배열에서 특정 연산자를 사용해서 key를 알아낼수 있을까요?
declare -A xxx
xxx[abc,101]=1
xxx[abc,201]=2
xxx[def,101]=5
echo ${xxx[@]}
이렇게 하면
1 2 5
이렇게 나옵니다.
키처럼 쓰인 인자 abc,101 abc,201 def,101 도 뽑아내고 싶습니다.
-------------
참고로 아래는 실제 코드 입니다.
sm_data.csv 파일을 읽어서 연관 배열(associative array)에 키,밸류로 쓰려고 저장했습니다.
세개의 컬럼이 콤마로 한 배열인자가 되는데
배열인자만 뽑을수 있나요?
declare -A data_arr
while read d_in_time_s d_in_station d_out_station;
do
r_updown=${updown_arr[$d_in_station,$d_out_station]};
d_in_time_m=${d_in_time_s:0:12};
echo "$d_in_time_m,$d_in_station,$d_out_station,$r_updown";
if [ x${data_arr[$d_in_station,$r_updown,$d_in_time_m]} == x ];
then
data_arr[$d_in_station,$r_updown,$d_in_time_m]=1;
else
r_data=${data_arr[$d_in_station,$r_updown,$d_in_time_m]};
data_arr[$d_in_station,$r_updown,$d_in_time_m]=`expr $r_data + 1`;
fi
done < sm_data.csv > r1.csv
echo ${data_arr[@]}


참고해보세요.
코딩 그라운드 - 웹 컴파일러
http://tpcg.io/E3hsn5
# Hello World Program in Bash Shell declare -A data_arr declare -A test_arr declare -i d_in_time_s=10; declare -i d_in_station=20; declare -i d_out_station=30; declare -i d_AA=10; declare -i d_BB=20; #Numeric indexing #https://www.artificialworlds.net/blog/2012/10/17/bash-associative-array-examples/ declare -A MYMAP=( [go1 go2 go3]=one [go4 go5 go6]=two ) KEYS=(${!MYMAP[@]}) # Make a normal array containing all the keys in the associative array echo ${KEYS[0]} # Find a key via an index echo ${KEYS[1]} # Find a key via an index echo ${KEYS[2]} # Find a key via an index echo ${KEYS[3]} # Find a key via an index echo ${KEYS[4]} # Find a key via an index echo ${KEYS[5]} # Find a key via an index echo ${MYMAP[${KEYS[0]}]} # Find a value via an index echo ${KEYS[@]} # Find a key via an index # Loop through using an index for (( I=0; $I < ${#MYMAP[@]}; I+=1 )); do KEY=${KEYS[$I]}; echo $KEY --- ${MYMAP[$KEY]}; done #echo ${test_arr} #------------------------------------------------------------- #https://stackoverflow.com/questions/3112687/how-to-iterate-over-associative-arrays-in-bash for i in "${!test_arr[@]}" do echo "key : $i" echo "value: ${test_arr[$i]}" done #------------------------------------------------------------- #updown_arr[]배열 생성 - r_updown 에 입력 r_updown=${updown_arr[$d_in_station, $d_out_station]}; #시간 생성 d_in_time_m=${d_in_time_s:0:12}; #출력 echo "$d_in_time_m, $d_in_station, $d_out_station, $r_updown"; #data_arr 생성 x$에서 사용 == x 와 같으면. if [ x${data_arr[$d_in_station,$r_updown,$d_in_time_m]} == x ]; then #data_arr[] = 1 data_arr[$d_in_station,$r_updown,$d_in_time_m]=1; else #다르면. #r_data 에 data_arr[] 입력 r_data=${ data_arr[$d_in_station,$r_updown,$d_in_time_m]}; #data_arr[] 는 expr $r_data+1 data_arr[$d_in_station,$r_updown,$d_in_time_m]=`expr $r_data + 1`; fi echo ${data_arr[@]}----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.
매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.
각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com
감사합니다
감사합니다. 이제야 댓글 봤네요.
저번에는 급해서 다른 방법을 썼는데 또 필요한 일이 생겨서 검색하다가 이제 봤습니다.
링크도 잘 봤습니다.
댓글 달기