2

The input.csv is:

id, name, shortName
1, Example1, Ex1
2, Example2, Ex2

I need to add a column of date. So the output.csv should be:

id, 2021-06-23, name, shortName
1,  2021-06-23, Example1, Ex1
2,  2021-06-23, Example2, Ex2.

For this purpose, i used this comand:

awk -F"," 'BEGIN { FS=OFS = "," } {$2="2021-06-23"; print}' input.csv 

But, when i did it the column "name" was deleted. The output was like this:

id, 2021-06-23, shortName
1,  2021-06-23, Ex1
2,  2021-06-23, Ex2.

Then, i also tried put the colum at the end with the comand:

awk -F"," 'BEGIN { FS=OFS = "," } {$4="2021-06-23"; print}' input.csv 

But the exit was even stranger:

,  2021-06-23 
,  2021-06-23
,  2021-06-23

So, I would like to know what I'm doing wrong both in the case of adding a column to the middle of the csv file and adding it to the end.

Obs.: I am now learning how to use it and therefore I believe that my mistakes must be a beginner's mistakes.

3
  • 1
    Your source file probably has Windows line endings (\r\n) and therefore the latter output is a mess. RS="\r\n" probably fixes that. To get the expected output, $2="2021-06-23" OFS $2 should work. Commented Jun 23, 2021 at 7:53
  • @JamesBrown, don´t work Commented Jun 24, 2021 at 21:18
  • Please don't tell me just don't work, be more specific. Commented Jun 25, 2021 at 9:41

1 Answer 1

2

This

awk -F"," 'BEGIN { FS=OFS = "," } {$2="2021-06-23"; print}' input.csv

does overwrite 2nd column using 2021-06-23. Note also that this code is redundant - you are setting field seperator twice, once using -F then setting FS in BEGIN, code above has same effect as

awk 'BEGIN { FS=OFS = "," } {$2="2021-06-23"; print}' input.csv

You want to add column, if you know in advance number of columns in your .csv file you might do, for 3 columns:

awk 'BEGIN{FS=OFS=", "}{print $1,"2021-06-23",$2,$3}' input.csv

which for input.csv content being

id, name, shortName
1, Example1, Ex1
2, Example2, Ex2

output

id, 2021-06-23, name, shortName
1, 2021-06-23, Example1, Ex1
2, 2021-06-23, Example2, Ex2

(tested in gawk 4.2.1)

Sign up to request clarification or add additional context in comments.

2 Comments

If I have a large number of columns, is there any way to just specify where to insert? Because "{print $1,"2021-06-23",$2,$3}" in this part of the code I have to put the numbering of the other columns.
@Lucas if you want to add new column value after certain column, let say 1st then do {$1=$1 OFS "2021-06-23";print} it does replace content of 1st column with content of 1st column followed by output field separator followed by 2021-06-23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.