UNIX help adding data to end of a line with a variable

I am trying to add a date variable to the end of each line. This is what I have to start with

cat ${DATAPATH}/Participate_Stream${STREAMDATE}.dryak1.csv | grep ^',' | awk '{print $0}' >> ${DATAPATH}/badparticipant.csv

This is what I tried $DATE is a variable I have defined.

cat ${DATAPATH}/Participate_Stream${STREAMDATE}.dryak1.csv | grep ^',' | awk '{print $0 $DATE}' >> ${DATAPATH}/badparticipant.csv

Any suggestions?

Welcome to the forum.

Thanks for sharing your approach. Some comments on it:

  • Please supply minimum context info like OS and shell version, sample input, and desired output; or even more if applicable and helpful.
  • cat is not needed - grep or awk or many other commands do open and read files by themselves.
  • awk has great pattern matching capabilities - no grep piping into it needed.
  • shell variables ($DATE) are NEVER expanded within single quotes - use different mechanisms.
  • It's good habit to deploy the tool's of choice ( awk ) mechanisms - read the man page on the -v option for parameter passing.

Not sure what the ^',' pattern stands for and how it is being interpreted, try this modified version of your command pipe, if it comes close to what you need:

awk -v DT="$DATE" '/^,/ {print $0, DT}' ${DATAPATH}/Participate_Stream${STREAMDATE}.dryak1.csv >> ${DATAPATH}/badparticipant.csv

The grep ^',' is looking for a comma in the first position of the file I have. If that is true then I want to Print the record and add the DATE variable I have created.

It would look something like this.

 ,John Smith,somewhere,blue,outside,20170405

That got me very close. I had to tweak it a bit because of my file.