I am looking for a command to remove first 2 columns from the first row of a file. The delimiter for the columns is a pipe. Number of columns vary.

we just need to blindly remove first 2 columns. sample below.

input: col1|col2|col3|col4

output col3|col4

  • Could you edit your question and provide a sample of the file?
    – Erathiel
    Commented May 21, 2015 at 17:35
  • How many columns does the file have? Should the two 1st fields be made blank (so foo|bar|baz becomes ||baz) or should the output line only have N-2 columns (so that foo|bar|baz becomes baz)? Please edit your question and include an example of your input and desired output.
    – terdon
    Commented May 21, 2015 at 18:05
  • 1
    @sunshine737 - unless you are the OP how do you know what the sample input and desired output look like ? Commented May 21, 2015 at 19:15

4 Answers 4


Probably the simplest command is

sed '1s/^[^|]*|[^|]*|//g' input > output

This sed command will only on line 1 do the substitution.

For this input:


it will produce that output:


try this command

head -2 inputFile.txt | cut -d "|" -f 3- > outputFile.txt

The cut command should do the job if I guess at what you are referring to.

echo "foo|bar|baz" | cut -d"|" -f3

So if you are only wanting to edit the first row then use the head command as well.

head -n1 filename | cut -d"|" -f3

You could also use sed to do the job.

head -n1 filename | sed 's/^.*|.*|//g'

This will cut out the first 2 pipe-delimited fields of line 1, and copy the rest of the file (line 2 onward) without changes.

(head -n 1 input | cut -d '|' -f 3- ; tail -n +2 input) > output

A slightly more efficient but syntactically trickier way to do it is:

{ head -n 1 input | cut -d '|' -f 3- ; tail -n +2 input; } > output
  • 1
    With {..} you could golf it shorter: { head -n 1 | cut -d '|' -f 3-; cat; } <infile >outfile Commented May 21, 2015 at 19:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .