regex - How to group strings with whitespace, using sed? -


suppose text file following strings:

apple foo foobar banana foo foobar1 abc b c orange barfoo pear foo 

how group strings comes after apple, banana, orange, , pear?

i apple, wouldn't work rest of text files.

sed 's/\([^ ]*\) \([^ ]*\) \([^ ]*\)/\2 \3/'

i want output this:

foo foobar foo foobar1 abc b c barfoo foo 

is there general case can print these strings after first whitespace?

sed -r 's/^[^ ]+[ ]+//' in.txt 

(gnu sed; on osx, use -e instead of -r).


update:

as @jotne points out, initial ^ not strictly needed in case - though makes intent clearer; similarly, can drop [] around second space char.

the above deals spaces separating columns (potentially multiple ones, final + in regex), whereas op more mentions whitespace.

generalized whitespace version:

note: in forms below, \s , [:space:] match kinds of whitespace, including newlines. if wanted restrict matching spaces , tabs, use [ \t] or [:blank:].

sed -r 's/^\s+\s+//' in.txt 

(gnu sed; form not work on osx, -e.)

posix-compliant version (e.g., aix - thanks, @neronlevelu):

sed  's/^[^[:space:]]\{1,\}[[:space:]]\{1,\}//' in.txt 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -