LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 12:55 PM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

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 02:10 AM.
 
Old 12-28-2017, 01:07 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,294
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
Think about replacing the numbers from a set [ ] like in the other question.
 
Old 12-28-2017, 01:12 PM   #3
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

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

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

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
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, 01:18 PM   #6
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

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, 01:32 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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-28-2017 at 11:04 PM.
 
2 members found this post helpful.
Old 12-28-2017, 02:17 PM   #8
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

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, 09:23 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

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


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

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

All times are GMT -5. The time now is 11:35 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
Open Source Consulting | Domain Registration