Another sed/awk search=>replace question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Another sed/awk search=>replace question
# 8  
Old 08-28-2014
Quote:
Originally Posted by f77coder
Thanks for reply.

I removed but still get this error.
Code:
ed: 1: "sub_c18.csv": unterminated substitute pattern

From what i've read the double '//' are needed to end the wildcard???
What you mean by "end the wildcard"?

Also, that asterisk (regex repetition operator) is probably wrong there. What is it's intended function?

Otherwise, can you show a sample line from the original file and what you want to replace it with?
# 9  
Old 08-28-2014
This data file sub_c18.csv looks like this

Code:
.
.
61406774,0.553508428
61406775,a2f14cf2
61406776,0.169964852
.
.

Where 61406775,a2f14cf2 is a bad line of data to be replaced with something like 61406775,0.7463839

a2f14cf2 hex number is random.

Don't I need the wildcard to match the complete line? I want to do whole line substitution.

Last edited by rbatte1; 08-29-2014 at 08:34 AM.. Reason: Added ICODE tags and corrected spelling
# 10  
Old 08-28-2014
Try:
Code:
"s/^$i,.*/$i , $ratio/"

or

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

if there can be zero or more spaces before the comma

Try it without the -i option first..
# 11  
Old 08-28-2014
I tried both and they both started looping without stopping and not substituting.

After looking closely, there is a white space after the hex number.
# 12  
Old 08-28-2014
After the hex number? That should not make a difference, as .* should cover that..

What starts looping. What did you try exactly?
If I put your sample in a file, I get this:
Code:
$ cat somefile
.
.
61406774,0.553508428
61406775,a2f14cf2
61406776,0.169964852
.
.
$ i=61406775 ratio=0.7463839
$ sed "s/^$i *,.*/$i , $ratio/" somefile
.
.
61406774,0.553508428
61406775 , 0.7463839
61406776,0.169964852
.
.

# 13  
Old 08-28-2014
This is the code I used.
Code:
#!/usr/local/bin/bash
array=(61329025
61333750
61348174
61368157
61371144
61374807
61406775
61413072
61416814
61418304
61421223
61423127
61432337
61449549
61449715
61451883
61453612
61457479)

file2mod=sub_c18.csv

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 "s/^$i *,.*/$i , $ratio/" $file2mod 
done
echo "done"

part of sub_c18.csv file
Code:
60000000,0.22370241
60000001,0.192148371
60000002,0.235125105
60000003,0.553508428
60000004,0.20143351
60000005,0.169964852
60000006,0.272900889
60000007,0.103461449
60000008,0.0722885972
60000009,0.300323534
60000010,0.169650792
60000011,0.169964852
60000012,0.256249637
60000013,0.551763018
60000014,0.192014202
60000015,0.103461449
60000016,0.103461449
60000017,0.445011978
60000018,0.433271354
60000019,0.230131215
60000020,0.218191459
60000021,0.213877195
60000022,0.0722885972
60000023,0.295587426
60000024,0.0472011667
60000025,0.0472011667
60000026,0.335213292
60000027,0.305288462
...

# 14  
Old 08-28-2014
I think it's got to be sed "s/^$i.*/$i , $ratio/"

---------- Post updated at 08:37 PM ---------- Previous update was at 08:35 PM ----------

Worked for me as I've tried to systematically trace it...

---------- Post updated at 08:41 PM ---------- Previous update was at 08:37 PM ----------

x
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