Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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 video discusses moving either the default database or any database to a new volume.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now