Tag: bash

BASH : Delete the last X chars from a string

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

head -c -6

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

 

 

Linux : Sort a list of IP address

like we saw previously in that article , it’s possible to ping an entire /24 subnet with a little convenient command.

64 bytes from 192.168.1.21: icmp_seq=1 ttl=64 time=0.163 ms
64 bytes from 192.168.1.31: icmp_seq=1 ttl=64 time=0.360 ms
64 bytes from 192.168.1.8: icmp_seq=1 ttl=128 time=66.8 ms
64 bytes from 192.168.1.32: icmp_seq=1 ttl=64 time=25.1 ms
64 bytes from 192.168.1.42: icmp_seq=1 ttl=255 time=1.93 ms
64 bytes from 192.168.1.46: icmp_seq=1 ttl=255 time=60.8 ms
64 bytes from 192.168.1.41: icmp_seq=1 ttl=255 time=67.0 ms
64 bytes from 192.168.1.254: icmp_seq=1 ttl=64 time=0.344 ms

 

The Problem is , the IP adresse are going to be displayed in the order they answer the ping , and some devices are going to answer faster than other , making a list that is not in order

the sort command in her default setting will sort using the first changing character , which is going to give me a list like that : 21, 254, 31, 32, 41, 42, 46, 8

If you have installed on you host a recent version of the sort command , you will be able to use sort -V
but on older and some embedded devices the -V option is not available ,

You can then launch the sort command with these options :

sort -n -t . -k 4,4

the result will be :

64 bytes from 192.168.1.8: icmp_seq=1 ttl=128 time=0.978 ms
64 bytes from 192.168.1.21: icmp_seq=1 ttl=64 time=0.246 ms
64 bytes from 192.168.1.31: icmp_seq=1 ttl=64 time=0.383 ms
64 bytes from 192.168.1.41: icmp_seq=1 ttl=255 time=76.5 ms
64 bytes from 192.168.1.42: icmp_seq=1 ttl=255 time=2.47 ms
64 bytes from 192.168.1.46: icmp_seq=1 ttl=255 time=65.4 ms
64 bytes from 192.168.1.254: icmp_seq=1 ttl=64 time=0.290 ms

 

if you need to sort a /16 list of ip address :

you can just add -k 3,3 to the command : sort -n -t .-k 3,3 -k 4,4

bash : Put the source of a webpage in a variable

When you do some bash scripting , this is often useful to get data from  webserver because they are a very simple way to exchange data from computer from computer.
to put the data that’s available on a webserver into a $var you just have to use the command :

 var=$(wget -qO- http://server.com/file.htm)

this will fetch the data from http://server.com/file.htm in a non verbose way and put it in variable named var .

you can now do whatever you want with that $var , like greping it for example.

 

Extract values separated by character ( : , . – )

In a bash script if you find yourself with values presented that way

Hello:345:18.5:TRUE:000

you might want to keep only the second or third of this values that are separated by the : character

like always , in bash there is a lot of ways to accomplish that , but i like to use a one that use the AWK utility because it’s very easy to read and understand.

my list of value is contained in a var name $var let’s extract the third value which is 18.5

echo $var | awk -F’:’ ‘{print $3}’

Will return the value 18.5

but i can do some math operation in the utility, like :

echo $var | awk -F’:’ ‘{print $2-$3}’

this will return 326.5 the result of 345 – 18.5

awk is a fast and powerful text editor , there is a lot things you can do with it.

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 -e "s/word to delete//g"

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 :

echo $visitor | sed -e "s/current number of users: //g"

the result is going to be : 234

 

Grep a line before or after a match in bash

Sometime when you grep a file on Linux you need to not only display the line where the result is found but also several lines before or after the match.

Grep as a useful function permitting you to do just that.

Let’s take an example file.

file.txt:

Title : login issue
Ticket number : 16417
Details : user password is no longer working.

Title : Broken keyboard
Ticket number : 16418
Details : the space key on the user keyboard no longer work

 

If the file is list of hundreds of this informations and you need to see only the information related to ticket 16417 you need to

cat file.txt|grep 16417

Will only return the result Ticket number : 16417  living out the useful informations

If you need to display a line after the grep match

cat file.txt| grep -A1 16417

will return :

Ticket number : 16417
Details : user password is no longer working.

But there is also useful information before  the match

You can associate the 2 options to display the line after and before

cat file.txt| grep -A1 -B1 16417

this will return all the information i need :

Title : login issue
Ticket number : 16417
Details : user password is no longer working.