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
Thanks in advance,
T. Lah
cat filename |sed 's/\|$//p'
--
--Ami
Have YOU hugged YOUR smurf today?
That secret you've been guarding, isn't.
> cat filename |sed 's/\|$//p'
sed 's/|$//g' infile > outfile
to read infile and write to outfile.
/d
--
: 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.)
: 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
5. last character of the last line is a newline?
7. Removing last character from last line in a file
8. Caching and extra drives (was: IDE CACHE BOARD with Linux??)
10. Q: Stripping -n first or last lines from file
12. Telnet to Linux strips 8th bit from incoming characters
13. stripping out con-conformant characters