for loop and grep question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting for loop and grep question
# 1  
Old 04-22-2009
Error for loop and grep question

I am trying to work on a script file for tcsh but am fairly new to linux and have several questions.

1. What is the most effective way to set up a type of for loop? I need to run two loops that will each run in steps from a starting value to final value, eg i=0, 1==10, i++ in any other language. What is the most efficient way to set this up?

So far we came up with:
set i_forloopval = `echo "forloop" | awk '{for(p=0.7;p<1.43;p=p+0.08){print p}}'`
set j_forloopval = `echo "forloop" | awk '{for(p=0.9;p<1.81;p=p+0.1){print p}}'`

foreach i ($i_forloopval)
foreach j ($j_forloopval)

but it seems like there must be a more efficient way to go about this.

2. Every time it runs through the loop, it is going to run a program that creates a unique output file. Instead of having to manually search through this file, I was hoping to use grep to search for the line that we need.

A given output might look like:
1\1\GINC-NODE006\Freq\RB3LYP\6-311G(d)\Br2\TYLERT\02-Feb-2009\0\\#N Ge
om=AllCheck Guess=Read SCRF=Check GenChk RB3LYP/6-311G(d) Freq\\Title\
\0,1\Br,0.,0.,-0.0263112705\Br,0.,0.,2.3073112705\\Version=AM64L-G03Re
vD.01\State=1-SGG\HF=-5148.2839416\RMSD=5.854e-11\RMSF=1.293e-06\Therm
al=0.0034749\Dipole=0.,0.,0.\DipoleDeriv=0.,0.,0.,0.,0.,0.,0.,0.,0.,0.
,0.,0.,0.,0.,0.,0.,0.,0.\Polar=17.7366557,0.,17.7366557,0.,0.,51.89093
66\PG=D*H [C*(Br1.Br1)]\NImag=0\\0.00021679,0.,0.00021679,0.,0.,0.1438
2273,-0.00021679,0.,0.,0.00021679,0.,-0.00021679,0.,0.,0.00021679,0.,0
.,-0.14382273,0.,0.,0.14382273\\0.,0.,0.00000224,0.,0.,-0.00000224\\\@

the only information that is needed out of this is HF=-5148.2839416. What is the best way to extract only this from the output file? This is further complicated by the fact that this does not always occur on one line but will sometimes appear as:
one one line: HF=-5148.28
39416 and then this part will be wrapped on to the next line.

Any help would be appreciated.
# 2  
Old 04-23-2009
Quote:
Originally Posted by redalert013
I am trying to work on a script file for tcsh but am fairly new to linux and have several questions.

1. What is the most effective way to set up a type of for loop? I need to run two loops that will each run in steps from a starting value to final value, eg i=0, 1==10, i++ in any other language. What is the most efficient way to set this up?

So far we came up with:
set i_forloopval = `echo "forloop" | awk '{for(p=0.7;p<1.43;p=p+0.08){print p}}'`
set j_forloopval = `echo "forloop" | awk '{for(p=0.9;p<1.81;p=p+0.1){print p}}'`

foreach i ($i_forloopval)
foreach j ($j_forloopval)

but it seems like there must be a more efficient way to go about this.

2. Every time it runs through the loop, it is going to run a program that creates a unique output file. Instead of having to manually search through this file, I was hoping to use grep to search for the line that we need.

A given output might look like:
1\1\GINC-NODE006\Freq\RB3LYP\6-311G(d)\Br2\TYLERT\02-Feb-2009\0\\#N Ge
om=AllCheck Guess=Read SCRF=Check GenChk RB3LYP/6-311G(d) Freq\\Title\
\0,1\Br,0.,0.,-0.0263112705\Br,0.,0.,2.3073112705\\Version=AM64L-G03Re
vD.01\State=1-SGG\HF=-5148.2839416\RMSD=5.854e-11\RMSF=1.293e-06\Therm
al=0.0034749\Dipole=0.,0.,0.\DipoleDeriv=0.,0.,0.,0.,0.,0.,0.,0.,0.,0.
,0.,0.,0.,0.,0.,0.,0.,0.\Polar=17.7366557,0.,17.7366557,0.,0.,51.89093
66\PG=D*H [C*(Br1.Br1)]\NImag=0\\0.00021679,0.,0.00021679,0.,0.,0.1438
2273,-0.00021679,0.,0.,0.00021679,0.,-0.00021679,0.,0.,0.00021679,0.,0
.,-0.14382273,0.,0.,0.14382273\\0.,0.,0.00000224,0.,0.,-0.00000224\\\@

