Removing "'" from a file

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Removing "'" from a file
# 1  
Old 11-06-2013
Removing "'" from a file

Hi I have a file in which the last characters are ' and space, i.e


Here in the above code, 1st line is having the comma as the last character and in the 2nd line space is the last character, I want to delete these character if it is there.
Please guide me in achieving the result.

Expected Result:

Thanks in advance. Smilie

Last edited by Scrutinizer; 11-06-2013 at 11:22 AM.. Reason: code tags
# 2  
Old 11-06-2013
Please use code tags as required by forum rules!

Your second sample line does NOT have a space as the last character, although this may be due to the missing tags. Anyhow, sth. like sed 's/[\' ]$//' file might work.

Last edited by RudiC; 11-06-2013 at 03:42 PM..
# 3  
Old 11-06-2013
You could try something like:-
sed "s/[\' ]$//" file

Note the use of double quotes because you are looking for a single quote. The expression being searched for is:-

[\' ]   - either a single quote or a space (single quote escaped with a back-slash
$       - end of line.

The bit between the first and second / is the search string, and between the second and third is what it is replaced with, in this case, nothing at all.

I can see the trailing space on line 2 of your input and this works okay for me.

I hope that this helps
# 4  
Old 11-06-2013
If you are unlucky enough to have a corrupted or non-existent sed here is a longhand version using bash builtins only...
#/bin/bash --posix
# Remove "'" DEMO.
echo -n "$yourtext" > /tmp/text
text=$(cat < /tmp/text)
# This line is not needed.
echo "$text"
length=$[ ( ${#text} - 1 ) ]
# This line is not needed.
echo "${#text}"
while [ $subscript -le $length ]
	decimal=`printf "%d" \'${text:$subscript:1}`
	if [ $decimal -eq 39 ]
		subscript=$[ $subscript + 1 ]
		subscript=$[ $subscript + 1 ]
# Print the final string...
echo "$newtext"
# This linbe is not needed...
echo "${#newtext}"
# These two lines added to show the _invisible_ characters are preserved and are not needed.
echo -n "$newtext" > /tmp/text
hexdump -C /tmp/text
# DEMO end.

Results are these with all not needed line left in:-
Last login: Wed Nov  6 19:44:44 on ttys000
AMIGA:barrywalker~> ./
00000000  61 62 63 6e 0a 6a 6b 64  6a 66 20 0a 64 6b 66 6a  |abcn.jkdjf .dkfj|
00000010  64                                                |d|
AMIGA:barrywalker~> _

# 5  
Old 11-06-2013
For fun, a bit different approach:
awk -F"[' ]$" '{print $1}'  file

# 6  
Old 11-06-2013
Originally Posted by rbatte1
[\' ]   - either a single quote or a space (single quote escaped with a back-slash
$       - end of line.

The single quote does not need to be backslash-escaped because it is not a special character in sed. Even if it were, within a bracket expression the backslash is incapable of escaping/quoting anything because in that context it is not a special character.

$ echo "123'\\ 345"
123'\ 345

$ echo "123'\\ 345" | sed "s/[\' ]//g"

The correct interpretation of [\' ] is that it is an expression which matches a single backslash, quote, or space.

For detailed information, consult POSIX Regular Expressions.


Last edited by alister; 11-06-2013 at 05:07 PM..
This User Gave Thanks to alister 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

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Removing duplicates on a single "column" (delimited file)

Hello ! I'm quite new to linux but haven't found a script to do this task, unfortunately my knowledge is quite limited on shellscripts... Could you guys help me removing the duplicate lines of a file, based only on a single "column"? For example: M202034357;01/2008;J30RJ021;Ciclo 01... (4 Replies)
Discussion started by: Rufinofr
4 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

Removing "^M" from the end of a String (i.e. "Ctrl+M")?

Hello All, I have an Expect script that ssh's to a remote server and runs some commands before exiting. One of the commands I run is the "hostname" Command. After I run this command I save the output using this line in the code below... Basically it executes the hostname command, then I... (2 Replies)
Discussion started by: mrm5102
2 Replies

5. Shell Programming and Scripting

Removing comma "," in a field value in csv file

Hi, I have csv file with records as below. Now i have remove any comma in the filed value because that creates problem when i feed this file to an application. for example below are two sample records, the second record have a comma in ", Inc." field, now i have to remove this... (13 Replies)
Discussion started by: anaga
13 Replies

6. Shell Programming and Scripting

Removing "Hidden Characters" on a file

Hi - I'm having a problem with hidden characters on Linux. When I produced an output from Oracle database, there is a an extra "Hidden Character" included on the output. How can I remove that character? See below: The extra dollar sign is creating a new line on my .csv output file. I... (16 Replies)
Discussion started by: Jin_
16 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

Removing special characeter "~V" in a unix file

I have the Unix XML file as below: <?xml version="1.0" encoding="UTF-8"?> <ReportData version="1.0"><DisplayName>Non-Agency CMO Daily Trade Recap - Hybrids</DisplayName><ReportType>MgmtTradingReport</ReportType><Description>Management Trading... (7 Replies)
Discussion started by: mohsin.quazi
7 Replies

9. Solaris

removing "/" file system from solaris volume

Hi all, I have created a volume for the root device as d0 and the sub mirror for same is d10. the output from metastat d0 is as below I want to clear these volume , as i cant unmount the "/ " file system , please suggest as how can i clear this. Also the required entries are there... (2 Replies)
Discussion started by: kumarmani
2 Replies

10. Shell Programming and Scripting

removing the "\" and "\n" character using sed or tr

Hi All, I'm trying to write a ksh script to parse a file. When the "\" character is encountered, it should be removed and the next line should be concatenated with the current line. For example... this is a test line #1\ should be concatenated with line #2\ and line number 3 when this... (3 Replies)
Discussion started by: newbie_coder
3 Replies
Login or Register to Ask a Question