2

I've read this thread https://unix.stackexchange.com/a/7718/256195, that only if var doesn't contain any tab/spaces but in my case it does contain spaces, like below example:

"this is a test"      this_is_a_solid_line_that_doesnot_contain_tab_or_spaces

command column will separate this is ..etc also, but I'd want something acts on only "this is a test" and this_is_a_solid_line_that_doesnot_contain_tab_or_spaces.

Purpose: I have a bunch of lines like above in a file that don't be aligned properly.

Tuyen Pham
  • 1,765
  • 1
  • 16
  • 46
  • Are the fields tab-delimited? – Kusalananda Nov 29 '18 at 07:55
  • Possibly of some help (if the fields are tab-delimited): [Align columns evenly without "column - t"](//unix.stackexchange.com/q/484390) – Kusalananda Nov 29 '18 at 07:56
  • Thanks, what would it be when I'm using pipeline like: `cat file | somethine_here_to_filter`, what would your script will be put after a pipeline? – Tuyen Pham Nov 29 '18 at 08:00
  • The shell script in the second half of my answer expects a filename, so `./script.sh file`. To make it work on a pipe, use `something-that-produces-data | ./script.sh /dev/stdin`. – Kusalananda Nov 29 '18 at 08:05
  • @Kusalananda: I tried but with `var1 test` it produces `"var1.................................................................test"` that somehow treat spaces as tab. – Tuyen Pham Nov 29 '18 at 08:32
  • Nobody yet knows what delimiter your input uses, and I don't know what command you have tried. – Kusalananda Nov 29 '18 at 08:39

1 Answers1

2

Assuming the input doesn't contain | characters, you could convert those sequences of whitespace that are not inside quotes to | (or any other character that doesn't occur in the input) and then pipe to column -ts'|':

<input.txt perl -lpe 's/(".*?")|\s+/$1||"|"/ge' | column -ts'|'
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501