Skip to main content

Timeline for Intersection of two arrays in BASH

Current License: CC BY-SA 3.0

8 events
when toggle format what by license comment
Dec 8, 2018 at 18:08 comment added Sorpigal One should not set LC_ALL=C. Instead set LC_COLLATE=C for the same performance gain without other side effects. In order to obtain correct results you will also need to set the same collation for comm that was used for sort, e.g.: unset LC_ALL; LC_COLLATE=C ; comm -12 <(printf '%s\n' "${A[@]}" | sort) <(printf '%s\n' "${B[@]}" | sort)
Aug 18, 2015 at 8:19 comment added Jason R. Mick To protect \n try this: arr1=( one two three "four five\nsix\nseven" ); arr2=( ${arr1[@]:1} "four five\\nsix" ); n1=${#arr1[@]}; n2=${#arr2[@]}; arr=( ${arr1[@]/ /'-_-'} ${arr2[@]/ /'-_-'} ); arr=( $( echo "${arr[@]}"|tr '\t' '-t-'|tr '\n' '-n-'|tr '\r' '-r-' ) ); arr1=( ${arr[@]:0:${n1}} ); arr2=( ${arr[@]:${n1}:${n2}} ); unset arr; printf "%0.s-" {1..10}; printf '\n'; printf '{'; printf " \"%s\" " "${arr1[@]}"; printf '}\n'; printf "%0.s-" {1..10}; printf '\n'; printf '{'; printf " \"%s\" " "${arr2[@]}"; printf '}\n'; printf "%0.s-" {1..10}; printf '\n\n'; unset arr1; unset arr2
Dec 13, 2013 at 11:10 comment added Chris Down I've seen it in filenames when using GUI file managers that do not properly sanitise input filenames that are copied from somewhere else (also, nobody said anything about filenames).
Dec 13, 2013 at 10:19 history edited camh CC BY-SA 3.0
deleted 4 characters in body
Dec 13, 2013 at 10:17 comment added camh @ChrisDown: That's right. I always try to write shell scripts that are properly quoted and handle all chars, but I've given up on \n. I have NEVER seen it in a filename, and a large bunch of unix tools work with \n delimited records that you lose a lot if you try to handle \n as a valid char.
Dec 13, 2013 at 8:54 vote accept Bogdan
Dec 12, 2013 at 16:08 comment added Chris Down This will only work if your values don't contain \n.
Dec 12, 2013 at 10:06 history answered camh CC BY-SA 3.0