## sed with special characters

sed with special characters
# 1
04-06-2009
sed with special characters

Hi,

I am reading a file (GC_JAR.log) which has entries like:

511725.629, 0.1122672 secs]
525268.975, 0.1240036 secs]
527181.835, 0.2068215 secs]
527914.287, 0.2884801 secs]
528457.134, 0.2548725 secs]

I want to replace all the entries of "secs]" with just "secs"

Thus, the output should be like:

511725.629, 0.1122672 secs
525268.975, 0.1240036 secs
527181.835, 0.2068215 secs
527914.287, 0.2884801 secs
528457.134, 0.2548725 secs

I tried using sed but it's not working for some reason. I read line by line from the original file ( GC_JAR.log ) and dump the final output in GC_cycles.csv

# !/bin/bash
SOURCE=secs\]
TARGET=secs
cat GC_JAR.log | while read line
do
echo "${line}" | sed 's/"${SOURCE}"/"${TARGET}"/g' >> GC_cycles.csv done Any ideas please? what's going wrong with sed command there.  itzz.me View Public Profile for itzz.me Find all posts by itzz.me # 2 04-06-2009 single quotes stop variable expansion  jim mcnamara View Public Profile for jim mcnamara Find all posts by jim mcnamara # 3 04-06-2009 Thanks! that works I am newbie.. still learning how to use single quotes and double quotes :-) I still need to do another operation on the file With the above sed operation, I get the output file as below ( GC_cycles.csv) 511725.629, 0.1122672 secs 525268.975, 0.1240036 secs 527181.835, 0.2068215 secs 527914.287, 0.2884801 secs 528457.134, 0.2548725 secs Now, I want to convert the first field ( which is in seconds currently) to hours. I used the following: echo "Converting seconds to hours" cat GC_cycles.csv | while read line do X=echo "${line}" | awk -F"," '{ print $1}' # X is the first field X=echo$X / 60/ 60 | bc # Convert seconds to hours
Y=echo "${line}" | awk -F"," '{ print$2}' # Y is the second field
echo $X "hrs" ","$Y >> Final_GC_cycles.csv
done

Now I get the output as:

142 hrs , 0.1122672 secs
145 hrs , 0.1240036 secs
146 hrs , 0.2068215 secs
146 hrs , 0.2884801 secs
146 hrs , 0.2548725 secs

There are 2 things which I want to look at:
1) the value of X is coming in integer. How can I get it to be a floating number? with rounding to 1 decimal place.
2) Is there a more efficient way to write the above code?
 itzz.me View Public Profile for itzz.me Find all posts by itzz.me
# 4
04-06-2009
What you have looks fine, just replace the bc line with this:

X=echo "scale=1;$X / 60/ 60" | bc # Convert seconds to hours, with 1 decimal. The "scale" variable defines your precision. It's documented in the "bc" man page. :-) I've added my version of your program above: #! /bin/bash cat data | sed 's/,//g' | while read a b c do x=$(echo "scale=1;$a/60/60" | bc) echo "$x hrs, $b$c"
done

And this was done without the need of calling awk; of course I called "sed" and "bc". but I think you get the idea.

Last edited by sandholm; 04-06-2009 at 10:47 PM.. Reason: further example
 sandholm View Public Profile for sandholm Find all posts by sandholm
# 5
04-06-2009
When using perl I use the time module for conversions. In shell it is a bit more work. Here is a function I use for converting seconds, which you can include in your script
 ldapswandog View Public Profile for ldapswandog Find all posts by ldapswandog

## sed in a while loop with special characters

I have the foolowing data file: File1 <p name="A">5004</p> <p name="B">5004</p> <p name="C">5004</p> <p name="A">15004</p> <p name="B">15004</p> <p name="C">15004</p> In a while loop using sed (100 of line need to be replace), I need the output to File3:...

## sed special characters issues

I am dusting off the sed cobwebs and had a basic question: I have a file that contains: $firewall = "on";$cache = "on"; $dataset{'mary had a little lamb'} = "on"; and want to only change the contents of what is between the single quotes:$dataset{'big bad wolf'} = "on"; I...

## Sed - remove special characters

Hi, I have a file with this line, it's always in the first line: I want to remove these special characters: ´╗┐ file1 ´╗┐\\bar\c$\test2\;3.348.118 Bytes;160 ;3 \\bar\c$\test\;35 Bytes;2 ;1 I want the same file to be only \\bar\c$\test2\;3.348.118 Bytes;160 ;3 \\bar\c$\test\;35...

## sed special characters issue

Hi I'm trying to replace string1 by string2 in file homepage.htm as follows but is not working. Please Help: sedsed -i "s@'://your server name:port/test/owa'@'://11.22.33.44:5555/pls/SAMPLE'@g" homepage.htm Where, String1 ://your server name:port/test/owa String2...

## Escape special characters in SED

Need help in escaping special characters in sed command. Here is the the string which i am trying to find a replace with From :- REQUEST_TYPE=PIXEL&amp;MSG_ID={//MESSAGE_ID} To :- REQUEST_TYPE=PIXEL&amp;MSG_ID= X_EDELIVERY_MESSAGE_ID &amp; BATCH_ID= X_EDELIVERY_BATCH_ID Here is the sed command i am...

## SED with Special characters

Hello All Seeking the right one SED command. My attempt is: From orginal.txt by SED to target.txt sed -i "/('outbound-callerid/a\$ext->add($context, $exten, '', new ext_SipAddHeader('P-Preferred-Identity', '<sip:${CALLERID(nummer)}@carrier.com>'));" orginal.txtWhat am make wrong?:wall: ...

## Using sed to replace special characters

Hi everyone I have file1 contains: '7832' ' 8765 6543 I want a sed command that will format as: '7832' , '8765' , '6543' I tried sed -e s/\'//g -e 's/^*//;s/*$//' file1 > file2 sed -e :a -e '$!N; s/\n/ /; ta' file2 which gives: 7832 8765 6543 I need some help to continue with...