Solved

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

Posted on 2008-06-20
3
1,135 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…

752 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