SED (regular expression) problem ---


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED (regular expression) problem ---
# 1  
Old 05-16-2010
Data SED (regular expression) problem ---

Hello,

I would like to replace Line 187 of my file named run_example.

The original line is below, including the spaces:
Code:
  celldm(1)   = 6.00,

I want it to become something like
Code:
  celldm(1)   = 6.05,

or
Code:
  celldm(1)   = 6.10,

where the number is stored in a variable called $bulksize . This variable increments properly in my for loop.

I tried doing this:
Code:
cat run_example | sed -e "187 s_  [a-z]*(1)   = _& $bulksize,_" > run_example

Didn't work...

Thank you for your help!

# 2  
Old 05-16-2010
hmm - couple of potential issues with

Code:
cat run_example | sed -e "187 s_  [a-z]*(1)   = _& $bulksize,_" > run_example

Firstly, by opening the file for read and write at the same time you are likely to simply zero it. You are better to output to a new file, or use sed or perl inline editting.

Taking a 5 line file:
Code:
#  cat run_example 
celldm(1)   = 6.00,
celldm(1)   = 6.00,
celldm(1)   = 6.00,
celldm(1)   = 6.00,
celldm(1)   = 6.00,


Then something like:

Code:
#  export bulksize=6.10

#  perl -pi -e 'if ($. == 4) { s/\=.*/= $ENV{bulksize},/ };' run_example 

#  cat run_example 
celldm(1)   = 6.00,
celldm(1)   = 6.00,
celldm(1)   = 6.00,
celldm(1)   = 6.10,
celldm(1)   = 6.00,

