I'm suggesting a small and simple addition to `cut` options.
Sometimes it is very useful to add some text around each line of `cut`
output in a pipeline. Most people use `sed` or something as big as `sed`,
but it would be much better if you can add this text directly in cut
options. Furthermore, `cut` would be suitable for simple concatenating
of strings around any input - for example output from `expr match`.
I suggest these options to be added:
-A, --output-before=STRING output STRING before each line
-Z, --output-after=STRING output STRING after each line
As a source I used `cut.c` from
ftp://alpha.gnu.org/gnu/coreutils/coreutils-5.3.0.tar.gz
, and I'm supplying the unified diff below.
Here is an example of a pipeline, that I recently used with patched `cut`:
ls -1|cut -d '.' -f 1 -A 'INSERT INTO mytable(file) VALUES("' -Z '");'
, so the output is SQL, suitable for direct execution. Here is the diff:
--- cut.5.3.0.c 2005-05-07 17:32:35.000000000 +0300
+++ cut.5.3.0.c.patched 2005-05-07 17:49:22.000000000 +0300
@@ -146,6 +146,12 @@
/* True if we have ever read standard input. */
static bool have_read_stdin;
+/* The before and after result parts in output. */
+static char *output_before_string;
+static bool output_before_specified;
+static char *output_after_string;
+static bool output_after_specified;
+
#define HT_RANGE_START_INDEX_INITIAL_CAPACITY 31
/* The set of range-start indices. For example, given a range-spec list like
@@ -172,6 +178,8 @@
{"only-delimited", no_argument, 0, 's'},
{"output-delimiter", required_argument, 0, OUTPUT_DELIMITER_OPTION},
{"complement", no_argument, 0, COMPLEMENT_OPTION},
+ {"output-before", required_argument, 0, 'A'},
+ {"output-after", required_argument, 0, 'Z'},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{0, 0, 0, 0}
@@ -212,6 +220,10 @@
or fields.\n\
"), stdout);
fputs (_("\
+ -A, --output-before=STRING output STRING before each line\n\
+ -Z, --output-after=STRING output STRING after each line\n\
+"), stdout);
+ fputs (_("\
-s, --only-delimited do not print lines not containing delimiters\n\
--output-delimiter=STRING use STRING as the output delimiter\n\
the default is to use the input delimiter\n\
@@ -543,6 +555,11 @@
if (c == '\n')
{
+ if(output_after_specified) {
+ fwrite (output_after_string, sizeof (char),
+ strlen(output_after_string), stdout)
+ ;
+ }
putchar ('\n');
byte_idx = 0;
print_delimiter = false;
@@ -557,6 +574,13 @@
{
bool range_start;
bool *rs = output_delimiter_specified ? &range_start : NULL;
+ if(byte_idx==0) {
+ if(output_before_specified) {
+ fwrite (output_before_string, sizeof (char),
+ strlen(output_before_string), stdout)
+ ;
+ }
+ }
if (print_kth (++byte_idx, rs))
{
if (rs && *rs && print_delimiter)
@@ -580,6 +604,7 @@
size_t field_idx = 1;
bool found_any_selected_field = false;
bool buffer_first_field;
+ bool output_before = false;
c = getc (stream);
if (c == EOF)
@@ -626,9 +651,24 @@
}
else
{
+ if(output_before_specified) {
+ fwrite (output_before_string, sizeof (char),
+ strlen(output_before_string), stdout)
+ ;
+ output_before=true;
+ }
+ if (field_1_buffer[n_bytes - 1] != '\n')
fwrite (field_1_buffer, sizeof (char), n_bytes, stdout);
+ else
+ fwrite (field_1_buffer, sizeof (char), n_bytes-1,
stdout)
+ ;
+ if(output_after_specified) {
+ fwrite (output_after_string, sizeof (char),
+ strlen(output_after_string), stdout)
+ ;
+ }
/* Make sure the output line is newline terminated. */
- if (field_1_buffer[n_bytes - 1] != '\n')
+ /* if (field_1_buffer[n_bytes - 1] != '\n') */ /* newline removed anyway
*/
putchar ('\n');
}
continue;
@@ -636,6 +676,12 @@
if (print_kth (1, NULL))
{
/* Print the field, but not the trailing delimiter. */
+ if(output_before_specified) {
+ fwrite (output_before_string, sizeof
(char),
+ strlen(output_before_string),
stdout)
+ ;
+ output_before=true;
+ }
fwrite (field_1_buffer, sizeof (char), n_bytes - 1, stdout);
found_any_selected_field = true;
}
@@ -644,6 +690,15 @@
if (c != EOF)
{
+ if (found_any_selected_field
+ || !(suppress_non_delimited && field_idx == 1)) {
+
if(output_before_specified&&(!output_before)) {
+ fwrite (output_before_string,
sizeof (char),
+
strlen(output_before_string), stdout)
+ ;
+ output_before=true;
+ }
+ }
if (print_kth (field_idx, NULL))
{
if (found_any_selected_field)
@@ -682,8 +737,15 @@
else if (c == '\n' || c == EOF)
{
if (found_any_selected_field
- || !(suppress_non_delimited && field_idx == 1))
+ || !(suppress_non_delimited && field_idx == 1)) {
+ if(output_after_specified) {
+ fwrite (output_after_string, sizeof (char),
+ strlen(output_after_string), stdout)
+ ;
+ }
+ output_before=false;
putchar ('\n');
+ }
if (c == EOF)
break;
field_idx = 1;
@@ -765,7 +827,7 @@
delim = '\0';
have_read_stdin = false;
- while ((optc = getopt_long (argc, argv, "b:c:d:f:ns", longopts, NULL)) != -1)
+ while ((optc = getopt_long (argc, argv, "A:Z:b:c:d:f:ns", longopts, NULL))
!= -1)
{
switch (optc)
{
@@ -785,7 +847,19 @@
operating_mode = field_mode;
spec_list_string = optarg;
break;
-
+
+ case 'A':
+ /* output this before the result */
+ output_before_string = xstrdup (optarg);
+ output_before_specified=true;
+ break;
+
+ case 'Z':
+ /* output this after the result */
+ output_after_string = xstrdup (optarg);
+ output_after_specified=true;
+ break;
+
case 'd':
/* New delimiter. */
/* Interpret -d '' to mean `use the NUL byte as the delimiter.' */
--
Dimo Bozduganov
DVR Review
_______________________________________________
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils