Simple while read line loop question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple while read line loop question
# 1  
Old 09-16-2011
Simple while read line loop question

Code:
#!/bin/bash

count=1
while read line
do
    if (($count > 4)); then
        awk -v var1="$count" '{printf "%3s%8s%11s%11s%11s\n",var1,$2,$3,$4,$5}'
    else
        echo $line
    fi
count=$((count+1))
done < posre_sub.itp > test

cat test

INPUT:
; position restraints for System_&_XYL of Protein in water
[ position_restraints ]
; i funct fcx fcy fcz
7399 1 100 100 100
7400 1 100 100 100
7401 1 100 100 100

OUTPUT (as should be):
; position restraints for System_&_XYL of Protein in water
[ position_restraints ]
; i funct fcx fcy fcz
1 1 100 100 100
2 1 100 100 100
3 1 100 100 100
The code should give me a file test with a consecutively numbered column $1 (after 4th line) with values 1-3, instead I always get the number 5. Any ideas why?
# 2  
Old 09-16-2011
Hi,

My changes. Test it:
Code:
$ cat origamisven.sh
#!/bin/bash

count=1
while read line
do
    if (($count > 3)); then
        num_line=$((++num_line))
        echo "$line" | awk -v var1="$num_line" '{printf "%3s%8s%11s%11s%11s\n",var1,$2,$3,$4,$5}'
    else
        echo $line
    fi
count=$((count+1))
done < posre_sub.itp > test

cat test
$ bash origamisven.sh 
; position restraints for System_&_XYL of Protein in water
[ position_restraints ]
; i funct fcx fcy fcz
  1       1        100        100        100
  2       1        100        100        100
  3       1        100        100        100

Regards,
Birei

Last edited by birei; 09-16-2011 at 07:59 PM..
This User Gave Thanks to birei For This Post:
# 3  
Old 09-16-2011
Using awk to process individual lines is extremely wasteful. It's efficient at batch data but takes time to load and quit. You're loading an entire programming language, putting 20 bytes into it, processing those 20 bytes, and waiting for it to quit every single line. It's like only saying one word per telephone call...

Since you're using awk anyway, why not do it all in awk?

Code:
# NR is the line number.  So, for lines 4 and up, set the first record,
# aka $1, to the line number minus three.
# The 1 after the } makes it print every line.
$ awk '{ if(NR>3) $1=NR-3 } 1' < data
; position restraints for System_&_XYL of Protein in water
[ position_restraints ]
; i funct fcx fcy fcz
1 1 100 100 100
2 1 100 100 100
3 1 100 100 100
$

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 09-19-2011
Quote:
Originally Posted by Corona688
Using awk to process individual lines is extremely wasteful. It's efficient at batch data but takes time to load and quit. You're loading an entire programming language, putting 20 bytes into it, processing those 20 bytes, and waiting for it to quit every single line. It's like only saying one word per telephone call...

Since you're using awk anyway, why not do it all in awk?

Code:
# NR is the line number.  So, for lines 4 and up, set the first record,
# aka $1, to the line number minus three.
# The 1 after the } makes it print every line.
$ awk '{ if(NR>3) $1=NR-3 } 1' < data
; position restraints for System_&_XYL of Protein in water
[ position_restraints ]
; i funct fcx fcy fcz
1 1 100 100 100
2 1 100 100 100
3 1 100 100 100
$

I know my code is clumsy. I'm a novice and need stuff done quickly and in a practical way. Thanks a lot for the heads up. The awk solution is wonderful. Smilie

(Both solutions work, actually)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While read line loop

Based on text file: PATH:/media/hdd/Media/Video/title1 FILE:/media/cache/281662-14.jpg PATH:/media/hdd/Media/Video/title2 FILE:/media/cache/281662-15.jpg PATH:/media/hdd/Media/Video/title3 FILE:/media/cache/281662-16.jpg PATH:/media/hdd/Media/Video/title4 FILE:/media/cache/281662-17.jpg... (12 Replies)
Discussion started by: TiedCone
12 Replies

2. Shell Programming and Scripting

While loop read line not working

Hi, I am trying to read a file line by line inside of a while loop. This while loop is part of a here document. while read line do ssh -t $2@$remotehost <<REMOTE ls path/to/dir > $path_to_dir while read line1 do echo "LINE --- $line" done... (4 Replies)
Discussion started by: mnanavati
4 Replies

3. Shell Programming and Scripting

While loop read line Issue

Hi I am using while loop, below, to read lines from a very large file, around 400,000 rows. The script works fine until around line 300k but then starts giving incorrect result. I have tried running the script with a smaller data set and it works fine. I made sure to include the line where... (2 Replies)
Discussion started by: saurabhkumar198
2 Replies

4. Shell Programming and Scripting

really simple for loop question

apologies for the basicness of this question, but within a for loop how can i "list" my values as opposed to adding them to the same line for example if i use this it works fine for n in peter paul david do echo "my name is $n" done but i wanted to list my items ( i have quite a... (2 Replies)
Discussion started by: rethink
2 Replies

5. Shell Programming and Scripting

While read line loop

Hi I'm writing a bash script which will read an input file and look for occurrences of the current user ($USER) executing the script. When i find the occurrence of the username I take that line and append it to a file with a line number and bracket display next to line. The input file has been... (12 Replies)
Discussion started by: BundBash
12 Replies

6. Shell Programming and Scripting

read line in a for loop

Hi All, How can we use read line using the index value of a FOR loop? eg: pt_mstr,pt_status,8 pt_mstr,pt_buyer,8 pt_mstr,pt_sfty_stk,8 pt_mstr,pt_ord_pol,3 pt_mstr,pt_capacity,8 pt_mstr,pt_plan_ord,3 pt_mstr,pt_ord_mult,8 From this file i want to read the line2, 3 and 4 only using a FOR... (3 Replies)
Discussion started by: balajim
3 Replies

7. Shell Programming and Scripting

Simple for loop question

Hello I am a beginner of shell scripting and i am having trouble to do a for loop. I want a for loop to do stuff 3 times. i.e. in visual basic i do this for (counter = 0; counter < 3; counter++) on my shell script i have something like this at the moment ... (7 Replies)
Discussion started by: arex876
7 Replies

8. Shell Programming and Scripting

Simple Bash Read File question

Hello all, I am trying to write a simple script that will parse through a text/properties file and check a couple of if statements. Here is what I have so far: FILENAME=$1 while read line do echo $line done < $FILENAME When I call ./simple.sh testfile.txt I recieve a file or... (7 Replies)
Discussion started by: lamagra
7 Replies

9. Shell Programming and Scripting

Simple while loop question

I have written a script that countsdown from 20 to 1 and has a sleep in between each count but I want to make it sleep for half a second but I get errors whenever I change the sleep from 1 second to half a second any ideas? I am using Sun OS 5.9 heres what I've got: X=0 while do echo... (3 Replies)
Discussion started by: Brokeback
3 Replies

10. Shell Programming and Scripting

Simple script loop question

Hey all. Thanks in advance for any help you can give, hopefully this is an easy one. I want to create a loop to run a simple performance monitor like vmstat and record it to a file, but have very limited scripting skills (obviously). Starting with this... date >> /var/log/perfmon.log vmstat... (2 Replies)
Discussion started by: mattlock73
2 Replies
Login or Register to Ask a Question