Data Manipulation: echo, printf, tr, wc, sort, cut, paste, diff, awk, sed
echo
Built-in Linux feature to print out arguments as standard output.
echo {string}
printf
Similar to echo, but allows for more control over the formatting of output.
printf {string}
| Option | Function |
| \n | newline printf "hello \n how are you" |
tr
tr, or translate, performs operations like removing repeated characters, converting uppercase to lowercase, and basic character removal
or replacement.
tr {character 1} {character 2}
echo 'linuxize' | tr 'lin' 'red'
reduxeze
In this example, tr has been used to replace the occurrence of 'lin' with 'red'.
wc
wc, or wordcount, is a function used for counting a part of an output. wc can be used to count the number of words, lines, characters, or bytes in a file or output.
wc [options] {filename}
| Option | Function |
| -l | Line count |
| -c | byte count |
| -m |
character count |
| -w |
word count |
sort
Linux utility for sorting lines of text files.
sort [options] {filename}
| Option | Function |
| -k {column number) | Sort by values in a specified column. |
| -n | Sorts based on numerical value |
| -r | Sorts fields in descending order |
| -t | Seperate one field from another |
cut
Extracts specified lines of text from a file.
cut [options] {filename}
| Option | Function |
| -c | Specify the number of characters to cut from each line. |
| -d {delimter} | Seperate one field from another. |
| -f {fieldnumber) | Specify the field numbers to cut as separated by the delimiter. IE f2 would be the field between the first and second instances of the delimiter. |
| -s | Suppress a line if a delimiter is not found. |
Example
root@jump:~# cat test1.txt
hello1 hello2 hello3 hello4 hello5 hello6
hello1 hello2 hello3 hello4 hello5 hello6
root@jump:~# cut -d " " -f2-3 test1.txt
hello2 hello3
hello2 hello3
In this example, I have a file with hello written 6 times across each line, seperated by a single space.
Using cut, I've set the space (" ") as the delimiter, or seperator, and then have selected fields 2-3 to be cut.
paste
Used to merge lines from text files horizontally.
diff
Used to compare text files.
diff [options] {file1} {file2}
| Option | Function |
| -b |
Ignore spacing differences |
| -i | ignore case differences |
| -t | Expand tab characters in output lines |
| -w | Ignore spacing differences and tabs |
| -c | Display a list of differences with three lines of context |
| -u | Output results in unified mode. |
awk
Performs pattern matching on files. awk does have some advanced functionality such as allowing execution of code blocks on patterns, ie if pattern X is found then execute code Y.
awk [options] ['patterns {actions}'] {filename}
sed
Stream Editor - sed. A program that can be used to modify text files according to various parameters.
sed {'option/addresss/action'} {filenames}
| Options | Function |
| d | delete the lines that match a specified pattern |
| -n,p | Print only the lines that contain the pattern. |
| s | Sustitute the first occurrence of the string in aa file. |
| s,g | Globally substitute the original string with the replacement string for each occurrence. |
No Comments