User Tools

Site Tools


linux:bash_tips

BASH TIPS

VARIABLES

If you want to display $variable followed by a custom string as:
$variable + '_rock'

${variable}_rock

Alternative values for a variable:

echo ${variable:-alternative value} # displays the alternative value
echo ${variable:=alternative value} # displays the alternative value and set the variable to alternative value
echo ${variable:+alternative value} # displays the alternative value only if the variable is set
Note: 'alternative value' accepts spaces.

Slicing:

variable="A very interesting string"
echo ${variable:3:1} # displays 1 chars starting from position 3 (0 is the first) = e
echo ${variable:3:5} # displays 5 chars starting from position 3 (0 is the first) = ery i
echo ${variable:3} # displays all the string starting from 3 = ery interesting string

Multiple variables access
If you define more variables which start with the same name, you can access them all at once:

fruit=banana
fruit1=orange
fruit2=lemon
echo ${!fruit*} # displays all variable names who start with fruit -> fruit fruit1 fruit2

Access their values in a loop:

for fr in ${!fruit*}; do
   echo $fr = ${!fr}
done

will display:
fruit = banana
fruit1 = orange
fruit2 = lemon

USING TR SORT and UNIQ

tr stands for translate (or delete) characters. Here is the description: Translate, squeeze, and/or delete characters from standard input, writing to standard output.

Let's suppose that we have a test.txt with the following in it:

This is a test file.

Here is the third line, with some text on it.
HERE IS A CAPITALIZED TEXT.

Last line is here!

  • Replace one character with another (e.g. spaces with points)

$ cat test.txt | tr ' ' '.'
This.is.a.test.file.

Here.is.the.third.line,.with.some.text.on.it.
HERE.IS.A.CAPITALIZED.TEXT.

Last.line.is.here!

  • Put each word on a new line (replace space with /n char)

$ cat test.txt | tr ' ' '\
> '
This
is
a
test
file.

Here
is
...
Note: to enter this command, after the last \ press ENTER and then continue with a quote.

  • Remove the punctuation (?!.)

$ cat test.txt | tr -d '[:punct:]'
This is a test file

Here is the third line with some text on it
HERE IS A CAPITALIZED TEXT

Last line is here

  • Make everything uppercase (or lowercase)

$ cat test.txt | tr '[:lower:]' '[:upper:]'
THIS IS A TEST FILE.

HERE IS THE THIRD LINE, WITH SOME TEXT ON IT.
HERE IS A CAPITALIZED TEXT.

LAST LINE IS HERE!

just switch the upper with lower and you'll get the reverse (lowercase).

  • Sort

$ cat test.txt | tr ' ' '\
' | sort


a
A
CAPITALIZED
file.
here!
Here
HERE
...

  • Uniq = report or omit repeated lines

$ cat test.txt | tr ' ' '\
' | sort | uniq -ci
      2
      2 a
      1 CAPITALIZED
      1 file.
      1 here!
      2 Here
      4 is
      1 it.
...

       -i, --ignore-case \\ 
              ignore differences in case when comparing \\ 
       -c, --count \\ 
              prefix lines by the number of occurrences \\ 

linux/bash_tips.txt · Last modified: 2013/03/16 17:40 (external edit)