简体   繁体   中英

How to replace the nth space before a string on each line in a file using sed

I am trying to replace the space before the surname on each line of a file with a comma using sed.

Example Source:

George W Heong§New York§USA
Elizabeth Black§Sheffield, Yorkshire§England
Lucy Jones§Cardiff§Wales
James G K Shackleton§Dallas, Texas§USA
Carl Seddon§Canberra,Australia

Example Ouput:

George W,Heong§New York§USA
Elizabeth,Black§Sheffield, Yorkshire§England
Lucy,Jones§Cardiff§Wales
James G K,Shackleton§Dallas, Texas§USA
Carl,Seddon§Canberra,Australia

I think I've worked out a method to obtain the index of the relevant space as follows:

int idx$ = str.indexOf("§");
int nthSpace = str.lastIndexOf(" ", idx$);

but I haven't been able to work out how to replace the nth instance with the variable nthSpace. This is what have got so far:

sed "s/$nthSpace" "/,/" datain.txt > dataout.txt

Any asistance would be appreciated.

With gensub , available in GNU awk , you can do this:

awk 'BEGIN{FS=OFS="§"} {$1=gensub(/[[:blank:]]([^[:blank:]]+)$/, ",\\1", 1, $1)} 1' file

Output:

George W,Heong§New York§USA
Elizabeth,Black§Sheffield, Yorkshire§England
Lucy,Jones§Cardiff§Wales
James G K,Shackleton§Dallas, Texas§USA
Carl,Seddon§Canberra,Australia

With sed :

sed 's/ \([^ ]*§\)/,\1/' sourcefile

The pattern looks for the first occurence of :

  • a space
  • followed by any non-space char
  • followed by §

The name is captured in a group that is used in the substitution to be prefixed with a ,

UPDATE :

To prevent strings as name § to be matched, you can preprocess the first substitution with s/ +§/§/ . The final command will be :

sed 's/ +§/§/;s/ \([^ ]*§\)/,\1/' sourcefile

As noticed in question comments, multipart surnames (separated with spaces) will be split if not rewritten manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM