User Tools

Site Tools


Site Tools

Table of Contents

Gnu tools

tail

Display last 20 lines of a file:

tail -20 myfile.txt

awk

Display first line :

head -1

Display two first lines :

head -2

sed

remove last “character” found in a line (here : ,):

sed 's/\(.*\),/\1/' 

Remove blanck lines :

sed '/^$/d'

Replace a value after a pattern : For example, to replace the integer after the string my_string in file myfile :

myfile
my_string : 40

Here no -i, only to test and show what will be modified. Note that \s+ means “space” or “multiple spaces”, in case of user add a space somewhere.

mynewvalue=10
sed -re "s/(my_string\s+:\s+)[^=]*$/\1 $mynewvalue/" myfile

Add -i to apply directly in the file :

mynewvalue=10
sed -i -re "s/(my_string\s+:\s+)[^=]*$/\1 $mynewvalue/" myfile

From http://stackoverflow.com/questions/19737398/sed-how-to-replace-a-string-found-after-a-specific-pattern-is-located-in-a-file