Sie sind auf Seite 1von 12

CAT

Topic
Welche sind die Linux cat Befehle?

Erluterung
cat BEFEHL:
Der cat Linux-Befehl verkettet Dateien und druckt dies auf dem Standardausgabegert
aus.
SYNTAX:
Die Syntax ist
cat [OPTIONEN] [DATEI]...
OPTIONEN:
-A
-b
-e
-E
-n
-s
-T
-v

alle anzeigen.
bercksichtigt nicht Zeilennummern fr den Leerraum in der Ausgabe.
Ein $ Zeichen wird am Ende jeder Zeile vor einer neuen Zeile gedruckt
werden.
Stellt ein $ (Dollarzeichen) am Ende jeder Zeile dar.
Zeilennummern fr alle Ausgabezeilen.
Wenn die Ausgabe zahlreiche Leerzeilen hat, dann ersetzt sie es mit
einer Leerzeile.
Stellt die tab-Zeichen in der Ausgabe dar.
Nicht auszudruckende Zeichen (mit Ausnahme von Tabs, neuen Zeilen
und Seitenvorschub) werden sichtbar ausgedruckt.

BEISPIEL:
1. eine neue Datei erstellen:
cat > file1.txt

Dieser Befehl erstellt eine neue Datei file1.txt. Nach der Eingabe in die Datei
drcken Sie Control+d (^d) simultan, um die Datei zu beenden.
2. Um Daten an die Datei anzuhngen:
cat >> file1.txt

Um Daten an dieselbe Datei anzuhngen, nutzen Sie den Anhngen-Operator >>


um in die Datei zu schreiben, ansonsten wird die Datei berschrieben werden

(d.h. ihr gesamter Inhalt wird gelscht werden).


3. Um eine Datei darzustellen:
cat file1.txt

Dieser Befehl stellt die Daten in der Datei dar.


4. Um einige Datei zu verketten und darzustellen:
cat file1.txt file2.txt

Der obige cat Befehl wird zwei Dateien verketten (file1.txt und file2.txt) und wird
das Resultat auf dem Bildschirm darstellen. Einige Male mag die Ausgabe nicht
auf die Monitorgre angepasst sein. In einer solchen Situation knnen Sie jene
Dateien in einer neuen Datei verffentlichen oder die Datei darstellen unter
Nutzung wenigerer Befehle.
cat file1.txt file2.txt | less

5. Um einige Datei zu verketten und die Ausgabe in eine andere Datei zu


bertragen.
cat file1.txt file2.txt > file3.txt

Im obigen Beispiel wird die Ausgabe umgeleitet zur neuen Datei file3.txt. Der cat
Befehl wird die neue Datei file3.txt erstellen und das verkettete Resultat in
file3.txt lagern.

SED
Topic

Welche sind die Linux sed Befehle?

Erluterung
sed BEFEHL:
sed ist ein Streameditor. Der sed Befehl hilft, alle Treffer einer Zeichenkette zu einer anderen innerhalb
einer Datei zu bearbeiten oder zu lschen. Er nimmt eine Datei als Input und druckt das Resultat auf dem
Bildschirm aus oder leitet die Ausgabe an eine genannte Datei um.
SYNTAX:
Die Syntax ist
sed [Optionen] '{Befehl}' [Dateiname]
OPTIONEN:
Der Befehl und seine Funktion
-n
gibt die anhngende neue Zeile nicht aus
-e
ermglicht die Interpretation der unten aufgefhrten Backslash-escaped Zeichen
-E verhindert die Interpretation solcher Sequenzen in STRINGs
Ohne -E werden die folgenden Sequenzen erkannt und interpoliert:
\NNN das Zeichen, dessen ASCII-Code NNN (oktal) ist
\a Alarm (BEL)
\b Rckwrtsschritt
\c unterdrckt die anhngende neue Zeile
\f Formfeed
\n neue Zeile
\r Wagenrcklauf/Carriage Return
\t horizontaler Tab
\v vertikaler Tab

BEISPIEL:
Lassen Sie uns annehmen, dass wir eine Datei file1.txt haben und sie hat folgende Daten.
hscripts hat viele ntzliche kostenlose Scripts
Es ist die Hauptseite von www.forums.hscripts.com
hscripts beinhaltet Gratisanleitungen und kostenlose gif-Bilder
kostenloses DNS Nachschlage-Tool
kaufen Sie Scripts von uns
Eine Webmaster/Webmaster Ressourcen-Webseite

