LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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-28-2017, 01:55 PM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 133

Rep: Reputation: Disabled
Replace a variable text with *


more simple as it is , is impossible .

This one can be tested over the terminal without a script .

Quote:
echo 1234 | sed "s/1234/*/g"
The problem i have is that i can change the text only to 1 * , and i want sed to replace the number of character in the search with the same number of * .

The most easy answer from anyone to this would be :

CODE]
echo 1234 | sed "s/1234/****/g"
[/CODE]

But the example i am writing here is not what i will use in the code , because in my code that "1234" i a variable that does not have a fixed size of characters .

for a script for test it could be used this one with user input :

Code:
#!/bin/bash
echo -n "Write anything to be turned to * : "
read var
out=$(echo $var | sed "s/$var/*/g")
echo ""
echo "Your input $var was converted to $out"

Does anyone knows this one ?

Last edited by pedropt; 12-29-2017 at 03:10 AM.
 
Old 12-28-2017, 02:07 PM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 2,810
Blog Entries: 3

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Think about replacing the numbers from a set [ ] like in the other question.
 
Old 12-28-2017, 02:12 PM   #3
GazL
Senior Member
 
Registered: May 2008
Posts: 4,592
Blog Entries: 10

Rep: Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090Reputation: 2090
How about:
Code:
tr '[:print:]' '*'
 
1 members found this post helpful.
Old 12-28-2017, 02:14 PM   #4
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 560

Rep: Reputation: 260Reputation: 260Reputation: 260
Code:
echo 1234 | sed 's/./*/g'
 
1 members found this post helpful.
Old 12-28-2017, 02:17 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,636

Rep: Reputation: 521Reputation: 521Reputation: 521Reputation: 521Reputation: 521Reputation: 521
Quote:
Originally Posted by pedropt View Post
Does anyone knows this one ?
Once again you posted a question, gave a sample input (only one) and no sample output. If you expect LQ people to help you, you must put in the effort to compose a good question.

Daniel B. Martin

.
 
2 members found this post helpful.
Old 12-28-2017, 02:18 PM   #6
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 133

Original Poster
Rep: Reputation: Disabled
You guys are fast , thanks a lot for the tip .

Final code :

Code:
#!/bin/bash
echo -n "Write anything to be turned to * : "
read var
out=$(echo $var | sed "s/./*/g")
echo ""
echo "Your input $var was converted to $out"
 
Old 12-28-2017, 02:32 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,362

Rep: Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024
Hopefully $var won't be something like this: beware; rm -rf /

Edit: possible fix:
Code:
out=$(printf -- '%s' "$var" | sed "s/./*/g")

Last edited by NevemTeve; 12-29-2017 at 12:04 AM.
 
2 members found this post helpful.
Old 12-28-2017, 03:17 PM   #8
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 133

Original Poster
Rep: Reputation: Disabled
Quote:
Hopefully $var won't be something like this: beware; rm -rf /
eheheheh , remove everything from the root by force , lol .

I am developing a quick script to work with velleman k8055 input/output card
https://www.velleman.eu/products/view/?id=351346

this sed code i asked is specifically to not show the password on the machine running the script .

https://s14.postimg.org/stwq2bbfl/keypad.jpg

This keypad script will work with k8055 input ports , and depending on the code inputed by user on a specific keypad connected to that card input ports can open a door or whatever user reminds him to do with it .

This will for github when finished .
 
Old 12-30-2017, 10:23 AM   #9
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 631

Rep: Reputation: 296Reputation: 296Reputation: 296
With a bash builtin
Code:
out=${var//?/*}
 
  


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] Replace text lines identified by leading text in line within multiple files juergen852 Linux - Newbie 9 09-21-2014 05:54 PM
[SOLVED] How to replace text using sed or awk in a file without changing the commented text coolpraz Programming 4 01-03-2013 07:19 AM
bash script to create text in a file or replace value of text if already exists knightto Linux - Newbie 5 09-11-2008 12:13 AM
Replace text of unknown content with other text in file brian0918 Programming 15 07-14-2005 10:22 PM
Replace text of unknown content with other text in file brian0918 Linux - Software 1 07-14-2005 04:22 PM

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

All times are GMT -5. The time now is 01:38 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