LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-11-2018, 10:09 AM   #1
gglq000
Member
 
Registered: Mar 2012
Posts: 65

Rep: Reputation: Disabled
removing garbage char from string or files


i extract a number from a text and tried using
bc -l << $num1/$num2 which kept giving error.

i found one of the number has contained invisible garbage char.
i only found about by
echo $num1 > file.txt
and
hexdump -C file.txt

which prints out ascii code of each byte in the text file.
i can see it contains char like 1b (escape), 6d (letter m), 5d (bracket) where visible char only contains digits and dot for numeric calculation.

I have tried myriad of methods with cut, tr with non-printable chars like [:space:], [:cntrl:] but does not work.

I think I need to figure out how to use sed in this case but by specifying the ascii code.
 
Old 02-11-2018, 10:16 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,849

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
probably:
Code:
sed 's/[^0-9.]//g'
 
Old 02-11-2018, 05:32 PM   #3
BudiKusasi
Member
 
Registered: Apr 2017
Distribution: Artix
Posts: 345

Rep: Reputation: 15
invisible char. Ascii usually in range of 1F and less than it and 7f and more, it depends on your precision, so removal of them is

Code:
sed -E 's/[0-1][0-9a-f]|[7-9a-f][0-9a-f]//gI' file.txt

Last edited by BudiKusasi; 02-11-2018 at 09:50 PM.
 
1 members found this post helpful.
Old 02-11-2018, 09:27 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Code:
fooball="$(echo $filename | sed 's/[^a-zA-Z0-9]//g')"
keeps a - z A - Z 0 - 9 gets rid of the rest.
 
Old 02-11-2018, 10:09 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
sed with [[:cntrl:]] would be my preferred - simple and elegant.
 
Old 02-13-2018, 02:38 PM   #6
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,963

Rep: Reputation: 271Reputation: 271Reputation: 271
Use tr 'backwards':

Code:
tr -dc '[:alnum:]'
removes every non-alphanumeric character.
 
Old 02-13-2018, 02:49 PM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Code:
tr -d '\0\2\32'
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between char *string and char string[20] savio Programming 7 08-10-2015 01:51 AM
getting garbage when printing array of string dynamically. ashish anand Programming 3 02-12-2012 11:32 PM
SH: Remove last char if it is an empty char in a string Xeratul Programming 2 07-06-2011 07:39 PM
[SOLVED] Why is char* a string and not a pointer to a char? el_b Programming 2 09-25-2009 10:33 AM
get first char of a string? raskol Programming 2 04-05-2008 06:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration