I am working on an enhancement of a shell script which reads a file and processes it. Basically the input file contains a header record followed by number of detail records. I want to get only the header record from the file.
$ cat sample_file.txt header1,header2,header3,header4 value1,value2,value3,value4
The existing script uses the following command to get the header from the file:
$ cat sample_file.txt | head -1 | egrep -o '[[:print:]]' | tr '\n' '\0' header1,header2,header3,header4$
I am not sure what
egrep -o '[[:print:]]' do here. Because even without the
egrep the command could have been just put like this
To print the header as it is
$ cat sample_file.txt | head -1 header1,header2,header3,header4
Or to print the header without a new line at the end
$ cat sample_file.txt | head -1 | tr '\n' '\0' header1,header2,header3,header4$
The man page of
egrep tells the below but it is not clear as to when
[[:print:]] should be used.
Finally, certain named classes of characters are predefined within bracket expressions, as follows. Their names are self explanatory, and they are [:alnum:], [:alpha:], [:cntrl:], [:digit:], [:graph:], [:lower:], [:print:], [:punct:], [:space:], [:upper:], and [:xdigit:]. For example, [[:alnum:]] means [0-9A-Za-z], except the latter form depends upon the C locale and the ASCII character encoding, whereas the former is indepen- dent of locale and character set. (Note that the brackets in these class names are part of the symbolic names, and must be included in addition to the brackets delimiting the bracket list.) Most metacharacters lose their special meaning inside lists. To include a literal ] place it first in the list. Similarly, to include a literal ^ place it anywhere but first. Finally, to include a literal - place it last.
Can you please help me to understand the usage of
egrep '[[:print:]]' option and where do we use the same.