How to get different lines using a for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get different lines using a for loop
# 1  
Old 11-18-2009
Question How to get different lines using a for loop

Hi

I am trying to write a small AWK program to extract 324 lines which have a pattern in the line numbers.
For examples, I want to extract lines 2, 5, 7, 11 and lines like (2+662), (5+662), (7+662), (11+662) and (2+662+662), (5+662+662), (7+662+662), (11+662+662) and so on for 27 times from 'File1'.

I wrote a code like
awk '{NR==i; for (i=2; i<=27; i+=662) print NR} {NR==i; for (i=5; i<=27; i+=662) print NR} {NR==i; for (i=5; i<=27; i+=662) print NR} {NR==i; for (i=11; i<=27; i+=662) print NR}' File1> File2

I think there are quite some flaws in my code.....Plese help Smilie
# 2  
Old 11-18-2009
It would help if you explicitly formalize your idea of your number sequence. 662 is not a prime number, for example. It is way to fuzzy for me to work with.
# 3  
Old 11-18-2009
First of all, Thanks Jim for doing an effort.

Well my requirement is like this.....I want to extract lines with these file numbers of 506, 515, 527, 536 and line numbers like 1170 (506+664*1) , 1179 (515+664*1), 1191 (527+664*1), 1200 (536+664*1) and so on. The last line numbers that I require are 17770 (506+664*26), 17779 (515+664*26), 17791 (527+664*26), 17800 (536+664*26). So I want the initial line numbers to get added to 664 multiplied from 1 to upto 26.....I am trying to put this multiplying number as a counter from 1 to 26.

So I will have in my new file the lines with line numbers of
506,515,537,536,1170,1179,1191,1200,.............,17770,17779,177791 and 17800.

Thanks a lot for the efforts.

---------- Post updated at 06:22 PM ---------- Previous update was at 03:43 PM ----------

Hi TonyLawrence,

can you write down your code with all the parantheses and everything.......i guess I need to change your code for my need a little bit
# 4  
Old 11-18-2009
I sure have to agree with Jim.

In pseudocode, is this your number sequence:

Code:
x=0
y=(2, 5, 7, 11)
while x < 81
do
for each of y
do
 print line x * 662 + y
done
x++
done

??
# 5  
Old 11-18-2009
Code:
awk '!((NR-506)%664) ||!((NR-515)%664) ||!((NR-527)%664)||!((NR-536)%664)" File1 > File2

# 6  
Old 11-19-2009
sweet

Hi

Please find the script and hope it may fulfill ur needs

I didn't try since i don't have that much lengthy file.please try and let me know. You may need to edit the script according to error that u will get.

#!/bin/ksh

set -x

F=506
S=515
T=527
L=536
C=664

for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
do
FN=`expr $F + $C * $i`
SN=`expr $S + $C * $i`
TN=`expr $T + $C * $i`
LN=`expr $L + $C * $i`

sed -n $FNp file1 >> file2
sed -n $SNp file1 >> file2
sed -n $TNp file1 >> file2
sed -n $LNp file1 >> file2
done

thank u
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop to get rid of first 2 lines(conditionally)

hello all, I get data from different vendors and need to clean it up. Usually it pretty straight forward when i have files that only have headers....but in my case i have files that have a starting line of file name(and some junk info) and 2nd line is headers and the 3rd line is were the actual... (11 Replies)
Discussion started by: crazy_max
11 Replies

2. Shell Programming and Scripting

Re: Read lines and compare in a loop

I have a file which has following content: NAME=ora.DG1.dg TYPE=ora.diskgroup.type TARGET=ONLINE STATE=ONLINE NAME=ora.DG2.dg TYPE=ora.diskgroup.type TARGET=ONLINE STATE=ONLINE NAME=ora.DG3.dg TYPE=ora.diskgroup.type TARGET=ONLINE STATE=ONLINE NAME=ora.DG4.dg... (7 Replies)
Discussion started by: rcc50886
7 Replies

3. Shell Programming and Scripting

Reading two lines in a while loop and combining the lines

Dear all, I have a file like this: imput scaffold_0 1 scaffold_0 10000 scaffold_0 20000 scaffold_0 25000 scaffold_1 1 scaffold_1 10000 scaffold_1 20000 scaffold_1 23283 and I want the output like this: scaffold_0 1 scaffold_0 10000 scaffold_0 10000 scaffold_0 20000... (6 Replies)
Discussion started by: valente
6 Replies

4. Shell Programming and Scripting

ignore blank lines in while loop

Hi, i am having one text file it contains some blank lines and i want to ignore that blank lines . #! /bin/bash clear rdCount=0; while read myline do echo $myline let rdCount=$rdCount+1 done < ps.txt echo "Total line=$rdCount" and ps .txt contains the data- (17 Replies)
Discussion started by: aish11
17 Replies

5. UNIX for Dummies Questions & Answers

analyze lines in a file by loop

Hi Dears, I use the below code to analyze lines in a file: for line in `cat ucsv` do echo $line //analyze statements donehowever, if line contains space char, it will be broken. for example, if file content is: #login,full name,email,project,role,action gmwen,Bruce... (3 Replies)
Discussion started by: crest.boy
3 Replies

6. Shell Programming and Scripting

Having a for loop read in lines with spaces?

Is this possible? I have a for loop in a shell script reading a list, but I want each line to be a loop, not each thing with a space. Here is the example: HOSTLIST="\ 1.2.3.4 serverA 1.2.3.5 serverB" for NBUHOST in `echo $HOSTLIST` do ssh ${SERVERNAME} "echo "${NBUHOST}"... (3 Replies)
Discussion started by: LordJezoX
3 Replies

7. Shell Programming and Scripting

Read lines with different lengths in while loop

Hi there ! I need to treat files with variable line length, and process the tab-delimited words of each line. The tools I know are some basic bash scripting and sed ... I haven't got to python or perl yet. So my file looks like this obj1 0.01953 0.34576 0.04418 0.01249 obj2 0.78140... (7 Replies)
Discussion started by: jossojjos
7 Replies

8. Shell Programming and Scripting

Loop through between 2 found lines

Hi frnd , hv nice week ahead, in shell script i found 2 words as below 1 ) targ_found=`sed -n \`echo $line_no\`p sesslog.txt | grep -ic 'Targ Rowid'` 2 ) bracket_found=`sed -n \`echo $line_no\`p sesslog.txt | grep -wc ')'` now we got he line number of 2 words. now i want to... (1 Reply)
Discussion started by: Gopal_Engg
1 Replies

9. Shell Programming and Scripting

exclude lines in a loop

I use while do - done loop in my shell script. It is working as per my expectations. But I do not want to process all the lines. I am finding it difficult to exclude certain lines. 1) I do not want to process blank lines as well as lines those start with a space " " 2) I do not want to... (2 Replies)
Discussion started by: shantanuo
2 Replies

10. UNIX for Dummies Questions & Answers

Loop through the lines in a file?

How do I loop through the lines of a file? Each line of the file will be the name of a server. Like: server1.stuff.com server2.stuff.com For each server I want to do a secure copy of the file to the other server. This will be a cron job so I guess it needs to run in just SH. I... (3 Replies)
Discussion started by: jimmy
3 Replies
Login or Register to Ask a Question