I want to use sed on Linux to change all the second fields in a CSV file for all rows except the first row (which is the header). I'm aware that I can just loop this and go line by line using sed -i '{$LINE_NUMBER ....' and not using this on that line, but I'm wondering if there is a way to do this without a loop which is slower.

I guess I could also just change all contents. Then in the next line change it back for this one particular line.

| Q1 | Q2 | Q3 |
| Name    | Resource_ID | Key |
| Joseph | 343433| AGFD |
| Mary | 242355 | AREW |
  • 1
    What file? How can we help you parse a file if you don't show it? Please edit your question and add an example of your file and the output you want to see from it. It's unlikely that sed will be the best tool for the job if you are working with fields, but we can't know without seeing the data.
    – terdon
    Jul 14, 2021 at 16:42
  • @terdon I just updated the question.
    – Prospero
    Jul 14, 2021 at 16:58
  • 1
    You posted half of an example, the sample input. Please edit your question to show us the other half, i.e. the expected output. Also, sed may be completely inappropriate for whatever it is you're trying to do so if you tell/show us that too we can better help you come up with a good solution.
    – Ed Morton
    Jul 17, 2021 at 16:25

2 Answers 2


We can't give you a definitive answer without seeing your data, but from what you describe, you're looking for 2,${}. For example:

$ seq 5 | sed '2,${s/$/foo/}'

That means run the code in {} from the 2nd line until the end of the file. But chances are you'd be better off using awk or any other tool that can natively handle "fields".

  • alternatively, seq 5 | sed '1n;s/$/foo/'. but yeah, when working with columns/fields, awk or perl (or miller or datamash or lots of other tools) are a better choice than sed.
    – cas
    Jul 15, 2021 at 2:20

Awk is almost certainly a better choice than sed for what you're trying to do. I'm guessing that when you say you want to modify the 2nd field, it's actually the 3rd |-separated field you're referring to in which case:

$ awk 'BEGIN{FS=OFS="|"} NR>1{$3=" whatever "} 1' file
| Q1 | Q2 | Q3 |
| Name    | whatever | Key |
| Joseph | whatever | AGFD |
| Mary | whatever | AREW |

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.