Solved

Sed in a while read loop wont replace pattern stored in a variable

Posted on 2008-06-20
3
1,134 Views
Last Modified: 2013-12-26
I am using sed to match patterns in csv File. The csv file is as follows:

 "VALUE";"VALUE";"VALUE";"VALUE";.......

to replace a value i use:

sed '2,4621s/[^;]*;/"'"$VALUE"'";/11' < file.csv

this works pretty fine if there is only one value given lets say:

VALUE=TEST

sed '2,4621s/[^;]*;/"'"$VALUE"'";/11' < file.csv

this replaces the 11th VALUE in every line from 2 to 4621 with TEST

but now i have a file2  where other values are stored. It's stored in this format

VALUE
VALUE
VALUE
VALUE


Now i want to read the values from the file above and replace it in the csv file

i tried this:

while read VALUE
do


        V=`sed '2,4621s/[^;]*;/"'"$VALUE"'";/11' < file.csv`

        echo $V > new_file.csv


done<file2<<EOF

but somehow this outputs only empty space on the place where the values of file2 should be inserted


 "VALUE";"VALUE";"VALUE";"EMPTY SPACE";"VALUE";.......

I tried many things and played around but without luck

i am using ubuntu hardy heron....

thx alot


 

0
Comment
Question by:Jelcin
3 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 21832811
I don't understand what you are trying to do
each time you read VALUE
new_file.csv gets replaced by the new $V
so all but the last VALUE gets discarded
is that what you meant?
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 21836467
I would suggest not using sed but awk instead. Since your csv file is structured, use fields to manipulate your data. will be easier than using regexp. I am assuming your 2 to 4621 is line 2 until line 4621. And i am assuming you want to insert the values of file2 into column 11 onwards.
awk 'BEGIN{OFS=FS=","}
FNR==NR {
 a[++d]=$0
 next
}
FNR>2 && FNR <= 4621{
  for(i=1;i<=11;i++ ) { printf "%s,", $i }
  for(j=1;j<=d;j++ ) { printf "%s,",a[j] }
  for(i=12;i<=NF;i++ ) { 
    if ( i== NF ) {printf "%s", $i}
    else { printf "%s,", $i  }
  }
  print ""
}
' file2 file

Open in new window

0
 
LVL 6

Accepted Solution

by:
Jelcin earned 0 total points
ID: 21851946

while read VALUE
do

        cat file.csv | head -$COUNTER | tail -1 | sed  's/[^;]*;/"'"$VALUE"'"';/11;' >> new_file.csv


        let COUNTER=COUNTER+1

done<file2

maybe not the best solution but it works for me

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question