Tag: sed

bash : Random number in OpenWRT

On some light linux environement running busybox or openWRT you will notice that you don’t have access to the $RANDOM variable ,
the date command is also incomplete so you can’t use the tail of the nanosecond date to have a random number.

.

Using this command you will get a random number beetween 0 and 69.
if you wish to change the range of the random numbers , just edit the regex in the grep part

BASH : Delete the last X chars from a string

sed delete last x chars

When you scrap data in HTML , it’s common , depending on the quality of you regex to end up with a string with some useless character at the end (Or the Beginning),

If you string looks like that :

My useful data</a> <

You can use the command head to drop the X last character of your string ,
So in my Case , to Keep only the string My useful data and drop the </a> <

I’m going to pipe my string to

This will delete the last 6 chars of my string , giving the data that i need later in my script

but in some implementation of the head command this will not work ,

you can use an alternative with sed

This will delete the last 4 characters of your line ,  or alternatively

 

Using sed to delete a word from a string

For example in a bash script you need to delete one or more word from the string , you can use sed to do just that

sed is searching for “word to delete” and replacing it with nothing ,

you can of course pipe to that command ,

example :

the var $visitor contain the string : ‘current number of users: 234’

But only the number (234) is usefull to you ,

your can do :

the result is going to be : 234