Reputation: 63
I have a file that contains a value on each line and it ends with a ,
. I want to remove the ,
in the last line of selected address range. I am able to achieve the following with sed -n '1,3p' file | sed '$s/,$//'
, but is there a simpler way?
Example:
'12345',
'45322',
'90456',
'67895',
...
'34552',
Expected output:
'12345',
'45322',
'90456'
Upvotes: 1
Views: 59
Reputation: 88766
With GNU sed:
sed -n '1,3{ 3s/,$//;p }' file
Output:
'12345', '45322', '90456'
Disadvantage: 3
must be entered twice.
Upvotes: 2
Reputation: 52529
Just run both commands in one sed
invocation, the one to remove the comma first:
$ cat input.txt
foo,
bar,
baz,
quux,
$ sed -n '3s/,$//; 1,3p; 3q' input.txt
foo,
bar,
baz
Note using the same line number for the s///
as for the end of the range to print instead of $
.
As an optimization, this also exits after printing the last line of the range instead of continuing to process the rest of the file (And do nothing with it).
Upvotes: 1