Issue
I am trying to add a column with a header "DATE" and the value equal to the date "DD-MM-YY". The issue I am having is that the code I run adds the "DATE" and "DD-MM-YY values to the second column of a new row instead of appending to the existing rows in the data.
I've tried using both awk and sed, but both produce the same results. Below is my current code, my original data, the data resulting from my code, and my desired dataset.
Thanks in advance for your help!
for y in 2021
do
for m in 02
do
for d in 01
do
awk -v d=$y-$m-$d -F"|" 'BEGIN {OFS = "|"} FNR==1{$(NF+1)="DATE"} FNR>1{$(NF+1)=d;} 1' Corporate_bond_mstr-$y-$m-$d.txt > check.txt
done
done
done
and
sed -e '1s/$/|DATE/' -e "2,\$s/$/|$y-$m-$d/" -e 's/\>|/ |/g' Corporate_bond_mstr-$y-$m-$d.txt > changed_$y-$m-$d.csv | column -s '|' -t
ORIGINAL DATA
FINRA_SCRTY_ID|CUSIP_ID|SYM_CD|CMPNY_NM|SUB_PRDCT_TYPE_CD
3698282|000336AC1|ANTM3698282|AAG HLDG INC|CORP|CORP
3698282|000336AC1|ANTM3698282|AAG HLDG INC|CORP|BOND
3698282|000336AC1|ANTM3698282|AAG HLDG INC|CORP|GOVT
DATA AFTER RUNNING CODE
FINRA_SCRTY_ID|CUSIP_ID|SYM_CD|CMPNY_NM|SUB_PRDCT_TYPE_CD
|DATE
3698282|000336AC1|ANTM3698282|AAG HLDG INC|CORP|CORP
|DD-MM-YY
3698282|000336AC1|ANTM3698282|AAG HLDG INC|CORP|BOND
|DD-MM-YY
3698282|000336AC1|ANTM3698282|AAG HLDG INC|CORP|GOVT
|DD-MM-YY
DESIRED DATA
FINRA_SCRTY_ID|CUSIP_ID|SYM_CD|CMPNY_NM|SUB_PRDCT_TYPE_CD|DATE
3698282|000336AC1|ANTM3698282|AAG HLDG INC|CORP|CORP|DD-MM-YY
3698282|000336AC1|ANTM3698282|AAG HLDG INC|CORP|BOND|DD-MM-YY
3698282|000336AC1|ANTM3698282|AAG HLDG INC|CORP|GOVT|DD-MM-YY
Solution
With GNU sed this will produce the desired data:
y=YY m=MM d=DD
sed -e '1s/[ ]*$/|DATE/' \
-e '2,$s/[ ]*$/|'"$d-$m-$y"'/' data
Perhaps the column
command is tricking you; try running without.
Answered By - urcodebetterznow