Project

General

Profile

« Previous | Next » 

Revision 3772

expand_braces: Fixed bug where `head -1` seemed to read more lines than just the first, causing EOF to be returned after the first line, by using `read` instead. Support data containing \r (such as Excel-dialect CSVs) by removing it. Fixed bug where ./{...} was not being properly escaped.

View differences:

expand_braces
1
#!/bin/sh
1
#!/bin/bash
2 2
# Expands bash-style {} expressions.
3 3
# See <http://www.gnu.org/software/bash/manual/bash.html#Brace-Expansion>.
4 4
# Filters each line from stdin to stdout.
......
10 10

  
11 11
echoEach () { local line; for line in "$@"; do echo "$line"; done; }
12 12

  
13
braceKeepRe="./'{'"
14
braceKeepRepl="./{"
13
singleQuote="'"
14
singleQuoteEsc="'\''"
15
CR=$'\r'
16
LF=$'\n'
15 17

  
16 18
while true; do
17
    line="$(head -1)" # get next line from stdin; $() strips trailing newlines
19
    read # get next line from stdin; strips trailing newlines
20
    line="$REPLY"
18 21
    test -z "$line" && break # EOF if no line or empty line
19
    line="$(echo "$line"|sed -e "s/[^{,}]+/'&'/g")" # escape special chars
20
    line="${line//$braceKeepRe/$braceKeepRepl}" # don't expand ./{
21
    eval "echoEach $line" # brace-expand $line
22
    
23
    line="${line//$CR/}" # remove \r
24
    line="${line//$singleQuote/$singleQuoteEsc}" # escape single quotes
25
    # Escape non-brace chars; don't expand ./{
26
    line="$(echo "$line"\
27
|sed -e "s/[^{,}]+/'&'/g"|sed -e "s/\.\/'\{'([^}]*)'\}'/.\/{\1}/g")"
28
    
29
    eval echoEach $line # brace-expand $line
22 30
done

Also available in: Unified diff