Reading a line and the next line in a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading a line and the next line in a shell script
# 1  
Old 08-30-2012
Reading a line and the next line in a shell script

Hi,

I'm trying to read a line and the next line in a shell script by executing the following code:

Code:
for i in `seq 1 $numLines`; do
        line=$(sed -n '{$i}p' outputFile)
        echo $line
done

$numLines contanis the outputFile's number of lines.

It doesn't work and I cannot use a while loop because I need to reed $(i+1) line after $i line.

Another question, how can I increase the counter in two integers instead of one?

By using a while loop like the following I think I cannot read the line and the next one because it runs line-by-line:

Code:
N=0
cat outputFile | while read LINE ; do
    N=$((N+1))
    echo "$LINE"
done

Any idea?

Thanks everybody!
# 2  
Old 08-30-2012
Hi
Code:
$ cat file
a
b
c
d
e

Code:
$ while read line
> do
>  echo first line is $line
>  read line1
>  echo second line is $line1
> done < file
first line is a
second line is b
first line is c
second line is d
first line is e
second line is

Guru.
# 3  
Old 08-30-2012
Code:
while read line; do echo  $line; done < outputFile

# 4  
Old 08-30-2012
bash

Hi,

Try this one,

Code:
while read line second_line;
do
   echo  "${line} - ${second_line}" 
done < file

Cheers,
Ranga Smilie
# 5  
Old 08-30-2012
Quote:
Originally Posted by rangarasan
Hi,

Try this one,

Code:
while read line second_line;
do
   echo  "${line} - ${second_line}" 
done < file

Cheers,
Ranga Smilie
This will not read 2 lines at a time. It will read one line at a time and split that one line into line and second_line variables.
# 6  
Old 08-30-2012
bash

Hi,

Try this one,
Apologies for the typo,

Code:
while read line; read second_line;
do
  echo "${line} - ${second_line}"
done <file1

This will print the value if both the variable have the value.

Cheers
Ranga Smilie
# 7  
Old 08-30-2012
Quote:
Originally Posted by rangarasan
Hi,

Code:
while read line; read second_line;

@rangarasan: This will not read the last line in case of file with odd number of lines. I mean it will not get inside while loop.

Guru.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script UNIX to read text file line by line

i have a text file as belows, it includes 2 columns, 1st is the column name, 2nd is the file_name data_file.txt column_name file_name col1 file1 col2 file2 col3 file1 col4 file1 col5 file2 now, i would like to... (4 Replies)
Discussion started by: tester111
4 Replies

2. Shell Programming and Scripting

Replace values in script reading line by line using sed

Hi all, Let's say I have a script calling for the two variables PA_VALUE and PB_VALUE. for pa in PA_VALUE blah blah do for pb in PB_VALUE blah blah do I have a text file with two columns of values for PA and PB. 14.5 16.7 7.8 9.5 5.6 3.6 etc etc I would like to read this... (7 Replies)
Discussion started by: crimsonengineer
7 Replies

3. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

4. Shell Programming and Scripting

Shell script to read multiple options from file, line by line

Hi all I have spent half a day trying to create a shell script which reads a configuration file on a line by line basis. The idea of the file is that each will contain server information, such as IP address and various port numbers. The line could also be blank (The file is user created). Here... (1 Reply)
Discussion started by: haggismn
1 Replies

5. Shell Programming and Scripting

Shell script to read a text file line by line & process it...

Hi , I am trying to write an shell, which reads a text file (from a location) having a list of numbers of strictly 5 digits only ex: 33144 Now my script will check : 1) that each entry is only 5 digits & numeric only, no alphabets, & its not empty. 2)then it executes a shell script called... (8 Replies)
Discussion started by: new_to_shell
8 Replies

6. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

7. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

8. Shell Programming and Scripting

Reading line by line from unix script

Hi I am a complete newbie in unix. Learning the ropes. I have a task where I have to write a shell script to read a file line by line. I have tried some code from the net like. The file data looks like this. FIN427;2010003;2010003 FIN475;2010001;2010003 FIN476;2010001;2010003... (4 Replies)
Discussion started by: chamajid
4 Replies

9. Shell Programming and Scripting

Reading a file line by line and processing for each line

Hi, I am a beginner in shell scripting. I have written the following script, which is supposed to process the while loop for each line in the sid_home.txt file. But I'm getting the 'end of file' unexpected for the last line. The file sid_home.txt gets generated as expected, but the script... (6 Replies)
Discussion started by: sagarparadkar
6 Replies
Login or Register to Ask a Question