How do I get from this:
randomcollege-nt\user90
to this:
user90
using sed?
How do I get from this:
randomcollege-nt\user90
to this:
user90
using sed?
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
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
grep -F the whole string.
– Roboman1723
Sep 04 '14 at 15:12
Example: randomcollege-nt\user90 randomcollege-nt\user91 randomcollege-nt\user92
Output: user90 user91 user92
– Roboman1723 Sep 04 '14 at 15:46I know you want to use sed, but I'd use something different...
echo "randomcollege-nt\user90" | cut -d'\' -f2
Another sed:
$ echo "randomcollege-nt\user90" | LC_ALL=C sed -e 's/.*\\//'
user90
or POSIXly:
$ a='randomcollege-nt\user90'
$ printf '%s\n' "${a##*\\}"
user90
sh feature. http://pubs.opengroup.org/onlinepubs/009604599/utilities/xcu_chap02.html#tag_02_06_02
–
Sep 05 '14 at 13:40
Rather you use 'awk' to filter "user90":
echo "randomcollege-nt\user90" | awk -F\\ {'print $2'}
-F can be a regex, which allows for removing prefixes that match a pattern (and thus don't have to be exactly known strings). Also, when the input has multiple lines (commonly the case with a file), you can use 'NF > 1 {print $2}' to avoid awk printing empty lines. For example, awk -F "^Some Key *: " 'NF > 1 {print $2}' will get you everything after lines starting with "Some Key : " where the amount of spaces before the colon can vary.
– Matthias Braun
Apr 28 '21 at 21:09
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.
This simple grep command will do the job,
$ echo 'randomcollege-nt\user90' | grep -oP '[^\\]*$'
user90
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
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.
In case anyone is trying to remove a commented out # server_tokens off; from nginx automatically to help with auto dev-ops:
sudo sed -ri 's/#\s(server_tokens off;)/\1/' /etc/nginx/nginx.conf.
Tested and working for nginx/1.12.1 on Ubuntu 16.04 LTS. Related answer to locking down NGINX by turning off server tokens here.
man the_command_nameand 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. – DBedrenko Sep 05 '14 at 07:05sedvisit : http://www.grymoire.com/Unix/Sed.html – Pandya Sep 05 '14 at 10:29