Text processing Flashcards
use a command to prints second, fifth and seventh character from each line of the file.
$ cut -c 2,5,7 name.txt
command prints starting from first character to end.
command prints starting position to the fifth character.
$ cut -c 1- state.txt
$ cut -c -5 state.txt
when space is used as a field separator or delimiter to print words of each line.
$ cut -d “ “ -f 1 state.txt
use a command to print the 1th and 4th column (field) in a file.
$ awk ‘{print $1,$4}’ employee.txt
in awk print the first item along with the row number separated with ” – “ from each line.
$ awk ‘{print NR “- “ $1 }’ geeksforgeeks.txt
use a command for Printing lines with more than 10 characters.
$ awk ‘length($0) > 10’ geeksforgeeks.txt
To print the squares of first numbers from 1 to n say 6 in awk.
$ awk ‘BEGIN { for(i=1;i<=6;i++) print “square of”, i, “is”,i*i; }’
use a search command to do a Case insensitive search.
$grep -i “UNix” geekfile.txt
use a command to search and to Displaying only the matched pattern (word) you want to search in a file.
$ grep -o “unix” geekfile.txt
use a command to Show line number while displaying the output using grep.
$ grep -n “unix” geekfile.txt
use a command to display the lines that are not matching with the specified search string pattern.
$ grep -n “unix” geekfile.txt
use 2 commands to search for multiple strings (words) in a file.
$grep –e “Agarwal” –e “Aggarwal” –e “Agrawal” geekfile.txt
egrep -i milo|dandi|denne geekfile.txt
use 2 methods to sort a file and to write the output to a new file.
$ sort inputfile.txt > filename.txt
$ sort -o filename.txt inputfile.txt
use a command option to Sort In Reverse Order:
$ sort -r inputfile.txt
use a command option to sort the file with numeric data present inside.
$ sort -n filename.txt