String Manipulation

Editing Text

sed "1d" → Removes first line of file/stream

cut -c 5 new.txt → Outputs the 5th character in each line of the new.txt file

cut -f 2 new.txt → Extract contents by a field, by default everything separated by a TAB is a field.

cut -f 1 -d ";" new.txt → Extract first field separated by ;

head → Outputs the first 10 lines of a file

head -n 15 → Outputs the first 15 lines of a file

tail → Outputs the last 10 lines of a file

tail -n 15 → Outputs the last 15 lines of a file

join file1.txt file2.tx → Join file1.txt and file2.txt

sort new.txt → Sort new.txt in alphabetic order

sort -r new.txt → Sort new.txt in reverse alphabetic order

sort -n new.txt → Sort new.txt by numerical value

tr a-z A-Z → Translate all lower case character to uppercase characters

cat new.txt | tr -d "." → Remove all dots

cat new.txt | tr "." "_" → Remove all dots and replace them with underscores

uniq new.txt → Remove all adjacent duplicates in new.txt

uniq -c new.txt → Get the count of how many occurences are on a line

uniq -u new.txt → Gets only unique values

uniq -d new.txt → Gets only duplicate values

sort new.txt | uniq → Remove all duplicates in new.txt even if they are no adjacent

wc /etc/passwd → Shows the total count of words in a file

nl new.txt → Check the count of lines on a file

grep pattern new.txt → Finds "pattern" inside the new.txt file

grep -i pattern new.txt → Finds case insensitive "pattern" in the new.txt file

ls /somedir | grep '.txt$' → Returns all files ending with .txt in some dir

awk '{print}' filename → Prints every line of a file

awk '/192.16.40.10/' error.log → Filters out specific IP address

awk '/172.16.40.10.81/ {print $4}' error.log → Print out the forth column of a file

awk '{print $2,$5;}' error.txt → Prints out columns 2 and 5

awk -F ':' '{print $1}' test.txt → Prints first column of test.txt with : as a delimiter

Environ

echo $HOME → Path to your home directory

echo $USER → Returns username

env → Returns information about the environment variables

echo $PATH → Returns a list of paths separated by a colon that the system searches when it runs a command

Pipe/tee

ls -la /etc | less → Pipe less with the ls -la command

ls | tee new.txt → Displays the content of ls in the screen and inside the new.txt file

Last updated

Was this helpful?