0

I have a list of usernames and i would like add possible combinations to it.

Example. Lets say this is the list I have

johna maryb charlesc 

Is there is a way to use sed to edit it the way it looks like

ajohn bmary ccharles 

And also

john_a mary_b charles_c 

etc...

Can anyone assist me into getting the commands to do so, any explanation will be awesome as well. I would like to understand how it works if possible. I usually get confused when I see things like 's/\.(.*.... without knowing what some of those mean... anyway thanks in advance.

EDIT ... I change the username

1
  • For understanding commands you find online you should just read a tutorial like this one. Commented Jun 28, 2014 at 19:42

5 Answers 5

1

sed s/\(user\)\(.\)/\2\1/

Breakdown:

sed s/string/replacement/ will replace all instances of string with replacement.

Then, string in that sed expression is \(user\)\(.\). This can be broken down into two parts: \(user\) and \(.\). Each of these is a capture group - bracketed by \( \). That means that once we've matched something with them, we can reuse it in the replacement string.

\(user\) matches, surprisingly enough, the user part of the string. \(.\) matches any single character - that's what the . means. Then, you have two captured groups - user and a (or b or c).

The replacement part just uses these to recreate the pattern a little differently. \2\1 says "print the second capture group, then the first capture group". Which in this case, will print out auser - since we matched user and a with each group.

ex:

$ echo "usera > userb > userc" | sed "s/\(user\)\(.\)/\2\1/" auser buser cuser 

You can change the \2\1 to use any string you want - ie. \2_\1 will give a_user, b_user, c_user.

Also, in order to match any preceding string (not just "user"), just replace the \(user\) with \(.*\). Ex:

$ echo "marya > johnb > alfredc" | sed "s/\(.*\)\(.\)/\2\1/" amary bjohn calfred 
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Commander, thanks for your reply. I made some edits to point to my needs more specific. My apologies because i wasnt clear at the beginning.
+1, but, in your explanation, I suggest saying calling the 1st s argument regex (or some such thing) rather than string; also, there is no need for the g option.
@user3635042: Simply replacing user with .* in the sed command will do what you want.
No, I guess there isn't, hey? I have a bad habit of just typing the g any time I want to match more than one pattern... ;) And like @mklement0 says, it's trivial to make this match other strings than user - editing.
@mklement0 that solved it. Thank you very much :D Commander, thanks
0

here's a partial answer to what is probably the easy part. To use sed to change usera to user_a you could use:

sed 's/user/user_/' temp 

where temp is the name of the file that contains your initial list of usernames. How this works: It is finding the first instance of "user" on each line and replacing it with "user_"

Similarly for your dot example:

sed 's/user/user./' temp 

will replace the first instance of "user" on each line with "user."

1 Comment

Thanks dlahr. For the purpose of all the users have the same name pattern this will work fine. Well, maybe i didnt asked it right, my fault... but what if instead of usera userb, would be johnc marym etc
0

Sed does not offer non-greedy regex, so I suggest perl:

perl -pe 's/(.*?)(.)$/$2$1/g' file ajohn bmary ccharles perl -pe 's/(.*?)(.)$/$1_$2/g' file john_a mary_b charles_c 

That way you don't need to know the username before hand.

Comments

0

Simple solution using awk

awk '{a=$NF;$NF="";$0=a$0}1' FS="" OFS="" file ajohn bmary ccharles 

and

awk '{a=$NF;$NF="";$0=$0"_" a}1' FS="" OFS="" file john_a mary_b charles_c 

By setting FS to nothing, every letter is a field in awk. You can then easy manipulate it.
And no need to using capturing groups etc, just plain field swapping.

Comments

0

This might work for you (GNU sed):

sed -r 's/^([^_]*)_?(.)$/\2\1/' file 

This matches any charactes other than underscores (in the first back reference (\1)), a possible underscore and the last character (in the second back reference (\2)) and swaps them around.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.