Another sed/awk search=>replace question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Another sed/awk search=>replace question
# 15  
Old 08-28-2014
Quote:
Originally Posted by junior-helper
I think it's got to be sed "s/^$i.*/$i , $ratio/"

x
Where did you put the file name?

I tried this
Code:
  sed "s/^$i.*/$i , $ratio/" <$file2mod> test0

it the script goes into an infinite loop of re-writing the file, test0.
# 16  
Old 08-28-2014
Right after the sed command...
Code:
sed "s/^$i.*/$i , $ratio/" sub_c18.csv

I copied your original command, set the $i and $ratio variables manually and played with it until it worked...

---------- Post updated at 10:02 PM ---------- Previous update was at 09:56 PM ----------

I just tried Scrutinizer's code, it also works perfectly (I should have tried it before posting though)
# 17  
Old 08-28-2014
Now when I add '-i' to do inline substitution, I still get my original error
Code:
sed -i "s/^$i.*/$i , $ratio/" sub_c18.csv

Code:
sed: 1: "sub_c18.csv": unterminated substitute pattern

Ugh...
# 18  
Old 08-28-2014
I guess I just figured out what you mean with infinite loop, because I also tested it in a loop - with the sample data (csv file with just 3 lines) - not just manually on the command line and indeed it looked like a infinite loop and I was just like Smilie, but then I realized the csv is parsed 13 times (13 items in the array).

Here you can clearly see how 61406775 is matched in the 7th round.
Code:
$ ./sub.sh 
61406774,0.553508428
61406775,a12324
61406776,0.169964852
61406774,0.553508428
61406775,a12324
61406776,0.169964852
61406774,0.553508428
61406775,a12324
61406776,0.169964852
61406774,0.553508428
61406775,a12324
61406776,0.169964852
61406774,0.553508428
61406775,a12324
61406776,0.169964852
61406774,0.553508428
61406775,a12324
61406776,0.169964852
61406774,0.553508428
61406775 , 0.324324234234
61406776,0.169964852
61406774,0.553508428
61406775,a12324
61406776,0.169964852
61406774,0.553508428
61406775,a12324
61406776,0.169964852
61406774,0.553508428
61406775,a12324
61406776,0.169964852
61406774,0.553508428
61406775,a12324
61406776,0.169964852
61406774,0.553508428
61406775,a12324
61406776,0.169964852
61406774,0.553508428
61406775,a12324
61406776,0.169964852
done
$

Note that the sed command is much faster with the -i option or even redirection into another file because the output is not printed to the (slow) stdout.

Can you post your current script?

---------- Post updated at 10:58 PM ---------- Previous update was at 10:54 PM ----------

I'd be happy if some "senior" person could confirm my assumption though.
# 19  
Old 08-28-2014
I don't understand the looping problem? Shouldn't sed scan the array and find the value the first loop?

Code:
for i in "${array[@]}"
do
  a=`grep $i sub_c11* |cut -f2 -d","`
  b=`grep $i sub_c13* |cut -f2 -d","`
  c=`grep $i sub_c7* |cut -f2 -d","`
  ratio=`echo "$a+$b+$c-$a*$b-$a*$c-$b*$c" | bc -l`
  sed -i "s/^$i.*/$i , $ratio/" sub_c18.csv
done
echo "done"

Appreciate the help btw.
# 20  
Old 08-28-2014
No, the for loop goes through the array and passes the values one by one - placed in the $i variable - to sed.
sed does the text transformation part only.

---------- Post updated at 11:29 PM ---------- Previous update was at 11:26 PM ----------

Your latest script looks good imho...
This User Gave Thanks to junior-helper For This Post:
# 21  
Old 08-29-2014
Quote:
Originally Posted by junior-helper
[..]I just tried Scrutinizer's code, it also works perfectly [..]
These two suggestions work in a similar way:
Code:
sed "s/^$i.*/$i , $ratio/"

and
Code:
sed "s/^$i *,.*/$i , $ratio/"

There is a difference however:

The second form uses the comma as a kind of "anchor" at the end of the number, so that it is anchored at start and at the end...

IF the number in the file is always 8 digits, then there is no difference between the two suggestions, BUT if there can also be 9-digit numbers or higher in the input files, then with the first one there may be false matches, which may lead to the wrong numbers being replaced..

Last edited by Scrutinizer; 08-29-2014 at 07:39 AM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

2. Shell Programming and Scripting

Search and replace is not working by sed or awk

Hi , I have one file and in this file i have one like TEST1 KEY0=AAC040R1;AAC041R1ISE;AAC041R2ISE;AAC370R1;ADR0500;ADR0600;AME245R1;AME245R2;BAP0135;BAP0300;PPINVDTD*;PPJERPTD*;PPJERPT*;PRBSUMM*;: i want to replace this line with the following line TEST1... (4 Replies)
Discussion started by: ashissau
4 Replies

3. Shell Programming and Scripting

awk/sed to search & replace data in first column

Hi All, I need help in manipulating the data in first column in a file. The sample data looks like below, Mon Jul 18 00:32:52 EDT 2011,NULL,UAT Jul 19 2011,NULL,UAT 1] All field in the file are separated by "," 2] File is having weekly data extracted from database 3] For eg.... (8 Replies)
Discussion started by: gr8_usk
8 Replies

4. Shell Programming and Scripting

How to use SED or AWK to search and replace an exact string

I have a file DS1 DDS DS I want to replace only "DS" to "DSmail.blah.com" in a lot of files. I tried sed 's/DS/DSmail.blah.com' but it changes all the lines . thanks in advance (2 Replies)
Discussion started by: gubbu
2 Replies

5. Shell Programming and Scripting

awk/sed string search and replace

Need help with either sed or awk to acheive the following file1 ----- In the amazon forest The bats eat all the time... mon tue wed they would eat berries In the tropical forest The bats eat all the time... on wed bats eat nuts In the rain forest The bats eat all the time... on... (2 Replies)
Discussion started by: jville
2 Replies

6. Shell Programming and Scripting

Bash sed search and replace question

I have several files that I need to modify using sed. I know how to do that, but now a new requirement has come up. Now, I need to make changes to all lines that don't start with certain strings. For example, I need to change all lines except for lines that start with "a", "hello there",... (3 Replies)
Discussion started by: RickS
3 Replies

7. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

8. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

9. Shell Programming and Scripting

search and replace with restriction (awk, sed)

Hello, i have a file like that foo A new bar A new bar B new I need to replace 'new' with 'done', but only in lines containing 'bar' AND 'A'. output file should then become foo A new bar A done bar B new Sorry im not able to figure it out, not even shure if i should take sed.... (10 Replies)
Discussion started by: knoxo
10 Replies

10. Shell Programming and Scripting

SED Search and Replace Question for Google Analytics

Well, I'm losing my regex ability in sed! Please help. I need to search for this text in multiple html files in a directory: </body> and add the following lines in front of the text above: <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> </script> ... (11 Replies)
Discussion started by: Neo
11 Replies
Login or Register to Ask a Question