Files | Manual

Linux Text Processing Commands (Cheat Sheet)

Linux Text Processing Commands (Cheat Sheet)

Main text processing commands: sed, grep, cat, sort, iconv, comm

File Conversion

iconv -f cp1251 -t utf-8 123.txt > new.txt
Convert from Windows txt to Linux: shellhacks.com
find . -name '*.txt' -exec iconv -f CP1251 -t UTF-8 {} -o {} ;
or for all files

Search and Replace Text

find ./ -type f -name '*.php' -exec sed -i -r 's/OLD-PASSWORD/NEW-PASSWORD/g' {} ;
find ./ -type f -name '*.ini' -exec sed -i -r 's/OLD-PASSWORD/NEW-PASSWORD/g' {} ;

Working with File Contents

cat file_original | [operation: sed, grep, awk, etc.] > result.txt
general syntax for performing actions on file contents and outputting the result to a new file.
cat file_original | [operation: sed, grep, awk, etc.] >> result.txt
general syntax for outputting the result to an existing file (the file will be created if it does not exist).
Examples:
grep Aug /var/log/messages
sed '/^$/d' example.txt (removing empty lines)
sed 's/string1//g' example.txt (removing the string "string1")

Comparing and Sorting Files

paste file1 file2
combine the contents of file1 and file2 in a tabular format.
sort file1 file2 | uniq
sort the contents and remove duplicates.