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?

4
  • Whichever of the n solutions that you would be able to use, each would probably ask you to create a new file. Commented Dec 17, 2016 at 16:44
  • I want to create a new csv file after adding columns. Commented Dec 17, 2016 at 16:48
  • 1
    paste -d ',' file1 file2 > file3 Commented Dec 17, 2016 at 16:54
  • You haven't mentioned if there is an entry in file2 corresponding to each entry in file1 Commented Dec 17, 2016 at 16:58

3 Answers 3

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

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

Comments

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 ].

Comments

0

another sed alternative

$ sed 's/$/,'"$(cat file2)"'/' file1

Comments

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.