File handling in UNIX


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers File handling in UNIX
# 1  
Old 05-17-2005
File handling in UNIX

Hi All,
I want to read a file in UNIX line by line.

Can u suggest me any command for this?
# 2  
Old 05-17-2005
Depends on what you want to carry out.

Here is one solution
Code:
#! /bin/sh
for i in $(cat file.name)
do
echo " This is the line just read $i"
done

The above assumes you have a file and you want to read the contents line by line.

Suppose you want to input lines to a file from the standrd input, then this would do

Code:
#! /bin/sh

while read line
do
echo $line >> file.name
done


Vino
# 3  
Old 05-17-2005
In Vino's post the first method would be better done like this:

Code:
#! /bin/ksh

while read line
do
    echo $line >> outputfile.name
done < inputfile.name

note the the only change from the second one is the bit in bold. In his example $(cat file.name) is incorrect since his example was using sh as it's shell that syntax is used in ksh/bash not sh, also it is a UUOC, basically cat file.name is a waste of a process, since the shell can handle the input from a file.
# 4  
Old 05-17-2005
See the Tips and Tutorials Section for 12 methods for reading/parsing a file.
# 5  
Old 05-18-2005
Reborg,

I stand corrected.

Thanks
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX file handling issue

I have a huge file semicolon( ; ) separated records are Pipe(|) delimited. e.g abc;def;ghi|jkl;mno;pqr|123;456;789 I need to replace the 50th field(semicolon separated) of each record with 9006. The 50th field can have no value e.g. ;; Can someone help me with the appropriate command. (3 Replies)
Discussion started by: Gurkamal83
3 Replies

2. Programming

Please help:program hang stuck there signal handling on POSIX Message Queue UNIX C programming

in a single main() function,so need signal handling. Use Posix Message Queue IPC mechanism , can ignore the priority and other linked list message,to implement the scenario: client:Knock Knock server:who's there client: Eric Server:Eric,Welcome. client:exit all process terminated ... (1 Reply)
Discussion started by: ouou
1 Replies

3. Shell Programming and Scripting

UNIX File handling -Issue in reading a file

I have been doing automation of daily check activity for a server, i have been using sqls to retrive the data and while loop for reading the data from the file for several activities. BUT i got a show stopper the below one.. where the data is getting store in $temp_file, but not being read by while... (1 Reply)
Discussion started by: KuldeepSinghTCS
1 Replies

4. UNIX for Dummies Questions & Answers

File Handling in UNIX

Hi all, I have a requirement here where I am dealing with a dynamic file. Each record in the file can contain anywhere between 1(min) to 42(max) Reject codes. For example I may have one record in the file having 3 reject codes and another record having 5 reject codes. The reject codes will be... (2 Replies)
Discussion started by: sujainarayan
2 Replies

5. Shell Programming and Scripting

Error handling in Unix shell scripting

Hello, I have written a shell script and suppose there is any error in the script. How i can do exception handling in shell script.for example i have below code sqlplus -s <<uid>>/<<pwd>>@<<$ORACLE_SID>> <<EOF > 1_pid1.log set pagesize 0 set feedback off set heading off set linesize 200... (1 Reply)
Discussion started by: rksingh003
1 Replies

6. UNIX for Dummies Questions & Answers

Socket Handling Differences Between Linux & Unix?

Sorry if this is a stupid question! I have been developing a Java application that I am deploying on both Unix and Linux servers, which uses lots of socket handling. When the server side connection is dropped by the server un-gracefully I have been seeing close_waits and null connections. ... (0 Replies)
Discussion started by: Vinnie
0 Replies

7. Shell Programming and Scripting

Help needed in switch case handling in UNIX

Hi, In below code, i am expecting the output has Bye Bye But i am getting has Bye Hi Code: #!/usr/bin/bash var="Hi" cat txt.txt | while read var1 do next="Bye" case $var in Hi) (1 Reply)
Discussion started by: Balamani
1 Replies

8. Shell Programming and Scripting

File handling, getopts command in unix

I need to create a shell script having the menu with few options such as 1. Listing 2. Change permissions 3. Modify Contents 4. Delete Files 5. Exit 1. For 1. Listing: Display a special listing of files showing their date of modification and access time (side by side) along with their... (2 Replies)
Discussion started by: bab123
2 Replies

9. UNIX for Dummies Questions & Answers

VARIABLE HANDLING in UNIX

Hi All, :confused: can anyone throw some light on variable handling in UNIX script?? I want to know about the local variables we declare inside the UNIX script. e.g. i=10 OR cat file1 | while read line do echo $line done etc. Does UNIX have any data types Can some one... (1 Reply)
Discussion started by: VENC22
1 Replies

10. Shell Programming and Scripting

handling spaces in unix

I am testing a ksh script for email. In the script I receive several parameters. One of them is a subject. The subject may contain spaces. Ex. Test this. When I am running the script on telnet to test, how should the syntax at the command line be written. I have this: ksh ResendE.sh '001111'... (2 Replies)
Discussion started by: supercbw
2 Replies
Login or Register to Ask a Question