Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-06-20
3
Medium Priority
?
1,137 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
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…
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…

722 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