-2

I'd prefer to edit TestFile in place to get the desired output. How could I do this? This is a work box so I'd prefer to not have to install additional packages.

TestFile:

randomcollege-nt\jsmith90
randomcollege-nt\aakhbar91
randomcollege-nt\pmanning92
randomcollege-nt\dvader93
jshephard94
bwayne95

Desired output:

jsmith90
aakhbar91
pmanning92
dvader93
jshephard94
bwayne95
Roboman1723
  • 2,915
  • 8
  • 23
  • 31

4 Answers4

6

You can use sed:

sed -i~ 's/.*\\//' TestFile
  • -i~ tells sed to create a backup with the ~ extension.
  • s/// is substitution.
  • .*\\ means anything (.*) followed by a backslash
  • the replacement part is empty //, i.e. everything up to a backslash will be replaced by nothing.
choroba
  • 9,643
3

You can get output simply by using command cut:

cut -d '\' -f 2 < TestFile

Where -d is used to determine delimiter which is / here and -f for selecting fields which is 2 here.

Example:

$ cat TestFile
randomcollege-nt\jsmith90
randomcollege-nt\aakhbar91
randomcollege-nt\pmanning92
randomcollege-nt\dvader93
jshephard94
bwayne95

$ cut -d '\' -f 2 < TestFile
jsmith90
aakhbar91
pmanning92
dvader93
jshephard94
bwayne95

If you want to save output to another file then use:

cut -d '\' -f 2 < TestFile > Output

If you want to overwrite TestFile then use following command:

cut -d '\' -f 2 < TestFile|tee TestFile
Pandya
  • 35,771
  • 44
  • 128
  • 188
2

Though two correct answers have been given but I am giving my personal solution--

sed 's/randomcollege-nt\\//g' testfile

Thank you,

1

This is another solution using awk:

 awk -F "\" '{print $(NF)}' testfile > outFile

where -F define the input field separator, than print $(NF) print out the last field.

outFile will contains:

jsmith90
aakhbar91
pmanning92
dvader93
jshephard94
bwayne95
Lety
  • 6,039
  • 2
  • 29
  • 37