Text processing Flashcards
(26 cards)
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
sort a file with numeric data in reverse order.
$ sort -nr filename.txt
(combination of 2 options)
Use a command option to sorting a table on the basis of any column number. Also if that column is numeric.
$ sort -k 2 employee.txt
$ sort -k 2n employee.txt
use a command option to sort and remove duplicates .
$ sort -u filename.txt
use a command option to sort by month
$ sort -M filename.txt
use a command option prints the number of words present in a file.
wc -w filename.txt
use a command to convert lower case characters to upper case.
tr [a-z] [A-Z] < filename.txt
tr [:lower:] [:upper:] < filename.txt
$ cat greekfile | tr [a-z] [A-Z]
use a command to translate white-space characters to tabs.
tr [:space:] “\t” <filename.txt
tr “ “ “\t” <filename.txt
use a command to translate braces into parenthesis and redirect it to another file.
$ tr “{}” “()” <greekfile>newfile.txt</greekfile>
use a command to squeeze a sequence of repetitive characters.
$ echo “Welcome To GeeksforGeeks” | tr -s “ “
tr -s “ “ <filename.txt