2

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?

2
  • 1
    Short answer: no. Use sed. Commented Nov 21, 2017 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. Commented Nov 21, 2017 at 16:56

1 Answer 1

2

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 [^\\]

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.