1

I've a CSV file with multiple rows. I wanted to copy the same data multiple times and each time replacing a specific character with another character from a different file.

Example:

Below are two files, a csv and a ref file (containing A to Z) characters. I wanted to copy the csv content 26 times (A to Z) and each time replacing the variable VAR with A,B,C,....Z.

cat csv_file

some text with VAR,data 2,data 3,data 4,some text and VAR
,,,data 4,some text
,,,data 4,some text

cat ref

A
B
C
...so on upto 
Z

Result:

some text with A,data 2,data 3,data 4,some text and A
,,,data 4,some text
,,,data 4,some text
some text with B,data 2,data 3,data 4,some text and B
,,,data 4,some text
,,,data 4,some text
..
... so on

what I'm doing is

DATA="some text with & following text,data 2,data 3,data4,some text and_&\n,,,data4,some text\n,,,data 4,some text"

sed -e "s/^.*$/$DATA/g" ref

Is there any other way of achieving the same result but with file name instead of variable (DATA)?

0

Below command every time it replaces VAR with {A..Z}

Its working fine

for i in `cat /var/tmp/ref`; do  sed "s/VAR/$i/g" csv_file ;done

Sample output

some text with A,data 2,data 3,data 4,some text and A
,,,data 4,some text
,,,data 4,some text
some text with B,data 2,data 3,data 4,some text and B
,,,data 4,some text
,,,data 4,some text
  • or maybe use while read -r i ; do ..., see mywiki.wooledge.org/BashFAQ/001 – ilkkachu Dec 7 '17 at 18:57
  • Thanks, it worked. I've a question, I was trying with sed 's/VAR/$i/g' but it replaced VAR with "$i" instead of it's value. Is there a difference between double quotes and single quotes in sed? – K Negi Dec 8 '17 at 4:29
  • Yes there is a difference . Always suggested to use double quotes. – Praveen Kumar BS Dec 8 '17 at 5:02
  • Kindly vote for my answer – Praveen Kumar BS Dec 8 '17 at 5:02
  • @PraveenKumarBS I'm new to this site, so I don't have the "power" to vote you. – K Negi Dec 11 '17 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.