Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sed or other way to replace last , with \n 1

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL

I can do it with some auxiliary echo command but not with sed directly...

Code:
$ printf "1,2,3,4,"
1,2,3,4,$
$
$ printf "1,2,3,4,"|sed 's/\,$/4/'
1,2,3,44$
$
$ printf "1,2,3,4,"|sed 's/\,$/\n/'
1,2,3,4[COLOR=#EF2929]n[/color]$
$
$ printf "1,2,3,4,"|sed 's/\,$//';echo
1,2,3,4
$ { printf "1,2,3,4,"|sed 's/\,$//';echo; }
1,2,3,4
$
 
I'd try this:
Code:
printf '1,2,3,4,' | awk '{sub(/,$/,"");print}'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
You can't remove that '$' because it's not part of the output. It's your command line prompt. The "printf" command does not add a new line at the end of the output, so your command line prompt is appearing at the end of the output string.

Try this...

Code:
# Using printf with a newline character (\n)
printf "1,2,3,4,\n"

# Using print (which adds its own newline)
print "1,2,3,4,"

Also...

Code:
# This makes it a bit more obvious
PS1=Command:

 
Oh, I think I misunderstood the question.

Try this...

Code:
printf "1,2,3,4,\n" | sed 's/,$//g'

# or

print "1,2,3,4," | sed 's/,$//g'

Better?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top