Skip to main content
added 139 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Using gsub() in awk on the last ;-delimited field:

$ awk -F ';' 'BEGIN { OFS=FS } { gsub(" ", ",", $NF); print }' file
some name;another thing; random;,value,value,value,value,value

Using sed and assuming we'd like to replace all spaces after the last ; with commas:

$ sed 'h;s/.*;//;y/ /,/;x;s/;[^;]*$//;G;s/\n/;/' file
some name;another thing; random;,value,value,value,value,value

Annotated sed script:

h               ; # Duplicate line into hold space
s/.*;//         ; # Delete up to the last ;
y/ /,/          ; # Change space to comma in remaining data
x               ; # Swap pattern and hold spaces
s/;[^;]*$//     ; # Delete from the last ;
G               ; # Append hold space delimited by newline
s/\n/;/         ; # Replace the embedded newline with ;
                ; # (implicit print)

The "hold space" is a separate storage buffer that sed provides. The "pattern space" is the buffer into which the data is read from the input and to which modifications may be applied.

Using gsub() in awk on the last ;-delimited field:

$ awk -F ';' 'BEGIN { OFS=FS } { gsub(" ", ",", $NF); print }' file
some name;another thing; random;,value,value,value,value,value

Using sed and assuming we'd like to replace all spaces after the last ; with commas:

$ sed 'h;s/.*;//;y/ /,/;x;s/;[^;]*$//;G;s/\n/;/' file
some name;another thing; random;,value,value,value,value,value

Annotated sed script:

h               ; # Duplicate line into hold space
s/.*;//         ; # Delete up to the last ;
y/ /,/          ; # Change space to comma in remaining data
x               ; # Swap pattern and hold spaces
s/;[^;]*$//     ; # Delete from the last ;
G               ; # Append hold space delimited by newline
s/\n/;/         ; # Replace the embedded newline with ;
                ; # (implicit print)

Using gsub() in awk on the last ;-delimited field:

$ awk -F ';' 'BEGIN { OFS=FS } { gsub(" ", ",", $NF); print }' file
some name;another thing; random;,value,value,value,value,value

Using sed and assuming we'd like to replace all spaces after the last ; with commas:

$ sed 'h;s/.*;//;y/ /,/;x;s/;[^;]*$//;G;s/\n/;/' file
some name;another thing; random;,value,value,value,value,value

Annotated sed script:

h               ; # Duplicate line into hold space
s/.*;//         ; # Delete up to the last ;
y/ /,/          ; # Change space to comma in remaining data
x               ; # Swap pattern and hold spaces
s/;[^;]*$//     ; # Delete from the last ;
G               ; # Append hold space delimited by newline
s/\n/;/         ; # Replace the embedded newline with ;
                ; # (implicit print)

The "hold space" is a separate storage buffer that sed provides. The "pattern space" is the buffer into which the data is read from the input and to which modifications may be applied.

deleted 1 character in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Using gsub() in awk on the fourthlast ;-delimited field:

$ awk -F ';' 'BEGIN { OFS=FS } { gsub(" ", ",", $4$NF); print }' file
some name;another thing; random;,value,value,value,value,value

Using sed and assuming we'd like to replace all spaces after the last ; with commas:

$ sed 'h;s/.*;//;y/ /,/;x;s/;[^;]*$//;G;s/\n/;/' file
some name;another thing; random;,value,value,value,value,value

Annotated sed script:

h               ; # Duplicate line into hold space
s/.*;//         ; # Delete up to the last ;
y/ /,/          ; # Change space to comma in remaining data
x               ; # Swap pattern and hold spaces
s/;[^;]*$//     ; # Delete from the last ;
G               ; # Append hold space delimited by newline
s/\n/;/         ; # Replace the embedded newline with ;
                ; # (implicit print)

Using gsub() in awk on the fourth ;-delimited field:

$ awk -F ';' 'BEGIN { OFS=FS } { gsub(" ", ",", $4); print }' file
some name;another thing; random;,value,value,value,value,value

Using sed and assuming we'd like to replace all spaces after the last ; with commas:

