extract line by line from file

hi dudes,

I have a text file in the below format

1 s sanity /u02
2 r script1 /u02
3 s sanity /u02

Please tell me a script to read this file line by line, I wrote the below script , but it is printing only 1st line not printing rest of the lines.

#!/bin/bash
i=1;
sed -n ${i}p config.txt | while read line; do
print $line
i=`expr $i + 1`;
done

In your code the sed command's output is only getting redirected to the while.
So the first line only will get printed.
You could do the incrementation of i in an while loop and then you need to readirect.
This is the correct way.

---------- Post updated at 03:56 PM ---------- Previous update was at 03:47 PM ----------

You could use like this way

i=1;
for i in `seq 1 3`
do
sed -n ${i}p file | while read line; do
echo $line
done
done

Now the output I am getting is

1 s sanity /u02
2 r script1 /u02
3 s sanity /u02

Here only there are 3 lines.
So that I have used 1 3 in seq.

By the way, sed reads file line by line.

 
#!/bin/bash
i=1;
while read line;
do
sed -n ${i}p config.txt
i=`expr $i + 1`;
done < "config.txt"

This code will work for any no of lines

also, why need sed? if you want to read the line line by line, "while read" statement will do that!.

---------- Post updated at 04:08 PM ---------- Previous update was at 04:06 PM ----------

oops.. Its already suggested above. : )

Hi Dudes,

Can you please suggest me how to create a logfile to track the below script output ? Thanks

#!/bin/ksh
# backup the "std" I/P file descriptor
exec 5<&0
#echo "Proceed ?"
while read config_line; do
# backup the I/P file descriptor of "while" block
exec 6<&0
# restore the "std" I/P file descriptor
exec 0<&5
script_type=`print $config_line | awk -F" " '{print $2}'`
script_name=`print $config_line | awk -F" " '{print $3}'`
script_path=`print $config_line | awk -F" " '{print $4}'`
#echo "script_type = ${script_type}"
#echo "script_name = ${script_name}"
#echo "script_path = ${script_path}"
if [[ ! -x ${script_path}/${script_name} ]]; then
print "Either there is NO such FIle / Path or Execute permission is not set for ${script_name}."
exit 1;
fi
cd $script_path;
print "Started executing the script, ${script_name} ..."
if [[ ${script_type} = 's' ]] || [[ ${script_type} = 'S' ]]; then
./${script_name};
retval = $?;
elif [[ ${script_type} = 'r' ]] || [[ ${script_type} = 'R' ]]; then
run_script.ksh ${script_name};
retval = $?;
fi

            if [[ $\{retval\} -ne 0 ]]; then
                    print "$\{script_name\} script is Failed."
            else
                    print "$\{script_name\} script is successfully completed."
            fi

# restore the I/P file descriptor of "while" block & close the fd of 51
exec 0<&6 6<&-
done < config.txt

Please, don't piggy-back on existing threads - open a new thread for a new subject!

Adressing the original question, why the use of sed? This should do:

#!/bin/bash
while read line; do
  echo "$line"
done < config.txt