Introduction To Unix and Linux File Editors
Introduction To Unix and Linux File Editors
First take a look at the man pages for the sed command.
man sed
1. The -n means it will only produce output when explicitly told to via the
p command (the default is to print each line)
2. The '/Pooh/p' tells it to print out lines that have the word “Pooh” in
them.
You can also use sed to find and replace something in a file:
For example, you can use sed to delete underscores in a file. Take a look at
myfile.txt There are underscores around some words that probably have to
do with the formatted version of this material. To get rid of them we create
a pattern that looks for an underscore and replaces it with nothing.
Change the name of one of the characters in the text to something else.
Solution
The solution is similar to the previous one, you just need something in
the “replace” pattern slot
Make a change and send the output to a file instead of the console.
Solution
Make a change and save it in the original file. Hint: there is a command
line option for this.
Solution
Delete all blank lines. Hint: you need a pattern that will match a blank
line.
Solution
The ^ indicates the beginning of the line and the $ indicates the end of
the line and since there is nothing between the two, it means a blank
line. This is deleting all the blank lines, not just the ones at the end so
not the perfect solution for this case.
Solution
The last line is the string “So I tried”, starting with this search pattern it
will delete everything until the end of the file, unfortunately including
this line. In this case, it isn’t exactly what we want.
Delete everything after a specific line and save it “in place”. Hint: you
want to not match the specific line.
Solution
The -i command line option means edit “in place”. Finally, the right
way to do this! The ’1,/So I tried/ specifies a range from the first line
up to and including “So I tried”. The !d means things is this range will
not be deleted but everything else will.
sed -i '1,/So I tried/!d' myfile.txt
For more information, reference the documentation for the sed command.
awk
The name awk is a combination of the first letters of the last names of the
people who created the language - Alfred Aho, Peter J. Weinberger, and
Brian Kernighan.
The awk command provides a way to search for a pattern, and perform
actions on the found text. awk reads lines from the input file one at a time.
The line is scanned for each pattern in the program and if there is a match
the associated action is executed.
Either the pattern or the action can be omitted, but not both.
* If the pattern is omitted, then the action is performed for every line.
* If the action is omitted, then all lines that match the pattern will be
printed out.
man awk
As you will likely notice, awk is very powerful and you can write complete
programs to perform complex tasks using it.
Variable Description
$0 The entire line - not including the newline at the end
$1..$n The fields in a line (delimited by the field separator)
FNR Current line number - just spans the current file
FS Field separator - default is space
NF Number of fields
NR Current line number - spans multiple files
RS Record separator - the default is newline
Rules Description
BEGIN Startup actions
END Cleanup actions
Using awk to count the number of occurrences
of the word “Pooh” in the text
Solution
Add up the number of “fields” on each line. The default field separator
is space so this counts words and prints the total when it gets to the end
of the line.
Solution
The AWK variable NR contains the number of lines in a file so you just
have to print it at the end.
Parse the comma-separated text and output two fields from each line. Don’t
print the header line.
awk -F"," '{if (NR!=1){ print $1 " wrong answers " $5 " out of
total " $8 }}' data.csv
Solution
You don’t need the if statement because you’re printing the entire
column including the title line.
Solution
You use the same syntax you would use for any regular expression.
Unlike echo, printf does not send an automatic newline at the end of the
output. The printf command allows you to optionally assign the result to a
variable rather than outputting to the screen (which is useful in Bash
scripts).
man printf
Let’s take a look at the first syntax pattern (printf format [arguments])
which prints ARGUMENT(s) according to FORMAT.
Solution
Print only the first 4 digits beyond the decimal point of a floating point
number.
Solution
You can use the precision modifier . followed by a number to set the
number of digits beyond the decimal point to display. Use the %f to
format a floating point number.
Ctrl-x exits from the nano editor. Nano does not autosave, you must type
Ctrl-o. The other commands that can be used with nano are listed at the
bottom of the edit screen.
man nano
You can specify -B or --backup on the command line if you want to create a
backup version of the current file before changes are saved.
man vim
Typing vim at the command line will bring up the vim text editor. You can
specify a file name on the command line and that file will open.
important
The : in Vim
The : is used in Vim to enter command mode - always remember to
hold down the shift key to type it - otherwise you will type a ;
Key Description
i Enter insert mode
: Enter command mode
R Enter replace mode
v Enter visual mode (highlighting)
V Enter visual line mode (highlighting lines)
esc Return to normal mode from insert or replace mode
esc+esc Return to normal mode from command or visual mode
Aside from insert mode, Vim users also regularly use command mode. For
example, Vim does not auto-save, you must type :w and click Return or
Enter. The : enters command mode and the w specifies save. Return or
Enter tell vim you have completed the command.
Key Description
w Save the current file
Save the current file
wq and close it, exits vim
if no other open files
Save a copy of the
current file as newname
w newname
but continue editing
the current file
Save a copy of the
sav newname current file as newname
and edit newname
Close a file without
q!
saving
e filename Open another file
Write changes made to
x the current file and
close it
For more information, reference the documentation for vim. There is also a
quick reference.