Commit 218374f4 authored by bobloblaw's avatar bobloblaw
Browse files

Updates basics_of_linux.md

Auto commit by GitBook Editor
parent 36905b16
Loading
Loading
Loading
Loading
+37 −39
Original line number Diff line number Diff line
@@ -165,30 +165,40 @@ cp: cannot stat 'thisfiledoesnotexist': No such file or directory
# If we now look at result.txt we will find that it is empty. Since the error-text we recieved could not be redirected into the textfile, since it is stderr and not stdout.
```

### Filters

There are certain programs that are especially useful to use together with pipes. They can also be used as stand-alone programs but you will often see them together with pipes.


* [https://linuxjourney.com/lesson/stderr-standard-error-redirect](https://linuxjourney.com/lesson/stderr-standard-error-redirect)

### cut

This is a useful command to cut in text.

Let's say that we have the following text, and we want to cut out the ip-address.
`sort`

```
64 bytes from 192.168.0.1: icmp_req=1 ttl=255 time=4.86 ms
sort test.txt
```

`uniq`

```
cut -d" " -f4
sort -u test.txt
sort test.txt | uniq
cat filename | sort -u > newFileName
```

`-d stands for delimiter. and -f for field.`
`grep`

`head`

`tail`

`tr`

`sed`



### sed - Stream editor
### Editing text

sed can perform basic editing on streams, that is to say, text.

`sed` - Can perform basic editing on streams, that is to say, text.

Remove first line of file/stream

@@ -196,37 +206,29 @@ Remove first line of file/stream
sed "1d"
```

### Show all lines just once
`awk`

You have a list of passwords, or ip-addresses or whatever. And you want to remove all duplicates. How do you do it?

You have list:

```
aa
aa
bb
bb
cc
```
`cut` - Cut by column

And you want to see:
This is a useful command to cut in text.

Let's say that we have the following text, and we want to cut out the ip-address.

```
aa
bb
cc
64 bytes from 192.168.0.1: icmp_req=1 ttl=255 time=4.86 ms
```

Command:

```
sort -u test.txt
sort test.txt | uniq
cat filename | sort -u > newFileName
cut -d" " -f4
```

### tr - translate
`-d` stands for delimiter. and `-f` for field.



**tr** - Translate

Transform all letter into capital letters

@@ -248,7 +250,8 @@ cat file.txt | tr "." "_"

[http://www.thegeekstuff.com/2012/12/linux-tr-command/](http://www.thegeekstuff.com/2012/12/linux-tr-command/)

### AWK

**awk** 

So awk is an advanced tool for editing text-files. It is its own programming language to it can become quite complex. Awk iterates over the whole file line by line.

@@ -267,8 +270,6 @@ This just prints every line of the file.
awk '{print}' filename
```

#### Filtering capabilites

Filtering out specific ip-address:

```
@@ -284,15 +285,12 @@ awk '{print $2,$5;}' error.txt
This prints columns 2 and 5.
```

#### Custom delimiter

We can use the -F flag to add a custom delimiter.

```
awk -F ':' '{print $1}' test.txt
```

#### BEGIN and END statements

So if you are manipulating some text you might want to start the output with some info about the columns or something like that. To do that we can use the BEGIN-keyword.