Linux Essential Commands - File content and Regular expressions
Compare and Manipulate File Content
cat file
# Print file content
tac file
# Print file content in reverse
head -n 10
# print first 10 lines of file
tail -n 10 file
# print last 10 lines of file
wc -l | cat file
# count number of lines in file
diff file1 file2
# Compare differences between file1 and file2
diff -i file1 file2
# Compare differences between file1 and file2 with case in-sensitive
diff -c file1 file2
# view differences in context mode
diff -u file1 file2
# view differences in unified mode
# similar to context mode, but it doesn't display any redundant information
diff -y file1 file2
# or
sdiff file1 file2
# Compare file1 and file 2 side by side with output in two columns
diff -c file1 file2
# Compare file1 and file 2 with output in two columns
diff -e file1.txt file2.txt > my-ed-script.txt
echo "w" >> my-ed-script.txt
# output a script, which can be used by the editing programs ed or ex
ed - file1.txt < my-ed-script.txt
# use ed program to makes the changes and write the changes to file1.txt
# file1.txt now matches file2.txt exactly
# But we can use use this simple way, instead of above steps
cat file2.txt > file2.txt
uniq file
# Remove equal consecutive rows
uniq -w 2 fle
# Remove equal consecutive rows comparing only first two characters
uniq -c file
# Remove equal consecutive rows and show number of occurrences
sort file
# order file content
sort -k 2 file
# Order file content using as reference second word
sort file.txt | uniq
# Order file content and remove repeating lines
cut -d delimiter -f column
cut -d ' ' -f 1 file
# Print first word of each line. Delimiter will be space
cut -d ' ' -f 1,3 file
# Print first and third word of each line. Delimiter will be space
tr SET1 SET2
# translate set of characters one to set of characters 2
cat file | tr test sub
# It will replace all occurrences of test with sub
cat file | tr -s ' '
# It will replace all consecutive occurrences of space with one space
file namefile
# print the type of namefile
sed command (stream editor)
sed –i 's/USA/CANADA/g' userinfo.txt
# replace all USA words by CANADA in the file userinfo.txt.
# The -i is in-place, without -i the file won't be changed