Thursday, 10 November 2016

Chopping up strings on the Linux command line




Manipulating strings whether they're an output returned by commands or input by the user or just echoed by a command line is an important aspect of shell scripting.
In this article, I'll share some methods I've learned over time to extract characters from strings to suit my needs.

So, now let's chop some stuff up!

Method 1: Using cut

Cut is a really cool tool when it comes to working with files & strings alike.

Let's say I have a situation in which the output of hostname command is returning the FQDN but I only want the hostname to input somewhere in my script.

Here's how we can cop that up with cut using . as a delimiter :

[root@cserver ~]# echo `hostname`.example.com | cut -d. -f1
cserver
[root@cserver ~]#

The above is an example of extracting a field (column) from a string using a delimiter. Now, let's extract some characters without using a delimiter:

[root@cserver ~]# ifconfig eno16777736 | awk '!/inet6/ && /inet/ {print $2}'
192.168.44.100
[root@cserver ~]# ifconfig eno16777736 | awk '!/inet6/ && /inet/ {print $2}' | cut -c1-11
192.168.44.
[root@cserver ~]# echo `ifconfig eno16777736 | awk '!/inet6/ && /inet/ {print $2}' | cut -c1-11`x
192.168.44.x
[root@cserver ~]#

This takes the awk formatted output from ifconfig & cuts characters from positions 1 to 11 & prints them to stdout.


Method 2: Using expr

expr is a useful bash built in that can perform arithmetic operations in addition to string extractions, comparisons & more.

The expr command shown below with the substr subcommand will take the value of variable a as input string & print 3 characters starting from position 11.

[root@cserver ~]# a=testserver007; expr substr $a 11 3
007

[root@cserver ~]# hostname
cserver

This will print the 1st character of the output of hostname command.

[root@cserver ~]# expr substr `hostname` 1 1
c
[root@cserver ~]#

We can also calculate the length of string using expr:

[root@cserver ~]# expr length `hostname`
7


Method 3: Using bash parameter substitution

We can use the % sign followed by a pattern that we'd like to omit from the input variable.
Using %? will cut the last character from an input string.

[root@cserver ~]# s="sahil suri" ; echo "${s%suri}"
sahil
[root@cserver ~]#
[root@cserver ~]# hostname
cserver

This will take the value of variable var1, extract 3 characters after position 2 & will assign the result to variable var2.

[root@cserver ~]# var1=$(hostname); var2=${var1:2:3} ; echo $var2
erv
[root@cserver ~]#


Method 4: Using awk

AWK has a built in substr function which can be used to extract some part of a string. It's usage is substr (filed,offset,length). In this the field is the column number from the file/STDIN, the offset is the position from which we want to begin extraction & length is the number of characters that we'd like to extract. Here is an example:

[root@cserver ~]# echo "sahil" | awk '{print substr ($1,1,2)}'
sa

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...