In this article, we will be discussing the useful cases of split unix command with examples. We will also be discussing the tr and tee commands
split unix command
it split a file into pieces.
SYNTAX for the Split command
split [-linecount | -l linecount | -b bytes] [file [name] ]
This command is widely used to split the files into smaller small for variour purpose like parallel processing. You have 1000 lines command where each command can be executed parallelly, then you can split the files in example 4 files and execute them parallely to finish the processing in time.
Other example would be split the big files and then transmit over network so that we dont get disconnection issues
How to split the files based on lines
split -l 100 x.txt z
This would split the file “x.txt” into files beginning with the name “z” each containing 100 lines of text each
$split -l 100 x.txt z $ls zaa zab zac z.txt
This will output three files, zaa, zab, and zac, and each one will be 1,00 lines long.
How to split the files based on bytes
We can use the split option -b to enable the splitting based on bytes
split -b 40k f1.txt segment
This will output four 40KB files: segmentaa, segmentab, segmentac, and segmentad.
tee – Used in the middle of a pipeline, this command allows you to both redirect output to a file, and pass it to further commands in the pipeline.
How to use tee command
Examples: $check_patch_logs.ksh | tee check.log| more
Output of check_patch_logs.ksh is stored in the filecheck.log, and also pass it to more to display it one screen at a time.
tr is used to Translate characters.
tr [-c] [-d] [-s] [string1] [string2]
|-c||Complement the set of characters specified by string1.|
|-d||Delete all occurrences of input characters that are specified by string1.|
|-s||Replace instances of repeated characters with a single character.|
|string1||First string or character to be changed.|
|String2||Second string or character to change the string1.|
How to convert lower case to upper case
We can use tr command with out option -d,-c and -s for that
$ echo $ORACLE_SID| tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ TECH $ echo $ORACLE_SID| tr [a-z] [A-Z] TECH $ echo $ORACLE_SID| tr [:lower:] [:upper:] TECH $ echo "good boy" | tr good bad bddd bdy
How to Delete specified characters using -d option
tr can also be used to remove particular characters using -d option.
$ echo $ORACLE_SID TECH $ echo $ORACLE_SID| tr -d ‘T’ ECH $echo "good boy" | tr -d "g" ood boy
How to use option -s to convert repeated occurrence of character into single character
tr ‘[A-Z]' ‘[a-z]' <1.txt | tr -cs 'a-z' '\n' | sort
What this does is pass the contents of 1.txt to the tr command to translate uppercase letters to lowercase; the output from that is piped to another tr command that turns everything except lowercase letters into line breaks (effectively putting each word on a separate line); that’s piped to sort which puts the lines (words) in alphabetical order
How to use option -c with tr
-c can be used to find the complement of string1.
For example, to remove all characters except digits, you can use the following
$ echo " My id is MQ123452" | tr -cd [:digit:] 123452
awk command : Complete guide on awk command in Unix with lot of examples
sed command : A good overview of sed command and difference between awk and sed and much more
grep command : A lot of grep command with options