Solved

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

Posted on 2008-06-20
3
1,130 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

809 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