1.20 Manipulate file content programmatically

Operations with content

What can we do with the contents of the file? Usually a few operations: create, view, copy, modify and delete. In the Linux operating system for these operations, there are many different options. The easiest way is to use a text editor, like vi or nano. But there are other ways. They are usually used when processing a large number of files, but sometimes they help to cope with simple tasks.

Content manipulation

With echo command you can create content wery simple, just use output redirection simbols > or >>

# echo 'some text' > f1.txt

If you need to create content divided by line use option -e and special identificator of new line in text \n

# echo -e 'line 1\nline 2\nline 3'>>f2.txt

Other options you can find in command manual.

To view content in file use cat command

# cat f2.txt
line 1
line 2
line 3

With halp of cat command you can concatenate files in one

# find ./ -name '*.txt' | xargs -i cat {} > all.txt
# cat all.txt
some text
line 1
line 2
line 3

One of the most powerfull command is sed, with it help you can search and transform content in files.

Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).

Search and replace text 's///'

# cat f2.txt | sed 's/line/lime/'
# sed 's/line/lime/' f2.txt
lime 1
lime 2
lime 3

Use regular expression

# sed 's/^.*[2-3]/lime/' f2.txt
line 1
lime
lime
# sed 's/[0-9]/(&)/' f2.txt
line (1)
line (2)
line (3)

By default 's///' replase only first finded pattern in string

# sed 's/[a-z]/(&)/' f2.txt
(l)ine 1
(l)ine 2
(l)ine 3

The g option appended to the substitution statement sets a “global” mode that forces sed to replace multiple instances of the match on the same line.

# sed 's/[a-z]/(&)/g' f2.txt
(l)(i)(n)(e) 1
(l)(i)(n)(e) 2
(l)(i)(n)(e) 3

You can delete whole string with instance

Type to delete string with "2"

# sed '/2/d' f2.txt 
line 1
line 3

Type to delete second string in file

# sed '2d' f2.txt
line 1
line 3

You can combain regular expressions to get better result.

# sed -e '2d;s/[a-z]/(&)/;s/\(3\)/\1\1\1/;' f2.txt
(l)ine 1
(l)ine 333

To save changes into file you can use output redirection or better use -i option.

# sed -i.bak -e '2d;s/[a-z]/(&)/;s/\(3\)/\1\1\1/;' f2.txt
# cat f2.txt
(l)ine 1
(l)ine 333
# cat f2.txt.bak 
line 1
line 2
line 3

Learning to use sed well will allow you to transform text with great speed and flexibility.

Linux distributions includes many other usefull commands like grep, sort, more, less, head, tail , cut, scut, cols, paste, join, pr and others. Read manpages and try it.

CentOS 7

No features.

openSUSE Leap 42.3

No features.

Ubuntu 17.04

No features.

Publication/Release Date: Aug 02, 2017

Advertisement