Will change line 4 in place (didn't want to make a 187 line file:-)   Just change the 4 to 187 for line 187.

HTH
This User Gave Thanks to Tytalus For This Post:
# 3  
Old 05-16-2010
Data SED (regular expression) problem...

Thanks -

Actually, I didn't realize this would make a difference, but I have a parent directory called [myExamples] that contains my scripts (prep) and (run_example).

I have already made subdirectories of [myExamples], and I can run (prep) to copy (run_example) into each subdirectory.

(run_example) has a few lines that look similar to Line 187, plus a lot of other hulababoos...

The problem is to automatically replace Line 187 of (run_example) in each subdirectory, with a different value of $bulksize (see top message)...

Below is a snippet of (prep) -

Code:
bulksize=`expr 6.00` #This line works
half=$(echo "scale=2; $bulksize / 2" | bc) #This line works
for i in `ls -d */`; 
    do    
    CORRECTED LINE HERE
    half=$(echo "scale=2; $half + 0.05" | bc) # This line works
    bulksize=$(echo "scale=2; $half * 2" | bc) # This line works
done;


Last edited by bluesmodular; 05-16-2010 at 03:20 PM..
# 4  
Old 05-16-2010
Hello, bluesmodular, and welcome to the forums.

Using ls to generate the for loop's list is often a poor approach. If there are any IFS characters (by default, space, tab, and newline) in the resulting filenames, they will be mangled. Often, when making use of some of ls' options (recursive listing, for example), this shortcoming is disregarded if troublesome filenames aren't expected. However, in this particular case, ls is completely unnecessary; you can generate a list of the necessary files with a safe, simple glob.

As for editing each file in place, you can do so with ed:
Code:
half=....
bulksize=....
for i in */run_example; do
    printf '187s/= [^,]*/= %s/\nwq\n' "$bulksize" | ed -s "$i"
    half=....
    bulksize=....
done

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 05-16-2010
MySQL

Thank you Alister!

Follow-up question - By similarly using printf and ed, how would I replace a line that contains only a number, e.g. 6.00, with another number stored in variable $bulksize or $half ?

I guess I don't understand all the parts in this regular expression
'187s/= [^,]*/= %s/\nwq\n'

I know 187 is the line number, the s after that is substitution, and ^, matches a comma in the beginning? Or anything that's not a comma? Not sure what the rest of it means... where can I find a good reference for this?

Thanks!!

Last edited by bluesmodular; 05-17-2010 at 01:35 AM..
# 6  
Old 05-17-2010
MySQL

For example i want to 6.line

Code:
# cat myfile
celldm(1)   = 6.00,
celldm(1)   = 6.00,
celldm(1)   = 6.00,
celldm(1)   = 6.00,
celldm(1)   = 6.00,
celldm(1)   = 10.00,
celldm(1)   = 6.00,

Code:
[root@sistem1lnx bin]# ./deg
celldm(1)   = 6.00,
celldm(1)   = 6.00,
celldm(1)   = 6.00,
celldm(1)   = 6.00,
celldm(1)   = 6.00,
celldm(1)   = 10.10,
celldm(1)   = 6.00,

Code:
#!/bin/bash
mybulksize=0.10
mynum=$(sed = myfile |
sed -e '{N;}' -e 's/\n//'|
sed -n "/^6/ {p;q}"|
sed 's/.*= \(.*\),/\1/')
mynewvalue=$(echo "scale=2; $mynum+$mybulksize" | bc -l)
newline="celldm(1)   = $mynewvalue,"
sed -i "6c$newline" myfile
cat myfile

# 7  
Old 05-17-2010
I do not understand the problem
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I am learning regular expression in sed,Please help me understand the use curly bracket in sed,

I am learning SED and just following the shell scripting book, i have trouble understanding the grep and sed statement, Question : 1 __________ /opt/oracle/work/antony>cat teledir.txt jai sharma 25853670 chanchal singhvi 9831545629 anil aggarwal 9830263298 shyam saksena 23217847 lalit... (7 Replies)
Discussion started by: Antony Ankrose
7 Replies

2. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

3. Shell Programming and Scripting

Help with sed regular expression

Hi all, I want to get a substring from a string based on given delimiter, for example: str="foo|bar|baz" with delimiter "|", I want to get one substring at each time with the order number the substring in the whole string, given 1 to get "foo", given 2 to get "bar", given 3 to get "baz", I... (2 Replies)
Discussion started by: Roy987
2 Replies

4. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

5. UNIX for Dummies Questions & Answers

Regular Expression In Sed

Hi , I am learing sed echo abc 123 def 456 | sed 's|\(*\) \(*\)|\1|' is returning abc def 456 i was hoping abc def "\1" should only print the occurence of the first pattern but according to my understanding it is just removing the first occurence of the second pattern... (7 Replies)
Discussion started by: max_hammer
7 Replies

6. Shell Programming and Scripting

sed regular expression help

please consider this: echo "11111*X*005010X279~ST*270*1111111*005010X279~BHT*0011*11" | sed 's/.*\(005010X(\d)(\d)(\d)*\).*$/\1/'i'm searching for first occurrence of 005010X while leaving rest of characters out. :confused: any tips? thnx in advance guys. (7 Replies)
Discussion started by: grep01
7 Replies

7. Shell Programming and Scripting

Regular expression (sed)

Hi I need to get text that are within "" For example File: asdasd "test test2" sadasds asdda asdasd "demo demo2" Output: test test2 demo demo2 Any help is good Thank you (12 Replies)
Discussion started by: blito_loco
12 Replies

8. Shell Programming and Scripting

Regular expression with SED

Hi! I'm trying to write a regexp but I have no luck... I have a string like this: param1=sometext&param2=hello&param3=bye Also, the string can be simply: param2=hello I want to return the value of param2: "hello". How can I do this? Thanks. (3 Replies)
Discussion started by: GagleKas
3 Replies

9. Shell Programming and Scripting

Regular expression with sed

Hi, I'm trying following:echo "test line XA24433 test" | sed 's/.*X\(.*\)/X\1/' XA24433 test While I want the output as: XA24433 I want to grab the words starting with letter X till the next space, this word can be anywhere in the line. (9 Replies)
Discussion started by: nervous
9 Replies
Login or Register to Ask a Question