$ sed 'h;s/.*;//;y/ /,/;x;s/;[^;]*$//;G;s/\n/;/' file
some name;another thing; random;,value,value,value,value,value

Annotated sed script:

h               ; # Duplicate line into hold space
s/.*;//         ; # Delete up to the last ;
y/ /,/          ; # Change space to comma in remaining data
x               ; # Swap pattern and hold spaces
s/;[^;]*$//     ; # Delete from the last ;
G               ; # Append hold space delimited by newline
s/\n/;/         ; # Replace the embedded newline with ;
                ; # (implicit print)

Using gsub() in awk on the last ;-delimited field:

$ awk -F ';' 'BEGIN { OFS=FS } { gsub(" ", ",", $NF); print }' file
some name;another thing; random;,value,value,value,value,value

Using sed and assuming we'd like to replace all spaces after the last ; with commas:

$ sed 'h;s/.*;//;y/ /,/;x;s/;[^;]*$//;G;s/\n/;/' file
some name;another thing; random;,value,value,value,value,value

Annotated sed script:

h               ; # Duplicate line into hold space
s/.*;//         ; # Delete up to the last ;
y/ /,/          ; # Change space to comma in remaining data
x               ; # Swap pattern and hold spaces
s/;[^;]*$//     ; # Delete from the last ;
G               ; # Append hold space delimited by newline
s/\n/;/         ; # Replace the embedded newline with ;
                ; # (implicit print)
added 29 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Using gsub() in awk on the fourth ;-delimited field:

$ awk -F ';' 'BEGIN { OFS=FS } { gsub(" ", ",", $4); print }' file
some name;another thing; random;,value,value,value,value,value

Using sed and assuming we'd like to replace all spaces after the last ; with commas:

$ sed 'h;s/.*;//;y/ /,/;x;s/;[^;]*$//;G;s/\n/;/' file
some name;another thing; random;,value,value,value,value,value

Annotated sed script:

h               ; # Duplicate line into hold space
s/.*;//         ; # Delete up to the last ;
y/ /,/          ; # Change space to comma in remaining data
x               ; # Swap pattern and hold spaces
s/;[^;]*$//     ; # Delete from the last ;
G               ; # Append hold space delimited by newline
s/\n/;/         ; # RemoveReplace the embedded newline with ;
                ; # (implicit print)

Using gsub() in awk on the fourth ;-delimited field:

$ awk -F ';' 'BEGIN { OFS=FS } { gsub(" ", ",", $4); print }' file
some name;another thing; random;,value,value,value,value,value

Using sed and assuming we'd like to replace all spaces after the last ; with commas:

$ sed 'h;s/.*;//;y/ /,/;x;s/;[^;]*$//;G;s/\n/;/' file
some name;another thing; random;,value,value,value,value,value

Annotated sed script:

h               ; # Duplicate line into hold space
s/.*;//         ; # Delete up to the last ;
y/ /,/          ; # Change space to comma in remaining data
x               ; # Swap pattern and hold spaces
s/;[^;]*$//     ; # Delete from the last ;
G               ; # Append hold space
s/\n/;/         ; # Remove the embedded newline
                ; # (implicit print)

Using gsub() in awk on the fourth ;-delimited field:

$ awk -F ';' 'BEGIN { OFS=FS } { gsub(" ", ",", $4); print }' file
some name;another thing; random;,value,value,value,value,value

Using sed and assuming we'd like to replace all spaces after the last ; with commas:

$ sed 'h;s/.*;//;y/ /,/;x;s/;[^;]*$//;G;s/\n/;/' file
some name;another thing; random;,value,value,value,value,value

Annotated sed script:

h               ; # Duplicate line into hold space
s/.*;//         ; # Delete up to the last ;
y/ /,/          ; # Change space to comma in remaining data
x               ; # Swap pattern and hold spaces
s/;[^;]*$//     ; # Delete from the last ;
G               ; # Append hold space delimited by newline
s/\n/;/         ; # Replace the embedded newline with ;
                ; # (implicit print)
added 663 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading