0

I am pretty new in BASH scripting. I have a csv file with 2 columns separated by comma.

ASDP01,01989015064
KSDP03,01988683270
KSDP06,01945993069
CSDP11,01990721863
CSDP13,01955883155
ASDP12,01953889744
CSDP11,01956798684
ASDP11,01959969994
KSDP01,01924824056

I want to add 2 more columns from another text file. this is written in the text file:

662,2016-12-31

after adding from the tex file, the csv file will be like below

ASDP01,01989015064,662,2016-12-31
KSDP03,01988683270,662,2016-12-31
KSDP06,01945993069,662,2016-12-31
CSDP11,01990721863,662,2016-12-31
CSDP13,01955883155,662,2016-12-31
ASDP12,01953889744,662,2016-12-31
CSDP11,01956798684,662,2016-12-31
ASDP11,01959969994,662,2016-12-31
KSDP01,01924824056,662,2016-12-31

can anyone help me with this?

James Brown
  • 31,411
  • 6
  • 31
  • 52

3 Answers3

1

I assume file2.csv contains only one row with 662,2016-12-31.

string="$(cat file2.csv)"
sed 's/.*/&,'"$string"'/' file1.csv > new.csv

Output to new.csv:

ASDP01,01989015064,662,2016-12-31
KSDP03,01988683270,662,2016-12-31
KSDP06,01945993069,662,2016-12-31
CSDP11,01990721863,662,2016-12-31
CSDP13,01955883155,662,2016-12-31
ASDP12,01953889744,662,2016-12-31
CSDP11,01956798684,662,2016-12-31
ASDP11,01959969994,662,2016-12-31
KSDP01,01924824056,662,2016-12-31

See: The Stack Overflow Regular Expressions FAQ

Community
  • 1
  • 1
Cyrus
  • 69,405
  • 13
  • 65
  • 117
0

You can accomplish the results using awk too.

#cat file1
KSDP01,01989015064
KSDP03,01988683270
KSDP06,01945993069
CSDP11,01990721863
CSDP13,01955883155
ASDP12,01953889744
CSDP11,01956798684
ASDP11,01959969994
KSDP01,01924824056

#cat file2
662,2016-12-31

#awk -v f2content="$(<file2)" '{$0=($0 "," f2content)}1' file1
KSDP01,01989015064,662,2016-12-31
KSDP03,01988683270,662,2016-12-31
KSDP06,01945993069,662,2016-12-31
CSDP11,01990721863,662,2016-12-31
CSDP13,01955883155,662,2016-12-31
ASDP12,01953889744,662,2016-12-31
CSDP11,01956798684,662,2016-12-31
ASDP11,01959969994,662,2016-12-31
KSDP01,01924824056,662,2016-12-31

See [ awk concatenation ].

sjsam
  • 19,686
  • 3
  • 43
  • 88
0

another sed alternative

$ sed 's/$/,'"$(cat file2)"'/' file1
karakfa
  • 62,998
  • 7
  • 34
  • 47