0

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 May 21 '15 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 May 21 '15 at 18:05
  • 1
    @sunshine737 - unless you are the OP how do you know what the sample input and desired output look like ? – don_crissti May 21 '15 at 19:15
1

Probably the simplest command is

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

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

For this input:

foo|bar|baz|doo|bee|doo
123|456
789|101|112

it will produce that output:

baz|doo|bee|doo
123|456
789|101|112
0

try this command

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

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'
0

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 – don_crissti May 21 '15 at 19:28

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.