sed G file1.txt>file2.txt

Im obigen Beispiel, bei Nutzung des sed Befehls mit G, wrde dies den Raum der Datei file1.txt
verdoppeln und die Resultate an file2.txt ausgeben.
sed = file1.txt | sed 'N;s/\n/\. /'

Im obigen Beispiel wird der sed Befehl benutzt, um jede Zeile in file1.txt auszugeben mit der
Zeilennummer, gefolgt durch einen Punkt und einen Raum vor jeder Zeile.
sed 's/scripts/javascript/g' file1.txt

ffnet die Datei file1.txt und sucht nach dem Wort 'scripts' und ersetzt jeden Treffer mit dem Wort
'javascript'.
sed -n '$=' file1.txt

Der obige Befehl zhlt die Anzahl der Zeilen von file1.txt und gibt die Ergebnisse aus.

Der obige Befehl zhlt die Anzahl der Zeilen von file1.txt und gibt die Ergebnisse aus.

Understanding the cat Command


cat
Cat is used to either view, create, or join multiple text files together. (In fact, the term cat
is short for catenate, which is a fancy way of saying to join two things together, end-toend.)
By default, stdin for cat is the keyboard, and stdout is the computer screen. If you just type
cat at the command prompt, youll be able to type in text, and make it echo back to you as
soon as you hit Enter. It will keep doing this until you press Ctrl-d to end it.
cat
My hostname is mail.example.com
My hostname is mail.example.com

Of course, this by itself isnt terribly useful. But, you can use cat with the stdout redirector to
create simple text files. When youre through typing the message, hit Enter once more to get
to a blank line, and then press Ctrl-d to exit.
cat > test
My hostname is mail.example.com

Once youve created your file, you can now use cat to display it. Its not like the less utility,
though; cat simply dumps everything in the file onto the display screen. Note that you dont
need to use a stdin redirector with cat. Thats because cat is designed to use arguments,
instead of stdin redirectors.
cat test
My hostname is mail.example.com

Now, use cat to create a second text file.


cat >test2
Your hostname is ftp.example.com

Heres where the catenate part comes in. Invoke cat again, but use the names of both of your
new files as arguments.
cat test test2
My hostname is mail.example.com
Your hostname is ftp.example.com

This time, youll see both of your files displayed as if they were one single file.
Now, add a stdout redirector, and youll be able to create a new file by combining the first
two.
cat test test2 >test3
cat test3
My hostname is mail.example.com
Your hostname is ftp.example.com

There are several display options that you can use with cat. Use the -s option to squeeze
out extra blank lines. That way, youll never have two or more consecutive blank lines.
cat -s

filename

The -t (or -T) option will cause all tabs to be shown as ^I.
If you need to see where the ends of lines are, you can use the -e (or -E) option to mark them.
cat -cat -e /etc/dovecot.conf
protocols = pop3 $
ssl_disable = yes$
protocol imap {$
}$
$
protocol pop3 {$
}$
protocol lda {$
postmaster_address = postmaster@example.com$
}$
auth default {$
mechanisms = plain$
passdb pam {$
}$
userdb passwd {$
}$

To use both the tabs and the end of line options together, use the -A option.
cat -A /etc/dovecot.conf

The -b option will number all non-blank lines for you.


cat -b /etc/dovecot.conf
1
protocols = pop3
2
ssl_disable = yes
3
protocol imap {
4
}
5
6
protocol pop3 {
7
}
8
protocol lda {
9
postmaster_address = postmaster@example.com
10
}
11
auth default {
12
mechanisms = plain
13
passdb pam {

Or, use the -n option to have all lines numbered.


cat -n /etc/postfix/main.cf
1
queue_directory = /var/spool/postfix
2
command_directory = /usr/sbin
3
daemon_directory = /usr/libexec/postfix
4
mail_owner = postfix
5
inet_interfaces = all
6
unknown_local_recipient_reject_code = 550
7
debug_peer_level = 2
8
debugger_command =
9
PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
10
xxgdb $daemon_directory/$process_name $process_id & sleep 5
11
sendmail_path = /usr/sbin/sendmail.postfix
12
newaliases_path = /usr/bin/newaliases.postfix
13
mailq_path = /usr/bin/mailq.postfix
14
setgid_group = postdrop
15
html_directory = no
16
manpage_directory = /usr/share/man
17
sample_directory = /usr/share/doc/postfix-2.3.3/samples
18
readme_directory = /usr/share/doc/postfix-2.3.3/README_FILES
19
inet_interfaces = all
20
21
# Basic Configuration

Tagged as: cat

Sed - Delete one or more lines from a file


Unix utility SED provides an effective and a versatile way of deleting one or more lines from a
designated file to match the needs of the user. This Unix command is used for command line
processing. This utility can be used to delete expressions from a file which can be identified by a
specifying delimiter (like comma, tab, space, etc.), by line number, by searching for a string,
expression or the address of a line in the syntax of SED.

Here is how to remove one or more lines from a file.

Syntax:
sed '{[/]<n>|<string>|<regex>[/]}d' <fileName>
sed '{[/]<adr1>[,<adr2>][/]d' <fileName>

/.../=delimiters
n = line number
string = string found in in line
regex = regular expression corresponding to the searched pattern
addr = address of a line (number or pattern )
d = delete

Examples
Remove the 3rd line:
sed '3d' fileName.txt

Remove the line containing the string "awk":


sed '/awk/d' filename.txt

Remove the last line:


sed '$d' filename.txt

Remove all empty lines:


sed '/^$/d' filename.txt
sed '/./!d' filename.txt

Remove the line matching by a regular expression (by eliminating one containing digital
characters, at least 1 digit, located at the end of the line):
sed '/[0-9/][0-9]*$/d' filename.txt

Remove the interval between lines 7 and 9:


sed '7,9d' filename.txt

The same operation as above but replacing the address with parameters:
sed '/-Start/,/-End/d' filename.txt

The above examples are only changed at the display of the file (stdout1= screen).
For permanent changes to the old versions (<4) use a temporary file for GNU sed using the "i[suffix]":
sed -i".bak" '3d' filename.txt

Replace text on the fly, without even starting an editor, using this classic
tool.
The filter sed can process text from standard input and write its results to standard output. The input
can be redirected from a file, and the output also can be redirected to a file using your shell's
redirection capabilities. It has hundreds of uses, and once you learn sed, you really would miss it if
you lost it.

sed can append lines, remove lines, change lines, rearrange lines, substitute text strings and
more. Using sed, you can write simple scripts that can become powerful text manipulating
commands.
sed can use regular expressions to define what processing will occur on lines of text and
which lines it processes. If you have never seen or used regular expressions before, you may
want to become familiar with the basic syntax of regular expressions. In this article, we use a
few regular expressions to make sed do some simple text processing.
Ways to Run sed

sed can be run on the command line as follows:


cat sample.txt | sed -e '1,15d'

You can cat the file sample.txt and use the pipe to redirect its output (the lines of text) into the
sed command. The -e option to sed tells it to use the next item as the sed command. The d
command tells sed to delete lines 115 of the input stream, which in this case is the lines read
from sample.txt. The rest of the file (if any) appears on standard output, your terminal
window, unless redirected elsewhere.
Also, you simply can specify the input file as a command-line argument, so the above sed
command also can be written as:
sed -e '1,15d' sample.txt

You also can tell sed to read commands from a script file by using the [-f script-file] option.

sed Command Format

A sed command has this format:


[pattern1][,pattern2][!] command [args]

The pattern1 and pattern2 are optional line ranges. Some commands don't use the patterns,
some commands use only one and some can use both to specify a range of lines that the sed
command can operate on, as we did in our simple example above.
pattern1 and pattern2 can be numbers, in which case they are treated like line numbers. They
can also be a regular expression delimited by slashes (/pattern/). When using regular
expression patterns, all lines that match the expression are filtered through the sed command.
If no pattern is specified, the sed command operates on every line of input.
The ! causes sed to operate on every line not included in the pattern range. You can change
our example above to be:
cat sample.txt | sed -e '1,15!d'

This command deletes all lines except lines 115.


A Few sed Commands

Here are a few basic sed examples. These can all be run right from the command line. Testing
and debugging your sed commands individually on the command line before integrating them
into a larger script will save you a lot of time that otherwise would be spent debugging the
commands from within a running script.
Let's say that you have a file that lists customers called customer.txt. For the following
examples, it contains simple lines of text, like this:
Sam Jones
Brenda Jones
Carl Simon
Liz Smith

Let's use some sed commands to manipulate this file. For example, if you want to remove
lines containing Carl Simon and update your customer file, you can do the following:
cat customer.txt | \
sed -e '/Carl Simon/d' > customer.txt

The pattern /Carl Simon/ is used by sed as a regular expression and matches every line that
has that pattern somewhere on the line. The d command deletes every line that matches the
pattern. So, any lines containing Carl Simon are removed from the file.
If you want to perform some type of text substitution on a text file, the s command is probably
what you are looking for. It substitutes one text string for another. We tend to use this a lot in

our scripts. For example, if Sam Jones calls up and tells you that you should have him listed
as Samuel Jones, you can use this command to make the change:
cat customer.txt | \
sed -e 's/Sam Jones/Samuel Jones/' > customer.txt

The s command in sed has three slashes that follow the s. The text between the first and
second slash is the pattern you want to match. The text between the second and third slash
contains the pattern that you want to substitute for the first pattern. If you wanted all instances
of Sam to be Samuel (not just Sam Jones), you could rewrite this example as follows:
cat customer.txt | \
sed -e 's/Sam/Samuel/' > customer.txt

The commands for append (a), replace (c) and insert (i) typically need to have the sed
commands specified in a separate script file. For example, say you want to append the line
After Brenda right after the line that contains the text Brenda. You can use the a sed command
to append the text there. However, you need to put the sed commands in a separate script file,
so fire up your favorite editor and create the following sed command file:
#
# sed command file (# are comment lines)
#
# append the line 'After Brenda'
# in this customer file
#
/Brenda/a\
After Brenda

Save this script file as sed1.cmd. Then, to run sed using this script file, use this syntax:
sed -f sed1.cmd customer.txt

You should see the contents of your customer file with the additional line added after the line
Brenda Jones. The pattern /Brenda/ (in the sed command file) determine where in the output
our appended line appears.
The difference between the append command and the insert command is where the text is
added. For the append command, the text is added after the line containing the match. For the
insert command, the text is added before the line that contains the match.
For those who have never used regular expressions, here are three regular expressions that
are very useful when combined with sed:
1. To match the start of a line, use the ^ character.
2. To match the end of a line, use the $ character.

3. To match any number of characters in a regular expression, use the characters .*. The .
matches any single character, and the * matches any number of characters (including
none at all).
Practical Examples
Filter out empty lines from a file:
sed -e '/^$/d' your_file.txt

Add the computer named mycomputer to the end of every line in /etc/exports:
cat /etc/exports | \
sed -e 's/$/ mycomputer/' > /etc/exports

Add the computer named comp2 only to the directories beginning with /data/ in /etc/exports:
cat /etc/exports | \
sed -e '/^\/data\//s/$/ comp2/' > /etc/exports

See how the forward slashes used in the directory name have to be escaped using back
slashes? Without the back slashes, sed interprets the forward slashes in the directory specifier
as the delimiters in the sed command itself. However, the back slashes can make the sed
command difficult to read and follow.
Remove the first word on each line (including any leading spaces and the trailing space):
cat test3.txt | sed -e 's/^ *[^ ]* //'

More regular expression matching is used in this example. Here's what it is doing.
The initial ^ * is used to match any number of spaces at the beginning of the line. The [^ ]*
then matches any number of characters that are not spaces (the ^ inside the brace reverses the
match on the space), so it matches a single word. The trailing space at the end matches the
space found at the end of the first word. The empty replace pattern removes the text.
Remove the last word on each line:
cat test3.txt | sed -e 's/^\(.*\) .*/\1/'

This command introduces the concept of hold buffers. Hold buffers are used to keep parts of
the matched text and to insert that text into the result. The pattern that matches the text
between the parentheses is recalled in the substitution pattern by the \1. If an additional set of
parentheses were in the match pattern, they would be addressed in the substitution pattern as

\2, and so on, for more sets of parentheses. Up to nine hold buffers can be specified. In this
example, the pattern contained within the parentheses matches from the start of the line up to
the last space (the space after the parentheses).
To remove leading { and trailing }, or a } from each line:
sed -e 's/^.*{\(.*\)},*/\1/' table.txt

I'll leave it to the reader to dig in to this regular expression to see how it operates. Keep this in
mindthe more comfortable you are with regular expressions and hold buffers, the more
powerful the sed command becomes.
Conclusion
sed recognizes many other commands. However, even with these basic commands, you can
successfully manipulate text files from within your own shell scripts or right from the
command line.