Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How do I get from this:

randomcollege-nt\user90

to this:

user90

using sed?

share|improve this question
1  
A helpful tip: when you are not sure what a command does, type in man the_command_name and you will get a helpful instruction manual for the command. You can also go to www.google.com and type in "command_name tutorial" and you will find plenty of step-by-side guides. –  NewWorld Sep 5 at 7:05
1  
For tutorial on sed visit : grymoire.com/Unix/Sed.html –  Pandya Sep 5 at 10:29

9 Answers 9

up vote 8 down vote accepted

I'd use a simple grep to look for user90:

$ echo "randomcollege-nt\user90" | grep -o user90
user90

If user90 is not constant, prefer this command:

$ echo "randomcollege-nt\user90" | grep -oP '(?<=randomcollege-nt\\)\w+'
user90

Finally using sed to edit the file in place:

$ sed -ri 's/randomcollege-nt\\(user[0-9]+)/\1/' my_file

Or to match all possible user accounts:

$ sed -ri 's/randomcollege-nt\\(\w+)/\1/' my_file
share|improve this answer
    
Funny thing is I tried that and it nothing returned. I'm using 12.04 if that matters. It only shows up if I grep -F the whole string. –  Roboman1723 Sep 4 at 15:12
2  
@muru good catch, thanks. I've updated my answer –  Sylvain Pineau Sep 4 at 15:15
    
@Roboman1723 tested on 12.04, my first commands works. Be sure that you don't have a grep alias defined somewhere. BTW as it seems you're looking for inline replacement with sed I've added such command. –  Sylvain Pineau Sep 4 at 15:41
    
Is there a way you could make that sed command cut out all "randomcollege-nt\" but display more than just user 90? Example: randomcollege-nt\user90 randomcollege-nt\user91 randomcollege-nt\user92 Output: user90 user91 user92 –  Roboman1723 Sep 4 at 15:46
1  
@Roboman1723 You should attach your original file and your desired output to your question so that everyone willing to help you can see it. –  Sylvain Pineau Sep 4 at 15:49

You're parsing some text to extract the username from a domain\username string, most likely from Windows. Most of the above answers are only addressing your specific example string.

The best way to do this is using regex in sed to extract whatever comes after \. Here's how you would do it:

sed 's|.*\\\(.*\)|\1|'

That will match everything (.*) until a backslash (here, we're escaping it, so it's \\), then match everything after the backslash (.*), but making it a capture group (i.e. wrap brackets around it, but we also have to escape them, so \(.*\)). Now that we have whatever comes after the \ in the string as a capture group, we print it by referencing it with \1.

You can use the above sed command with any domain name, not necessarily randomcollege-nt.

$ echo "randomcollege-nt\user90" | sed 's|.*\\\(.*\)|\1|'
user90

$ echo "domain\username" | sed 's|.*\\\(.*\)|\1|'
username

$ echo "anydomainname\roboman1723" | sed 's|.*\\\(.*\)|\1|'
roboman1723
share|improve this answer

Another sed:

$ echo "randomcollege-nt\user90" | sed -e 's/.*\\//'
user90

or POSIXly:

$ a='randomcollege-nt\user90'
$ printf '%s\n' "${a##*\\}"
user90
share|improve this answer
1  
It's not a bashism, it's a POSIX sh feature. pubs.opengroup.org/onlinepubs/009604599/utilities/… –  nyuszika7h Sep 5 at 13:40
    
@nyuszika7h: Oh, forget about this, updated. –  Gnouc Sep 5 at 15:15
    
+1. didn't see your answer, I'll remove my comment on OP's ^^ –  Olivier Dulac Sep 5 at 19:38

I know you want to use sed, but I'd use something different...

echo "randomcollege-nt\user90" | cut -d'\' -f2
share|improve this answer
    
Knowing how to use a tool is just as important as knowing which tool to use. –  Rafał Cieślak Sep 4 at 16:10

Is this the question ?

$ echo randomcollege-nt\user90| sed -e s,randomcollege-nt\,,
user90

if the sting randomcollege-nt is not contant use the awk commande above/below.

share|improve this answer
1  
when I run that I get /user90 not user90 –  Roboman1723 Sep 4 at 15:08
2  
No that's alright,I got user90. –  saptarshi nag Sep 4 at 15:41

Rather you use 'awk' to filter "user90":

echo "randomcollege-nt\user90" | awk -F\\ {'print $2'}
share|improve this answer
    
Is they're a way with awk to edit the file in place and not have to output it to a new file? –  Roboman1723 Sep 4 at 15:12
    
@Roboman1723 see stackoverflow.com/a/16531920/2072269 –  muru Sep 4 at 15:16
    
Yes you have to use -f option to awk.For details see man page. –  saptarshi nag Sep 4 at 15:23

This simple grep command will do the job,

$ echo 'randomcollege-nt\user90' | grep -oP '[^\\]*$'
user90
share|improve this answer


With sed delete everything in a string before a specific character (define into double bracket [Specific char]).

echo "randomcollege-nt\user90" | sed 's/.*[\]//'

Means replace all (.*[\]) characters before a \ char with whitespace character(//)

If you have a file and want to inplace replace use -i flag in sed command like this:

sed -i 's/.*[\]//' /path/to/FileName
share|improve this answer

The original question asked for sed, but I see that alternatives are popular here.

If you are using Bash, parameter expansion is by far the simplest:

ORIGIN='randomcollege-nt\user90'
echo "${ORIGIN#*\\}"

If you are potentially expecting more than one backslash, double the hash signs:

echo "${ORIGIN##*\\}"

For more information, man bash and search for Parameter Expansion.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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