linux - How cut characters from string and put it at the end- In shell -


i want able following:

string1= "hello 3002_3322 3.2.1.log" 

and output like:

output = "3002_3322 3.2.1.log hello" 

i know command sed able need guidance.

thanks!

in sed can do:

sed 's/\([^[:blank:]]*\)[[:blank:]]*\(.*\)/\2 \1/' 

which outputs 3002_3322 3.2.1.log hello.

explanation

  1. the first word captured \([^[:blank:]]*\) \(\) means want capture group use later. [:blank:] posix character class whitespace characters. can see other posix character classes here:

http://www.regular-expressions.info/posixbrackets.html

the outer [] means match of characters, , ^ means character except listed in character class. * means number of occurrences (including 0) of previous character. in total [^[:blank:]]* means match group of characters not whitespace, or first word. have complicated regex because posix sed supports bre (basic regex) greedy matching, , find first word want non-greedy matching.

  1. [[:blank:]]*, explained above, means match group of consecutive whitespaces.

  2. \(.*\) means capture rest of line. . means single character, combined * means match rest of characters.

  3. for replacement, \2 \1 means replace pattern matched 2nd capture group, space, first capture group.


Comments