0

I have an csv file that looks like:

id, name, nationality
1, Maria, ESP
2, John, NED
3, Alice, DEN

I am building a document script.sed that executes different sed expressions like this:

s/ESP/Spain/g

s/DEN/Denmark/g

s/NED/Netherlands/g

I run the sed script through:

sed -f script.sed "file.csv"

The expressions above work as I expected, however I need to include the csv headers in the output. Is there any way to print them in a sed expression and introduce it in the script.sed file?

I have this output:

1, Maria, Spain
2, John, Netherlands
3, Alice, Denmark

but I need this output:

id, name, nationality
1, Maria, Spain
2, John, Netherlands
3, Alice, Denmark

Thank you

2
  • 1
    Are the headers in the original document? If so, I don't see anything in what you've posted that would explain why they wouldn't also be in the output. If they're not in the input, I suppose 1s/^/id, name, nationality\n/ would add them. (Be sure to have the 1 at the beginning.)
    – frabjous
    Apr 26 at 23:31
  • This is not reproducible. The code presented is correct. Very probably it is a data issue, the input file contains non-Unix line endings, but there was no update about this.
    – thanasisp
    May 5 at 10:04

1 Answer 1

1

Add the p (print) command at the end of your 'script.sed' file

the p command instructs sed to unconditionally print out the input line .

1
  • The sed command in the description is already printing all the lines. Adding p anywhere could print lines twice. And to print some lines only once, p should be combined with -n.
    – thanasisp
    May 5 at 10:07

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