简体   繁体   English

使用 awk 或 sed 将带有 header 的列添加到管道分隔文件

[英]Adding column with header to pipe-delimited file using awk or sed

I am trying to add a column with a header "DATE" and the value equal to the date "DD-MM-YY".我正在尝试添加一个带有 header“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.我遇到的问题是我运行的代码将“DATE”和“DD-MM-YY”值添加到新行的第二列,而不是附加到数据中的现有行。

I've tried using both awk and sed, but both produce the same results.我试过同时使用 awk 和 sed,但都产生相同的结果。 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   

With GNU sed this will produce the desired data:使用 GNU sed 这将产生所需的数据:

y=YY m=MM d=DD
sed -e '1s/[ ]*$/|DATE/' \
    -e '2,$s/[ ]*$/|'"$d-$m-$y"'/' data

Perhaps the column command is tricking you;也许column命令在欺骗你; try running without.尝试不运行。

I needed to get rid of carriage returns in the text file for the code to run correctly.我需要去掉文本文件中的回车,以使代码正确运行。

sed -e 's/[\r\n]//g' Corporate_bond_mstr-$y-$m-$d.txt > chg1.txt sed -e 's/[\r\n]//g' Corporate_bond_mstr-$y-$m-$d.txt > chg1.txt
sed -e '0,/|CNVRB_FL/s//|CNVRB_FL| sed -e '0,/|CNVRB_FL/s//|CNVRB_FL| /' chg1.txt > chg.txt /' chg1.txt > chg.txt
sed -e '1s/$/DATE/' -e "2,$s/$/|$y-$m-$d/" -e 's/>|/ |/g' chg.txt > Corporate_bond_mstr-$y-$m-$d.txt sed -e '1s/$/DATE/' -e "2,$s/$/|$y-$m-$d/" -e 's/>|/ |/g' chg.txt > Corporate_bond_mstr- $y-$m-$d.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM