Need help to delete special characters exists only at the end of the each record in UNIX file?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Need help to delete special characters exists only at the end of the each record in UNIX file?
# 1  
Old 01-21-2019
Need help to delete special characters exists only at the end of the each record in UNIX file?

Hi,

I have a file in unix with 15 columns.It consists special characters(#,$,^M,@,*,% etc)at the end of the each record.I want to remove these special characters.I used the following:
Code:
Sed -e 's[^a-zA-Z|0-9]/ /g;s/  */ /g'

. But It is removing special characters exists everywhere in the file(begining,middle and end of the file). But I want to remove the special characters appearing only at the end of the each record. I do not want to delete the special characters which are present in the begining or middle of the record. The Unix operating system is AIX.Please do the needful.
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.


Thanks
Rakesh

Last edited by Don Cragun; 01-21-2019 at 02:10 PM..
# 2  
Old 01-21-2019
Hi, rakeshp
Try this. Will only delete the last one
Code:
sed 's/[^\w]$//'

or. Will delete range of last characters
Code:
sed 's/[^\w]\+$//'


Last edited by nezabudka; 01-21-2019 at 02:15 PM..
This User Gave Thanks to nezabudka For This Post:
# 3  
Old 01-21-2019
Quote:
Originally Posted by rakeshp
Hi,

I have a file in unix with 15 columns.It consists special characters(#,$,^M,@,*,% etc)at the end of the each record.I want to remove these special characters.I used the following:
Code:
Sed -e ‘s[^a-zA-Z|0-9]/ /g;s/  */ /g'

. But It is removing special characters exists everywhere in the file(begining,middle and end of the file). But I want to remove the special characters appearing only at the end of the each record. I do not want to delete the special characters which are present in the begining or middle of the record. The Unix operating system is AIX.Please do the needful.
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.


Thanks
Rakesh
Hi Rakesh,
Please note that UNIX shells and utilities are very picky about spelling, capitalization, and quoting. The utility named sed will not be found if you spell it with a leading capital letter (as in Sed as shown in your sample code above) and the single-quote characters surrounding the last argument to sed must be the single-quote character that is found as the unshifted character on the key that has the double-quote character as the shifted character on a US keyboard. The pretty-printed characters in the Sed command you showed us above will cause a syntax error and a mismatched quotes error.

Also note that ^M is two characters (one that is special in some, but not all, circumstances and one that is seldom special). Some utilities will print control characters as a two-character sequence (with ^M representing the <carriage-return> character), but you can't search for control characters using these two-character sequences in a regular expression.

Also note that your specification is ambiguous. Are you trying to get rid of a single special character at the end of a line or are you trying to get rid of all adjacent special characters at the end of a line? Please give us a more precise definition of what you're trying to do and show us a small, representative sample input file (in CODE tags) that shows us what the input files you will be processing look like and show us a corresponding sample output file (in CODE tags) that shows us the output you hope to produce. Producing a list of special characters ending with "etc." leaves us guessing at what you mean by special. Is your definition of special anything that is not an alphanumeric character in your current locale? If it is, please confirm that this is what you mean. If not, please provide a definitive definition of the term special character that you are using.

Since you're converting all non-alphanumeric characters to <space>s, you seem to be indicating that <space> is to be treated as a special character. Does that mean that a single <space> or all <space>s at the end of a line should be deleted?

Your code seems to be trying to change all occurrences of two or more adjacent <space> characters to a single <space> in your output. You didn't say anything about doing that in the description of what you're trying to do. Is this a requirement or just a side effect of the way you're converting non-alphanumeric characters to <space> as an intermediate step in your algorithm?

Quote:
Originally Posted by nezabudka
Hi, rakeshp
Try this
Code:
sed 's/[^\w]$//'

or
Code:
sed 's/[^\w]\+$//'

Hi nezabudka,
Note that rakeshp is using AIX. The GNU sed \w escape sequence to match a "word" in a BRE is not supported by AIX (and is not allowed by the POSIX standards for any version of sed).
These 2 Users Gave Thanks to Don Cragun For This Post:
# 4  
Old 01-22-2019
Hi nezabudka,

Thank you for the reply. sed 's/[^\w]$//'. This code is working good so far in AIX. Can we use the same code in Linux as well?

Thanks
Rakesh

--- Post updated at 02:05 PM ---

Hi Don Cragun,

Thank you for the reply and corrections in the code. I am trying to get rid of single special character and all adjacent special characters at the end of a line. I am not trying to convert any non-alphanumeric characters to <space>.I am only trying to get rid of single special/adjacent special characters appearing at the end of the each record only.

Thanks
Rakesh
# 5  
Old 01-22-2019
You weren't too far off in your own attempt. Try, after correcting the already commented on syntax errors and anchoring the regex at the line end,



Code:
sed 's/[^a-zA-Z|0-9]$/ /; s/  */ /g' file


Last edited by RudiC; 01-22-2019 at 05:25 PM.. Reason: Corrected error - thanks, Don Cragun.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 01-22-2019
I'm surprised that \w is working for you in AIX, but I'm glad that nezabudka's suggestion is working for you. (Even if \w is recognized as special in a BRE, I would not have expected it to treat the vertical bar symbol (|) as a regular character. Note that the BRE in the substitute command you were using (after inserting the missing slash character /) was:
Code:
s/[^a-zA-Z|0-9]/ /g

would match any character that was not lowercase alphabetic, was not uppercase alphabetic, was not numeric, and was not a vertical bar character and change each of the matched characters to a <space>. So, I assumed you want the vertical bar character to be treated as a normal character. Is \w really treating the vertical bar character the way you want it to?)

Something that should work with any standard sed (assuming that your definition of a special character is anything that is not an alphanumeric character and is not a vertical bar symbol) would be:
Code:
sed 's/[^[:alnum:]|]*$//'


Last edited by Don Cragun; 01-22-2019 at 03:29 PM.. Reason: Fix typo: "s/[" was incorrectly written as "/s["
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 01-22-2019
Quote:
Originally Posted by RudiC
You weren't too far off in your own attempt. Try, after correcting the already commented on syntax errors and anchoring the regex at the line end,



Code:
sed 's[^a-zA-Z|0-9]$/ /g; s/  */ /g' file

Hi RudiC,
I'm afraid that still won't work:
  1. There is a <slash> character missing after the s in the substitute command.
  2. That BRE will only match one "special" character at the end of a line.
  3. And, it still coalesces sequences of multiple <space>s not found at the end of a line to a single <space>.
Point #1 above leads to a syntax error. The other two points don't meet the (still poorly specified) requirements for this problem.

Staying closer to what was given originally, we could try:
Code:
sed 's/[^a-zA-Z|0-9]*$/ /g; s/  *$//' file

but that just uses two substitutions to do what I think I did in post #6 with a single substitution.

You're usually better with sed than I am. Am I missing something here? Smilie

~ Don
These 2 Users Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete special characters

My sed is not working on deleting the entire special characters and leaving what is necessary.grep connections_per a|sed -e 's/\<\!\-\-//g' INPUT: <!-- <connections_per_instance>1</connections_per_instance> --> <method>HALF</method> <!--... (10 Replies)
Discussion started by: kenshinhimura
10 Replies

2. Shell Programming and Scripting

UNIX Special Characters

Any time I do : ls *.txt > mytext.txt I get something like this in the output file: ^ Tue Jan 22 16:19:19 EST 2013 x86_64 x86_64 x86_64 GNU/Linux t1Fam_BrOv :~>alias | grep ls alias l.='ls -d .* --color=tty' alias lR='ls -R' alias la='ls -Al' alias lc='ls -ltcr' alias ldd='ls -ltr |... (5 Replies)
Discussion started by: genehunter
5 Replies

3. Shell Programming and Scripting

How to add trailer record at the end of the flat file in the unix ksh shell scripting?

Hi, How to add trailer record at the end of the flat file in the unix ksh shell scripting can you please let me know the procedure Regards Srikanth (3 Replies)
Discussion started by: srikanth_sagi
3 Replies

4. Shell Programming and Scripting

Need unix commands to delete records from one file if the same record present in another file...

Need unix commands to delete records from one file if the same record present in another file... just like join ... if the record present in both files.. delete from first file or delete the particular record and write the unmatched records to new file.. tried with grep and while... (6 Replies)
Discussion started by: msathees
6 Replies

5. Shell Programming and Scripting

Windows to UNIX FTP Special characters!

I have a file that has the name in one of the lines as MARíA MENDOZA in Windows. When this gets FTPed over to UNIX it appears as MAR�A MENDOZA. Is there anyway to overcome this? Its causing a issue because the file is Postional and fields are getting pushed by 2 digits.. Any help would be... (4 Replies)
Discussion started by: venky338
4 Replies

6. Shell Programming and Scripting

how to delete special characters from the file content

Hello Team, Any one suggest how to delte the below special character from a file which is having one column 10 rows of same below content. ---------------------------------------- Kosten|bersicht gemd_ ' =Welche Kosten kvnnen... (2 Replies)
Discussion started by: kanakaraju
2 Replies

7. Shell Programming and Scripting

sed delete pattern with special characters

Hi all, I have the following lines <b>A gtwrhwrthwr text hghthwrhtwrtw </b><font color='#06C'>; text text (text) <b>B gtwrhwrthwr text hghthwrhtwrtw </b><font color='#06C'>; text text (text) <b>J gtwrhwrthwr text hghthwrhtwrtw </b><font color='#06C'>; text text (text) and I would like to... (5 Replies)
Discussion started by: stinkefisch
5 Replies

8. UNIX for Dummies Questions & Answers

Advice on extracting special characters from a DB2 table to a file in the UNIX ENV

need some advice on the following situation. I have a DB2 table which has a varchar Column. This varchar column can have special characters like ©, ®, ™ . When I extract from this table to a sequential file for this varchar column I am only able to get © and ® . To Get the ™... (1 Reply)
Discussion started by: cosec
1 Replies

9. UNIX for Dummies Questions & Answers

How to delete a file with special characters

I don't now exactly how I did it, but I created a file named " -C " cexdi:/home1 $ls -lt total 1801336 -rw------- 1 cexdi ced-group 922275840 23 mars 10:03 -C How do I delete this file ? cexdi:/home1 $rm -C rm: invalid option -- C Syntax : rm filename ... Doesn't work...... (5 Replies)
Discussion started by: yveslagace
5 Replies

10. UNIX for Dummies Questions & Answers

Unix file does not display special characters

We have a unix file that contains special characters (ie. Ñ, °, É, ¿ , £ , ø ). When I try to read this file I get a codepage error and the characters are replaced by the # symbol. How do I keep the special characters from being read? Thanks. Ryan (3 Replies)
Discussion started by: Ryan2786
3 Replies
Login or Register to Ask a Question