Bash (Linux Shell)
Linux shell scripting language
Last updated
Linux shell scripting language
Last updated
/etc/passwd File
For instance, the file /etc/passwd contains information about each user on the system, one user per line, and each information field is delimited by a colon (":"). For example, the line of /etc/passwd for the root user may look like this:
These fields contain the following information, in the following order, separated by a colon character:
It is often used for parsing data from log files, csv files, and similar. Given a firewall's log file, for example, cut
and sort -u
can be used to create a sorted list of unique ip addresses. Add in grep
to get a strong tool for carving out data. The following lines show how to extract data from a line of text.
Example below:
The -d
flag sets the delimiter, space in this case, and the -f
flag shows which column to return, . The column count starts at .
In the next command, output from the first command is piped to a second command where the delimiter is a period and the column is . Finally, cut
is used to extract the first character from the results of the second command.
The above is for command line inputs.
HackerRank Task : Read N lines of input, then output the 3rd character of each input. (etc. Input = character, output = a)
No quote vs Double Quotes
When double quotes are used white space (tab space is a form of white space) is preserved. When double quotes aren't used tabspace isn't preserved. cut by default uses tabspace as a delimiter to extract the fields. If no tabspaces are present (and if delimiter option of cut hasn't been modified) then cut wont work.
2. IFS (Internal Field Separator)
It is used by the shell to determine how to do word splitting, i. e. how to recognize word boundaries. The default value for IFS
consists of whitespace characters (to be precise: space, tab and newline). Each character can be a word boundary.
Used to display first few lines of a file or last few lines of file
Translate offers various ways that the tr command may be used for transforming or translating characters or character classes. It can also be used to delete, complement, remove or squeeze characters
-c : complements the set of characters in string.i.e., operations apply to characters not in the given set
-d : delete characters in the first set from the output.
-s : replaces repeated characters listed in the set1 with single occurrence
-t : truncates set1
For example: 'e' being transformed to 'E'
Spaces being transformed to hyphens
Digits (numerals) being deleted
Deleting lowercases a-z
Replace multiple spaces with single space