Skip to content

sed command not replacing/removing tabs #9

Description

@douglasdd

((Thanks, https://iridakos.com/programming/2018/03/01/bash-programmable-completion-tutorial Is super-accessible, and very helpful!))

From this place in your doc

replace tabs with spaces ...
sed to the rescue.
... fc -l -50 | sed 's/\t//' ...

Actually removes the first plain "t" character!

Instead, to replace 1 tab character with 1 space,
the sed command you want is:

$ fc -l -50 | sed $'s/\t/ /'

Notes:

  • Bash '' strings do not do C-style escape sequences
    • (neither to "" strings in Bash)
    • only $'' bash strings process C-style escape sequences
      • (recall that echo -e "a\tb" TAB expansion is done inside the echo builtin code)
  • so sed sees '\t' in ARGV
    • which it interprets as:
      1. an unnecessary '\' character, then
      2. a plain 't' character
  • Your sed removes what it matches, it does not replace
  • Plain fc output already has 1 TAB and 1 SPACE between the num & command
  • ...So it's really not clear if you want 1 space or 2 in the end result

Full log:

## Raw `fc` output:
## (using `... | cat -vet` to show TAB chars as "^I")
$ fc -l -5 | cat -vet
57^I . ~/ds/env/complete-dothis.bash $
58^I . ~/ds/env/complete-dothis.bash $
59^I _comp_debug_dothis=1$
60^I . ~/ds/env/complete-dothis.bash $
61^I dothis fc -l -5 | sed $'s/\t/ /'$

## Your current sed command:
## (note "complee", not complete")
$ fc -l -5 | sed 's/\t//' | cat -vet
58^I . ~/ds/env/complee-dothis.bash $
59^I _comp_debug_dohis=1$
60^I . ~/ds/env/complee-dothis.bash $
61^I dohis fc -l -5 | sed $'s/\t/ /'$
62^I fc -l -5 | ca -vet$

## Fixed sed command:
$ fc -l -5 | sed $'s/\t/ /' | cat -vet
59  _comp_debug_dothis=1$
60  . ~/ds/env/complete-dothis.bash $
61  dothis fc -l -5 | sed $'s/\t/ /'$
62  fc -l -5 | cat -vet$
63  fc -l -5 | sed 's/\t//' | cat -vet$
[12:43:57 Sun Feb 14] X:0 !65

$ echo "${BASH_VERSINFO[@]}"
3 2 57 1 release x86_64-apple-darwin18
# Mac 10.14.6

# ...but same behavior w/ Bash 5 (from HomeBrew)
5 0 18 1 release x86_64-apple-darwin18.7.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions