extract line by line from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract line by line from file
# 1  
Old 03-16-2010
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
# 2  
Old 03-16-2010
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
Code:
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
Code:
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.
# 3  
Old 03-16-2010
By the way, sed reads file line by line.
# 4  
Old 03-16-2010
Code:
 
#!/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
# 5  
Old 03-16-2010
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. : )
# 6  
Old 03-17-2010
How to create a log file to track below script output?

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
# 7  
Old 03-17-2010
Quote:
Originally Posted by shirdi
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to extract information from a file line by line

In the below perl code I am using tags within each line to extract certain information. The tags that are used are: STB >0.8 is STRAND BIAS otherwise GOOD FDP is the second number GO towards the end of the line is read into an array and the value returned is outputed, in the first line that... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

3. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

4. Shell Programming and Scripting

Extract Line and Column from CSV Line in ksh or bash format

Hi, I was doing some research and can't seem to find anything. I'm trying to automate a process by creating a script to read a csv line and column and assigning that value to a variable for the script to process it. Also if you could tell me the line and column if it's on another work ... (3 Replies)
Discussion started by: vpundit
3 Replies

5. Shell Programming and Scripting

extract a line from a file by line number

Hi guys, does anyone know how to extract(grep) a line from the file, if I know the line number? Thanks a lot. (9 Replies)
Discussion started by: aoussenko
9 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

File Line Extract

If i have a file, how do i create a new file with say line x to y from the file i have. Eg. File1 has 100 line, i want File2 to have line 50-60 from File1 (4 Replies)
Discussion started by: kelseyh
4 Replies

9. Shell Programming and Scripting

extract a line from a file using the line number

Hello, I am having trouble extracting a specific line from a file when the line number is known. My first attempt involved grep -n 'hi' (the word 'hi will always be there) to get the line number before the line that I actually want (line 4). Extra Notes: -I am working in a bash script. -The... (7 Replies)
Discussion started by: grandtheftander
7 Replies

10. Shell Programming and Scripting

Extract a line from a file using the line number

I have a shell script and want to assign a value to a variable. The value is the line exctrated from a file using the line number. The line number it is not fix, and could change any time. I have tried sed, awk, head .. See my script # Get randome line number from the file #selectedline = `awk... (1 Reply)
Discussion started by: zambo
1 Replies
Login or Register to Ask a Question