Extract a number from a line in a file and sed in another copied file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract a number from a line in a file and sed in another copied file
# 1  
Old 12-11-2010
Extract a number from a line in a file and sed in another copied file

Dear all,
I am trying to extract a number from a line in one file (task 1), duplicate another file (task 2) and replace all instances of the strings 300, in duplicated with the extracted number (task 3). Here is what I have tried so far:
Code:
for ((k=1;k<4;k++)); do

 temp=`sed -n "${k}p" temperatures`; #Task 1
 
 cp nvt.mdp nvt_$k.mdp; #Task 2

 sed 's/300/$temp/g' < nvt_$k.mdp > $k_nvt.mdp; #Task 3

done

temperatures file:
Code:
275
277
279
281

nvt.mdp file
Code:
...
tau_t           = 0.1   0.1     ; time constant, in ps
ref_t           = 300   300     ; reference temperature, one for each group, in K
...


The result is that I get copies of nvt_1.mdp to nvt_3.mdp correctly, however, I obtain only one copy from task 3 and it is not even named correctly ie. 1_nvt.mdp is only .mdp.

In addition, since the sed command extracts a string from the line, it also extracts with it the line-break "character", changing the formatting of .mdp file.

I realized that if I were to convert the extracted number into an integer it should in theory work to prevent the format change of task 3 files. Though typset -i before Task 1 command gives the error:

")syntax error: invalid arithmetic operator (error token is "

If anyone could help with the proper implementation of task 3 such that formatting of $k_nvt.mdp is not changed and the proper number of copies are generated, I would be very grateful.

Cordially,
Ali

Last edited by Franklin52; 12-12-2010 at 08:46 AM.. Reason: Please use code tags
# 2  
Old 12-12-2010
Please try this within the loop and check:

Code:
temp=`sed "${k}s/^[^0-9]*\([0-9]*\).*$/\1/g" temperatures`; #Task 1

cp nvt.mdp nvt_$k.mdp; #Task 2

sed 's/300/$temp/g' < nvt_$k.mdp > ${k}_nvt.mdp; #Task 3

# 3  
Old 12-12-2010
This corrects the error of naming the new files in Task 3. Thank you.

However, the new files do not contain anything and running the script returns the following error:

sed: -e expression #1, char 9: unterminated `s' command
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed to extract line of a file

Ok .. This is driving me nuts. I suppose I need another set of eyes. Example Data interface Vlan1 description xxxxxxxxxxxx ip address 192.168.1.1 255.255.255.252 no ip redirects no ip proxy-arp ip flow ingress ip pim sparse-mode load-interval 30 ! interface Vlan2 ... (5 Replies)
Discussion started by: popeye
5 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

Perl extract number from file & write to file

I have 1 file that has elements as follows. Also the CVR(10) and the word "SAUCE" only appear once in the file so maybe a grep command would work? file1 CVR( 9) = 0.385E+05, ! VEHICLE CVR(10) = 0.246E+05, ! SAUCE CVR(11) = 0.162E+03, ! VEHICLE I need to extract the... (6 Replies)
Discussion started by: austinj
6 Replies

4. Shell Programming and Scripting

extract a line from a file by line number

Hi guys, does anyone know how to extract(grep) a line from the file, if I know the line number? Thanks a lot. (9 Replies)
Discussion started by: aoussenko
9 Replies

5. Shell Programming and Scripting

Extract string from multiple file based on line count number

Hi, I search all forum, but I can not find solutions of my problem :( I have multiple files (5000 files), inside there is this data : FILE 1: 1195.921 -898.995 0.750312E-02-0.497526E-02 0.195382E-05 0.609417E-05 -2021.287 1305.479-0.819754E-02 0.107572E-01 0.313018E-05 0.885066E-05 ... (15 Replies)
Discussion started by: guns
15 Replies

6. Shell Programming and Scripting

Help on Sed/awk/getting line number from file

I Have file1 with below lines : #HostNameSelection=0 :NotUsed #HostNameSelection=1 :Automatic #HostNameSelection=3 :NotForced I have file2 which has similar lines but with different values I want to copy the changes from file1 to file2 ,line by line only if line begins with '#'. for... (7 Replies)
Discussion started by: mvr
7 Replies

7. Shell Programming and Scripting

extract the lines between specific line number from a text file

Hi I want to extract certain text between two line numbers like 23234234324 and 54446655567567 How do I do this with a simple sed or awk command? Thank you. ---------- Post updated at 06:16 PM ---------- Previous update was at 05:55 PM ---------- found it: sed -n '#1,#2p'... (1 Reply)
Discussion started by: return_user
1 Replies

8. Shell Programming and Scripting

extract a line from a file using the line number

Hello, I am having trouble extracting a specific line from a file when the line number is known. My first attempt involved grep -n 'hi' (the word 'hi will always be there) to get the line number before the line that I actually want (line 4). Extra Notes: -I am working in a bash script. -The... (7 Replies)
Discussion started by: grandtheftander
7 Replies

9. Shell Programming and Scripting

Extract a line from a file using the line number

I have a shell script and want to assign a value to a variable. The value is the line exctrated from a file using the line number. The line number it is not fix, and could change any time. I have tried sed, awk, head .. See my script # Get randome line number from the file #selectedline = `awk... (1 Reply)
Discussion started by: zambo
1 Replies

10. Shell Programming and Scripting

SED: Update Last Line with Number Lines in File

Hi, I have to update last line of a text file with the number of lines in that file. This last line will have text such as 0.0000 and I should replace this with number lines. If lines are 20 then it should be replaced with 00020. Any sed or awk cmd help would be appreciated (3 Replies)
Discussion started by: bmkux
3 Replies
Login or Register to Ask a Question