Mars
Mars

Reputation: 173

sed command to add text for only last line in unix

I want sed command which will add comma to only last line e.g i have text file which contains

"872709"
"872700"
"145"
"872808B"
"8729029921"
"879B"
"87290"
"AirHo9"
"Ait22"
"DVDSept22"
"Gr929"

want to add comma at last line

"872709"
"872700"
"145"
"872808B"
"8729029921"
"879B"
"87290"
"AirHo9"
"Ait22"
"DVDSept22"
"Gr929",

Upvotes: 2

Views: 7124

Answers (3)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

No need to use sed:

echo -n ',' >> file 

Upvotes: 1

nu11p01n73R
nu11p01n73R

Reputation: 26667

sed -r '$ s/([a-zA-Z0-9"]*)/\1,/' inputfile

The $ mathces the last line in the file

([a-zA-Z0-9"]* matches charactes numbers or " and the matched patter is saved in \1 back reference.

\1, is the mathced pattern + commaa wich is the substituion pattern

test

"872709"
"872700"
"145"
"872808B"
"8729029921"
"879B"
"87290"
"AirHo9"
"Ait22"
"DVDSept22"
"Gr929",

Upvotes: 1

anubhava
anubhava

Reputation: 785058

You can use sed:

sed '$s/$/,/' file
"872709"
"872700"
"145"
"872808B"
"8729029921"
"879B"
"87290"
"AirHo9"
"Ait22"
"DVDSept22"
"Gr929",

To make it save changes inline use:

sed -i.bak '$s/$/,/' file

Upvotes: 11

Related Questions