Avatar
Hund

# emerge life

A collection of handy ways of manipulating text in Bash

Here’s my collection with handy ways of manipulating text in Bash. I have tested all of these commands with GNU Bash 4.4.23 (or newer). If you have any feedback or suggestions feel free to send them to me via any of the listed ways in the footer or by creating a issue on GitLab.

I have also added a version history to make it easier to keep track of the changes to this post.

Version history

Update 1 - 2019-02-10

  • Initial commit.

Adding words

Add a word to the beginning of a string

$ echo "is an example" | sed 's/^/this /'
this is an example

Add a word to the end of a string

$ echo "this is an" | sed 's/$/ example/'
this is an example

Add a word on a new line after a match

$ echo -e "1\n3" | sed '/1/a 2'
1
2
3

Add a word on a new line before a match

$ echo -e "1\n3" | sed '/3/i 2'
1
2
3

Printing words

$ echo "one two three" | awk '{print $3}'
three
$ echo "one two three" | awk '{print $1$3}'
one three

You can add some spacing between the words like this:

$ echo "one two three" | awk '{print $1" "$3}'
one three
$ echo -e "one\ntwo\nthree" | sed -n 1p
one
$ sed -n 10,20p <file>

Or pipe it with a command:

$ cat <file> | sed -n 10,20p

If you want to print all lines but lines 10 to 20 replace p with d.

From an output:

$ echo -e "one\ntwo\nthree" | head -10

From a file:

$ head -10 <file>

From an output:

$ echo -e "one\ntwo\nthree" | tail -10

From a file:

$ tail -10 <file>

To reverse this replace p with !p.

$ echo -e "one\ntwo\nthree" | sed -n '/two/p'
two
$ echo -e "one\ntwo\nthree" | sed -n '/^one/p'
one

Same thing but with several words:

$ echo -e "one\ntwo\nthree" | sed -n '/^one\|^three/p'
one
three

To reverse this replace p with !p.

$ echo -e "one\ntwo\nthree" | sed -n '/one$/p'
one
three

Same thing but with several words:

$ echo -e "one\ntwo\nthree" | sed -n '/one$\|three/p'
one
three
$ echo "this is an example,string" | cut -d "," -f1
this is an example
$ echo "this is an example,string" | rev | cut -d "," -f1 | rev
string

The line will be the same width as your terminal.

printf "%`tput cols`s"|tr ' ' '-')
---------------------------------------[...]
$ echo "Example" | rev
elpmaxE

Removing words

Remove the first character of the first line

$ echo -e "example\nwords" | sed '0,/./s/.//'
xample
words

Remove the last character of the first line

$ echo -e "example\nwords" | sed '0,/./s/.$//'
exampl
words

Remove the first character of every line

$ echo -e "example\nwords" | sed 's/.//'
xample
ords

Remove the last character of every line

$ echo -e "example\nwords" | sed 's/.$//'
exampl
word

Remove trailing spaces from all the lines in a file

$ sed -i 's/^[ \t]*//;s/[ \t]*$//' <file>

Remove all commented lines

$ sed -i 's/#.*//' <file>

Remove all empty lines

$ sed -i '/^$/d' <file>

Replacing words

Replace the first occurrence of a word

$ echo -e "one two four" | sed 's/four/three/'
one two three

Replace the second occurrence of a word

$ echo -e "one two three three" | sed 's/three/four/2'
one two three four

Replace all occurrences of a word

$ echo -e "dog cat cat" | sed 's/cat/dog/g'
dog dog dog

Replace a word on line 3

$ sed -i '3 s/cat/dog/' <file>

Replace all occurrences of a word on lines 3-10

$ sed -i '3-10 s/cat/dog/g' <file>

Replace whole line with matched pattern

echo -e "1\n4\n3" | sed '/4/c 2'
1
2
3

Replace all tabulator spaces with 4 regular spaces in a file

$ sed -i 's/\t/    /g' <file>

Changing lowercase & uppercase words

Change all words to lowercase

$ echo "EXAMPLE Text" | awk '{print tolower($0)}'
example text

Change all words to uppercase

$ echo "Example text" | awk '{print toupper($0)}'
EXAMPLE TEXT

Change the first letter to lowercase

$ echo "example" | sed 's/.*/\l&/'
Example

Change the first letter to uppercase

$ echo "example" | sed 's/.*/\u&/'
Example

Change the last letter to lowercase

$ echo "ExamplE" | sed 's/.$/\l&/'
Example

Change the last letter to uppercase

$ echo "EXAMPLe" | sed 's/.$/\u&/'
Example

Feedback

I don't have a commenting system on my blog since there's no good solutions that works without JavaScript, is user friendly and respects both the users freedom and privacy. So for now, all feedback and hellos can be done via either e-mail, XMPP or Mastodon.