I have a file with text in the format of:

User\ Name My\ Password

Notice this is actually a 2 column file, where columns contain escaped spaces.

cut -d' ' -f2 produces Name but I want to produce My Password.

Is it possible to use cut and delimited only based on non-escaped spaces? If not, what alternate command can I use?

  • 1
    Short answer: no. Use sed. Nov 21 '17 at 16:54
  • I saw your other question, you could use a special character to separate your columns. Then there is no need to escape the spaces.
    – ctx
    Nov 21 '17 at 16:56

I would suggest GNU grep approach:

Sample input.txt:

User\ Name My\ Password
first\ field second\ field

Cut the 2nd field from 2-columned file:

grep -Po '[^\\]\x20\K.*' input.txt

The output:

My\ Password
second\ field

Or the same with sed:

sed 's/.*[^\\]\x20\(.*\)/\1/' input.txt


The crucial regex pattern part is [^\\]\x20 that matches a space \x20(space hex code) if it's preceded by any char except escaping backslash [^\\]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.