3

I am sill new in this. I would like to ask, how to inverse the cut?

Example;

./24feb/frfr

I want after cut command, the result will be ./feb/frfr.

How to do it?

  • 2
    welcome to U&L, I reformat your question, yet it remains unclear, cut basically remove column of chat, what do you intend to do ? remove "24" ? remove 3th et 4th char ? – Archemar Oct 19 '15 at 14:33
8
% echo ./24feb/frfr | cut -c 1-2,5-
./feb/frfr

That would be the inverse of cut -c 3-4, that is outputs all characters (bytes with current versions of GNU cut) of each line except the 3rd and 4th.

The GNU implementation of cut also has a --complement option for that:

cut --complement -c 3-4

To remove the first sequence of decimal digits, you can use sed instead:

sed 's/[0-9]\{1,\}//'

To remove it, only if it's in 3rd position:

sed 's/^\(..\)[0-9]*/\1/'

Or to be very explicit on what pattern should trigger the removal:

sed 's|^\(./\)[0-9]*\([[:lower:]]\{3\}/\)|\1\2|'

That is only removed the <0-or-more-digits> in a line matching: ./<0-or-more-digits><3-lowercase-letters>/<anything>.

| improve this answer | |
  • The problem is we don't know how many digits are used for date. If there is other input (example ./9feb/frfr) then cut doesn't work. – Jesse K Oct 19 '15 at 14:47
  • @JesseKeilson, that's your interpretation, the subject in the OP's question suggests he wants the inverse of cut -c 3-4 (then again, it's my interpretation, the question is not very clear). – Stéphane Chazelas Oct 19 '15 at 14:48
  • Agreed, your answer is technically correct, but the question is unclear... my my, your edits are comprehensive! – Jesse K Oct 19 '15 at 14:57
1

That's not possible with cut. Cut relies on the availability of a field separator. In your example desired input, there's no character that could be used as a field separator. Unless you have more control over your input format, cut is not the tool for the job. This could be achieved with sed or awk. For example, In sed it would be:

sed 's/[0-9]//g'

| improve this answer | |
  • That would be the equivalent of tr -d 0-9, that is, it would remove all the decimal digits, which doesn't sound like what was asked for. – Stéphane Chazelas Oct 19 '15 at 14:56
0

As a complement to the accepted answer above, you could combine cut and sed subsitution into a single command using command substitution.

This only works if you know the character positions in the name you would like to remove, or can figure those values out problematically.

name="./24feb/frfr"
cut_start=3
cut_end=4

echo "${name}" | sed "s/$(echo "${name}" | cut -c${cut_start}-${cut_end})//"

./feb/frfr

As a side note...typically the ' is used for sed but in this case you should use " to allow the command substitution to be expanded.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.