Skip to main content
deleted 53 characters in body
Source Link
enzotib
  • 53.4k
  • 14
  • 126
  • 106

It would be simple, if all fruits were a single word ("unknown fruit" beingSupposing the anomaly). Substitutingfields are separated by one or more tabs, and that with a spaceless word:each field can contain spaces but not tabs, then the following should work

enzotib@acer:~$( sedIFS=$'\t'; -iprintf 's/unknown'%s fruit/unknown_fruit/'%s input_file%s\n' $(<input_file) )

you can simply(the subshell is convenient to not change IFS for the calling shell).

Alternatively, if fields are separated by one or more spaces and tabs, and that each field do: not contain spaces nor tabs, then the following should work

enzotib@acer:~$ printf '%s %-13s%s %s\n' $(<input_file)
0637 apple         8528
1392 orange        1390
8528 unknown_fruit 7537
1039 banana        1892
0989 pineapple     7537
8528 melon         7537
8528 grape         7537
8528 (null)        (null)

It would be simple, if all fruits were a single word ("unknown fruit" being the anomaly). Substituting that with a spaceless word:

enzotib@acer:~$ sed -i 's/unknown fruit/unknown_fruit/' input_file

you can simply do:

enzotib@acer:~$ printf '%s %-13s %s\n' $(<input_file)
0637 apple         8528
1392 orange        1390
8528 unknown_fruit 7537
1039 banana        1892
0989 pineapple     7537
8528 melon         7537
8528 grape         7537
8528 (null)        (null)

Supposing the fields are separated by one or more tabs, and that each field can contain spaces but not tabs, then the following should work

( IFS=$'\t'; printf '%s %s %s\n' $(<input_file) )

(the subshell is convenient to not change IFS for the calling shell).

Alternatively, if fields are separated by one or more spaces and tabs, and that each field do not contain spaces nor tabs, then the following should work

printf '%s %s %s\n' $(<input_file)
Source Link
enzotib
  • 53.4k
  • 14
  • 126
  • 106

It would be simple, if all fruits were a single word ("unknown fruit" being the anomaly). Substituting that with a spaceless word:

enzotib@acer:~$ sed -i 's/unknown fruit/unknown_fruit/' input_file

you can simply do:

enzotib@acer:~$ printf '%s %-13s %s\n' $(<input_file)
0637 apple         8528
1392 orange        1390
8528 unknown_fruit 7537
1039 banana        1892
0989 pineapple     7537
8528 melon         7537
8528 grape         7537
8528 (null)        (null)