Split name on new line

I have a file with and one line in the file looks like this:

GIVEN=David Smith
GIVEN=John Doe Young
GIVEN=Ms Sam Parker
GIVEN=Mr James Free Foo ABC
GIVEN=Joe Cam-Barr

I just want to find lines starting with GIVEN, and find the last space (assuming it's a last name), then create a new line.

So input =

FOO=Bar
GIVEN=David Smith
Baz=123

The exit should be

FOO=Bar
GIVEN=David
LAST=Smith
Baz=123

This is how much I can get:

(?<=(GIVEN=))(.*\ )

See here for a demo http://regexr.com?30uh8

+3
source share
4 answers
open(my $IN, "<infile.txt") or die $!;
chomp(my @lines = <$IN>);
close $IN;

foreach(@lines){
  s/^(GIVEN\=.+)\s+(\S+)$/$1\nLAST=$2/;
}

open(my $OUT,">outfile.txt") or die $!;
print "$_\n" foreach(@lines);
close $OUT;

Must work. Change if necessary to read line by line if the input file is very large.

+1
source
awk ' /^GIVEN=/ {last=$NF; $NF=""; print; print "LAST=" last; next} 1' filename
+1
source

substr rindex . rindex , , substr :

This substr works on $_, starts at the position given rindex, replaces the following character 1 \nLAST=:

while( <> ) {
    substr( $_, rindex( $_, ' ' ), 1, "\nLAST=" ) if /\AGIVEN=/;
    print;
    }

When you look at this code, you see that you already need it in the form for a single-line interface, although in this case I use generalized quoting to avoid problems with shell interpolation:

% perl -pi.old -e 'substr($_,rindex($_,q( )),1,qq(\nLAST=)) if /\AGIVEN=/' ...

This, however, can lead to the distortion of the names of some people. Not every surname is one word. Asking for a person is the only good way to find out what their last name is.

+1
source
thames.434> cat file
    FOO=Bar
    GIVEN=David Smith
    Baz=123

thames.435> awk '{if ($0~/GIVEN/){x=$2;$2="";print;print "LAST=",x}else print}' file
    FOO=Bar
GIVEN=David 
LAST= Smith
    Baz=123
0
source

All Articles