the only information that is needed out of this is HF=-5148.2839416. What is the best way to extract only this from the output file? This is further complicated by the fact that this does not always occur on one line but will sometimes appear as:
one one line: HF=-5148.28
39416 and then this part will be wrapped on to the next line.

Any help would be appreciated.
I am not quite clear about your first question. please elaborate on what you are trying to implement.

for the second question, please try this

awk -F "\\" '{for(i=1;i<=NF;i++){if ($i ~ /HF/)print $i}}' file


cheers,
Devaraj Takhellambam
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ps ax with grep in loop

Hello, I have built the following script to check if processes supplied by the argument are running or not. #!/bin/bash PROCLIST=$1 PROCESS="0" ERROR_PROCS="" IFS='+' read -ra ADDR <<< "$PROCLIST" for PROC in "${ADDR}"; do if ; then PROCESS=1 ... (9 Replies)
Discussion started by: nms
9 Replies

2. Shell Programming and Scripting

Grep issue in for loop

Hi friends, I am trying to use a grep command in for loop but i keep getting an error message on the grep, here is the code grep -i "Target connection group" $LogName | awk '{print $10}' > TableNames.rpt cat TableNames.rpt | sort -u > DistinctTable.rpt ... (3 Replies)
Discussion started by: Aditya_001
3 Replies

3. Homework & Coursework Questions

GREP loop

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I need to search through the users home directories for keywords, display them. The code listed below will show... (7 Replies)
Discussion started by: jcllns1
7 Replies

4. Red Hat

using grep in a while loop

Hello everybody, I have been searching it, but it seems I am unable to find the correct information, that s why I am asking you guys, hoping somebody get an idea. Here is my problem : I want a script to loop until a string is identified in a log file. Here is the script : #!/bin/sh... (5 Replies)
Discussion started by: guyiom
5 Replies

5. Shell Programming and Scripting

Help on grep in a do while loop

So this is what I'm trying to do: I have a file called registry.txt which has a list of registry entries I want to search for. I have another file called inctrl.txt on which I want to perform the search on. Here's the example contents of registry.txt SOFTWARE\Microsoft\Security... (3 Replies)
Discussion started by: r4v3n
3 Replies

6. Shell Programming and Scripting

Using grep within a while loop

Hi all, I have the below script to get input but i cannot get grep to work. input1.txt AAAAAAAAG input2.txt >gi|184009.1| LEAFY-like |AAAAAAAAGSGGGDHLPY However, when i use grep -f input1.txt input2.txt i cannot get any output matches (note that the match is underlined). Is it... (8 Replies)
Discussion started by: turkishvan
8 Replies

7. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

8. Shell Programming and Scripting

Grep commands in loop

Hi All, Reference to my previous post I need to compare all the lines in the file1 with file2 for this condition if file1 {$3,$5} ==file2 {$3,$5} then grep file2{$1}latest date. need output in file3 10/04/2008 09/04/2008 09/04/2008 08/04/2008 can anyone suggest me Thanks... (0 Replies)
Discussion started by: karthikn7974
0 Replies

9. UNIX for Dummies Questions & Answers

grep -v while loop

alist contain: a b c d e blist contain: a b c the code: #!/usr/bin/ksh cat blist | while read line do grep -V "$line" alist > data done (8 Replies)
Discussion started by: bobo
8 Replies

10. Shell Programming and Scripting

grep in a loop

Hi , I am trying a script which takes user input userid . I am stuck how to check whether that is a valid user id or not in the audit log files. My code is : cd $CCP_AUDIT cat * > /export/home/$USR/l***/files echo "UserId:\c" read UserId #Date Function echo "DATE : \c" read xxx I... (7 Replies)
Discussion started by: gundu
7 Replies
Login or Register to Ask a Question