Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using cut with unprintable delimiters

Tags:

Is it possible to use cut and have unprintable characters be the delimiter? For example I'd like to have the "^A" characters (also represented as \001) be the delimiter.

like image 444
pacman Avatar asked Apr 24 '09 10:04

pacman


People also ask

How do I specify delimiter in cut?

cut uses tab as a default field delimiter but can also work with other delimiter by using -d option. Note: Space is not considered as delimiter in UNIX.

How do you use multiple delimiters in cut command?

Use 'tr' to change each of the delimiters to a common delimiter. That way 'cut' only has to deal with one delimiter. Use multiple 'cut's, each with a different delimiter, in a pipeline to get at only the data you want. This can be hard if the delimiters are mixed in the data.

What is the default delimiter for cut?

The tab character is the default delimiter of cut, so by default, it considers a field to be anything delimited by a tab. Remember that the "space" between each word is actually a single tab character, so both lines of output are displaying ten characters: eight alphanumeric characters and two tab characters.

How do you make a cut command more specific?

The following command line options are used by the cut command to make it more specific: -b, --bytes=LIST: It is used to cut a specific section by bytes. -c, --characters=LIST: It is used to select the specified characters. -d, --delimiter=DELIM: It is used to cut a specific section by a delimiter.


2 Answers

If you're using Bash,

cut -d $'\001' ... 

works (see Bash Reference Manual # 3.1.2.4 ANSI-C Quoting).

Other (more portable) options,

cut -d `echo -e '\001'` ...  FS=`echo -e '\001'` cut -d $FS ... 

or inserting the control character directly using ^V as mentioned by Alnitak and etlerant -- on the shell command line, and in editors such as vi, this means "don't treat the next thing I type specially".

like image 153
ephemient Avatar answered Oct 05 '22 23:10

ephemient


Yes, it's perfectly possible.

If typing in a shell, press ^V and then ^A to insert the ^A verbatim in the current line rather than have it treated as the normal 'go to start of line' command:

% cat -v foo abc^Adef^Aghi % cut -d^A -f2 foo def 
like image 23
Alnitak Avatar answered Oct 06 '22 01:10

Alnitak