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!

Remove character at EOL 1

Status
Not open for further replies.

StevePB

Technical User
Dec 6, 2001
92
GB
I need to use a script (/usr/bin/ksh) to remove certain characters from a data file. The format is plain text. I want to remove the last character on a line only if it is the one specified in a parameter to the script. The specified character often needs to be the pipe character. I find I can manually write the 'sed' command line and it works (with the pipe escaped), but I can't figure out how to make it work as a parameter. The command line I am using is:
[tt]sed 's/\|$//' testin.txt > testout.txt[/tt]
and in the script I assign the first parameter to be the character to be removed and then:
[tt]sed 's/$DELIM$//' $TESTIN > $TESTOUT[/tt]
I have tried escaping the pipe on input (not ideal), quoting the pipe on input, quoting the variable, but nothing seems to work.
Any help will be much appreaciated.
-SB.
 
Hi

$DELIM is [tt]ksh[/tt]'s variable, so [tt]ksh[/tt] has to expand its value. But no such expansion is performed on strings delimited with single quotes ( ' ).
Code:
sed [COLOR=red pink]"[/color]s/$DELIM[COLOR=red pink]\[/color]$//[COLOR=red pink]"[/color] $TESTIN > $TESTOUT

[gray]# or[/gray]

sed 's/[COLOR=red pink]'"[/color]$DELIM[COLOR=red pink]"'[/color]$//' $TESTIN > $TESTOUT
By the way, you only have to escape pipe ( | ) if you use GNU [tt]sed[/tt] with -r ( --regexp-extended ) parameter.

Feherke.
 
Thanks very much feherke, that works perfectly. I still have to escape the pipe character on input as a parameter, but I guess there's no way around that.

Out of interest, in your first suggestion, why does the dollar need to be escaped after the delimiter variable ?

-SB.
 
Hi

SB said:
I still have to escape the pipe character on input as a parameter, but I guess there's no way around that.
Depends. I always single quote ( ' ) strings where no expansion has to be performed, so the pipe adds nothing new to me :
Code:
#!/bin/ksh

infile="$1"
outfile="$2"
delim="$3"

sed "s/$delim\$//" "$infile" > "$outfile"
Code:
[blue]master #[/blue] ./sb.sh '/path/to/input' '/path/to/output' '|'
SB said:
Out of interest, in your first suggestion, why does the dollar need to be escaped after the delimiter variable ?
Because the entire string is delimited with double quotes ( " ) so the shell will try to perform variable expansion everywhere. That dollar sign ( $ ) needs to be interpreted by [tt]sed[/tt], so you have to tell the shell to not touch it. ( Note that probably your shell will figure out that you would prefer to leave that dollar sign untouched, but this is the correct way. )

Feherke.
 
Thanks again Feherke, you have been a great help.
-SB.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top