Strip off last character

Strip off last character

Post by Thomas L » Thu, 14 Apr 1994 09:03:37



I have a file that has fields delimited by "|".  At the end of each line,
there is a "|" after the last field that I want to strip off.  Any ideas?


Thanks in advance,

T. Lah

 
 
 

Strip off last character

Post by Ami Fischm » Fri, 15 Apr 1994 01:33:12


: I have a file that has fields delimited by "|".  At the end of each line,
: there is a "|" after the last field that I want to strip off.  Any ideas?

cat filename |sed 's/\|$//p'

--
                                        --Ami
                                          Have YOU hugged YOUR smurf today?
That secret you've been guarding, isn't.

 
 
 

Strip off last character

Post by 288clif.. » Fri, 15 Apr 1994 23:40:34




> : I have a file that has fields delimited by "|".  At the end of each line,
> : there is a "|" after the last field that I want to strip off.  Any ideas?

> cat filename |sed 's/\|$//p'

Close. First, sed reads from stdin by default so the `cat' is a waste. Also,
since you are quoting the entire sed command the `\' single character quote
is not necessary and the `p' should be g for `global'. Try:

sed 's/|$//g' infile > outfile

to read infile and write to outfile.

/d
--

 
 
 

Strip off last character

Post by David W. Tamk » Sat, 16 Apr 1994 11:59:43



: sed 's/|$//g' infile > outfile

The g is pointless there; a right-anchored search string can occur only once
(if at all) per line.  (If the expression's contents appear more than once,
only the last occurrence is right-anchored, so all the earlier ones fail to
match the search string.)

 
 
 

Strip off last character

Post by Jari Aal » Sat, 16 Apr 1994 16:44:08



: I have a file that has fields delimited by "|".  At the end of each line,
: there is a "|" after the last field that I want to strip off.  Any ideas?

Not quite sure what you were after...pick the one you need.
1) If you wan't to strip the last character:

sed 's/\(.*\)|[ ]*$/\1/' file_in
             ^ ^^
             ^  add TAB char if you need it, now there is SPC only.
             ^grab last char

2) if you want to strip the *last* field

sed 's/\(.*\)|.*|[ ]*$/\1/' file_in
             ^^^^
             grab last field

The sed \( \)  stores contents to variable "1" and later \1 puts it back for
output. The 's/rexp/output_string/' cmd --> "s"ubstitutes rexp with
output_string

Cheers!

--

--

        |  -- Andre' Marrou virus --     (Active since June, 1, 1992 GMT +2  )
        | Erases unnecessary programs, disconnects your network and makes
        | yours  a stand-alone machine, improves productivity by eliminating
        | limits on resource allocation.

 
 
 

1. Stripping the last character of a file if it's a ^J (ascii 10)

I need to create a script that will scan a file, and if that last
character of the file is an end-of-line (i.e. ^J or ASCII 10), then it
will remove that character.  I wrote this script, but it doesn't work:

#!/bin/sh
file=`cat $1`
length=`expr length "$file"`
char=`expr substr "$file" $length 1`
ascii=`echo -n $char | od -An -t d1`
if [ $ascii = "10" ]
then
   length=$(($length - 1))
fi
expr substr "$file" 1 $length

I'm not sure what exactly is wrong, but I suspect it's because it's not
treating my file as a binary file.  There's some text-file-conversion
or something going on.  I'm guessing the "$file" on line 3 just doesn't
work.

I don't know perl or sed or awk, which is why I'm asking here for help.
 Thanks in advance.

2. E3500 GBIC's

3. stripping off control characters

4. IO Wait / Stripe & Mirror

5. last character of the last line is a newline?

6. 2.5.14 compile error

7. Removing last character from last line in a file

8. Caching and extra drives (was: IDE CACHE BOARD with Linux??)

9. stripping the last line

10. Q: Stripping -n first or last lines from file

11. strip out last char

12. Telnet to Linux strips 8th bit from incoming characters

13. stripping out con-conformant characters