Text Processing – A Unix Command to Truncate Each Line of a File

csvkshtext processing

I have a CSV file and I want to truncate it from the third semicolon. For example, if I have this file:

1;foo;bar;baz;x;y;z
2;foo;bar;baz;x;y;z
3;foo;bar;baz;x;y;z

I want to get the following output:

1;foo;bar
2;foo;bar
3;foo;bar

I don't know what kind of Unix command I can use for that. What do you suggest?

Note that this manipulation will be done on a KSH script.

Best Answer

For the sake of variety, here's another way with cut:

cut -d \; -f -3