Reputation: 1628
I've got a text file (clients)
that contains around 150 lines of information
Each line is similar to :
"2","USERID","ACCESSCODE" Eg:
"1","545ghu","7687686686868709ioo98968g"
"1","G2567u","54564df76786470976476987w"
"1","Y5po97","iuioubhjgjg768b79j9890980"
I want to grep this file, only find entries containing G2 or Y5 in the second column, remove all Double quotes and send the result to an array.
I can do this with
foo=( $(grep 'G2\|Y5' clients | sed 's/"//g') )
This result in the array foo
which contains entries like :
foo[0]
= 1,G2567u,54564df76786470976476987w
What I'd like is the results in foo
to look like this:
G2567u (54564df76786470976476987w)
Can someone advise how to do this ?
Thanks
Upvotes: 2
Views: 872
Reputation:
I try to use bash string slice, while loop to extract data into bash array.
declare -a arr
while IFS="\r\n" read -r line; do
# replace "," to | as field delimiter
line="${line//\",\"/|}"
# remove "
line="${line//\"/}"
# remove first field via delimiter |
line="${line#*|}"
# extract userid
userid="${line%%|*}"
if [[ "${userid}" =~ (G2|Y5) ]]; then
# extract access_code
access_code="${line##*|}"
# arr+=( "${userid} (${access_code})" )
arr[${#arr[@]}]="${userid} (${access_code})"
fi
done < <(echo "${clients}")
# done < /PATH/clients.txt
echo "${arr[@]}"
echo "${#arr[@]}"
Upvotes: 0
Reputation: 295805
There's no need for any external command -- awk
, sed
or grep
-- here; all the necessary primitives are available native to bash itself.
array=( )
while IFS='",' read -r num userid access_code _; do
if [[ $userid =~ (G2|Y5) ]]; then
array+=( "${userid} (${access_code})" )
fi
done < clients
See:
hosts=( $(aws ...) )
is an antipatternUpvotes: 3
Reputation: 67567
awk
for text manipulation (and separation of concerns) with readarray
to convert the output to bash
array
to simplify parsing, set quote and comma as field delimiters, which will change the field index.
$ readarray ar < <(awk -F'[",]' '$5~/G2|Y5/{print $5,"("$8")"}' file)
$ echo "${ar[0]}"
G2567u (54564df76786470976476987w)
also, perhaps you're looking for begins with instead of contains for the special values. In that case to eliminate false positives change the condition to $5~/^(G2|Y5)/
Upvotes: 1