LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-26-2017, 04:37 AM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 143

Rep: Reputation: Disabled
Sed , remove every number from user input from variable not in sequence


Hi , i am trying to use sed to search from user input into a fixed variable and remove every number from user input no matter if it is in order or not , but it is not working .

From this next example if i write "9" or 840" then since those sequences of numbers are in main variable , then sed will find them and it will remove them .

Bu what i need is to input ex: 890 , and wait to strip all the variable and search for those numbers individually and remove them if they exist .


Code:
#!/bin/bash

#some default variable
var="2840139"

#user input
echo -n "From  $var which numbers do you want to remove? : "
read -r out

#the output expected is to sed search in (var) from user input (out) 
# and remove the all digits from the main variable even if they are 
# not in squence
output=$(echo $var | sed -r "s#$out##g")

echo ""
echo "This is the left number $output"
Does anyone knows how to achieve this ?
The problem here is that sed is looking the all the user input at once and only removes the whole sequence as it is in case it finds it , but i need sed to remove all those digits from user input from main variable .

Last edited by pedropt; 12-29-2017 at 02:11 AM.
 
Old 12-26-2017, 04:48 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 16,653

Rep: Reputation: 2453Reputation: 2453Reputation: 2453Reputation: 2453Reputation: 2453Reputation: 2453Reputation: 2453Reputation: 2453Reputation: 2453Reputation: 2453Reputation: 2453
You have had how to strip a variable answered in another thread. Can you not apply that for yourself ?.
 
Old 12-26-2017, 05:00 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 10,898

Rep: Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241
you can specify list of chars in between [ and ] - for sed.
 
Old 12-26-2017, 05:12 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,484

Rep: Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071
Or you could quote the complete text of this homework of yours.
 
Old 12-26-2017, 05:26 AM   #5
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 143

Original Poster
Rep: Reputation: Disabled
Quote:
You have had how to strip a variable answered in another thread. Can you not apply that for yourself ?.
Yup , you are right , the situation is that the logic of this script that will work with some hardware is making my head blow with the logic that must run .

It is so many code , that i get lost on its working logic if i distract just for a second .

However , thank you for pointing that up , i managed a way to get this done .

Code:
#!/bin/bash

var="2840139"

echo -n "From  $var which numbers do you want to remove? : "
read -r out

for i in $(seq ${#out}); do
a1=$(echo "${out:$i-1:1}") 
output=$(echo $var | sed -r "s#$a1##g")
var="$output"
done

echo ""
echo "This is the left number $output"
 
Old 12-26-2017, 05:54 AM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,158

Rep: Reputation: 698Reputation: 698Reputation: 698Reputation: 698Reputation: 698Reputation: 698
Or without a loop and sed
Code:
${string//substring/replacement}
    Replace all matches of $substring with $replacement.
http://tldp.org/LDP/abs/html/string-manipulation.html

Code:
output=${var//[$out]/}

Last edited by keefaz; 12-26-2017 at 05:56 AM.
 
Old 12-26-2017, 09:34 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 10,898

Rep: Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241
the minimal modification (to the OP) would be:
Code:
...
output=$(echo $var | sed -r "s#[$out]##g")
...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] a problem with sed when a variable has '/' character. corone Programming 6 04-10-2012 09:48 AM
Remove first character with sed and grep r_jr Programming 11 02-04-2012 03:37 AM
[SOLVED] How sed can catch variable from input drele Programming 5 02-03-2011 12:50 AM
[SOLVED] Remove the Last character from Variable NetRock Programming 13 06-23-2010 12:07 AM
[SOLVED] How to remove a certain character from a variable linuxromeo Linux - Newbie 5 06-15-2010 08:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:11 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration