How do i iterate thru each line of a file in shell scripting?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do i iterate thru each line of a file in shell scripting?
# 1  
Old 09-05-2007
How do i iterate thru each line of a file in shell scripting?

Hi All,

I need to execute some commands on each line of a file.
How do i iterate thru each line of a file?

In detail:

First i will have to go to the first line of the file and execute a series of commands on it and then take the second line of the file, execute a series of steps and so on. I need to repeat this process until the last line of the file.

Please help me.

Thanks in anticipation,
Ravi.
# 2  
Old 09-05-2007
Welcome to the forums. The forum has a search feature which you can use to search for similiar posts that have come up before. See this post on the possible ways to parse a file - https://www.unix.com/tips-and-tutoria...se-a-file.html
# 3  
Old 09-06-2007
Quote:
Originally Posted by vino
Welcome to the forums. The forum has a search feature which you can use to search for similiar posts that have come up before. See this post on the possible ways to parse a file - https://www.unix.com/tips-and-tutoria...se-a-file.html

I wouldn't recommend the script in that post for a number of reasons.

For a start, it will fail on filenames containing spaces or other pathological characters.

It uses non-standard syntax that will not work in many POSIX shells.

It does not correctly read the entire line in some of the functions.

It uses a command that is no longer found on most systems (line).

It is an overwhelmingly large script for a person who needs to ask this question, and there are no useful comments to the functions.


The correct answer to the question is "use a while loop", as in this example:

Code:
while IFS= read -r line
do
   printf "%s\n" "$line"
done < FILE

There are, of course, variations on this, and for a large file, it is probably better to use sed or awk, depending on what needs to be done with the file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Scripting , need to search and print a line that contains a specific pattern

Take example of below file. abc.txt nas1:/abc/test/test1 /test nas1:/abc/test/test1/test2 /test/abc nas1:/abc/test/ Now i have a variable that contains "nas1:/abc/test/test1" value , so i need to search the above file for this variable and print only this line. ... (14 Replies)
Discussion started by: mohit_vardhani
14 Replies

2. UNIX for Dummies Questions & Answers

Script to iterate all command line arguments

Hi guys, I am having trouble with this script. What i want it to do is to iterate all command line arguments in reverse order. The code below does this fine but i need the output to print the words on separate lines instead of one line: #!/bin/bash #Takes in the arguments and displays them... (7 Replies)
Discussion started by: pipeline2012
7 Replies

3. Shell Programming and Scripting

Need one line scripting on Unix shell..help!!

Hi Experts, I need one line script for the below requirement. First it should check if a directory with todays date exist, and if not create it and move the PDF file to that directory. thanks in advance!!! (2 Replies)
Discussion started by: eeegopikannan
2 Replies

4. Shell Programming and Scripting

How to compare a command line parameter with -- in shell scripting

Hi, I need to check if a parameter provided at the command line is equal to --.How can i do that ? Please help me. Thanks and Regards, Padmini (4 Replies)
Discussion started by: padmisri
4 Replies

5. Shell Programming and Scripting

Shell - How to iterate through all the files in a directory?

Hi, I have a directory call Test, which contains files "a", b", "c", etc. I want to go through all of the files inside Test and remove any empty file. How would I do that with shell csh? So far I got... #!/bin/csh if (($#argv == 0) || ($#argv > 1)) then echo "no argument or too... (2 Replies)
Discussion started by: teiji
2 Replies

6. Shell Programming and Scripting

add a line at the end of a file using shell scripting

Hi All , I am new to shell scripting. i have a shell script(which is executed as a super user) , i want to it to do one more job for me. ie mounting a directory over other using lofs . i have done it manually using 1) using mount command mount -F lofs /export/home/dju /dju 2)... (4 Replies)
Discussion started by: meet123321
4 Replies

7. Shell Programming and Scripting

Delete a portion of a line using shell scripting

Hi all, I am new to awk programs.I have a file like this 1234567@2345||adcbdefhij: asgdfdasdfhhfd-asdfasd-dsfasdf |0.678|0.0|0.213 1234567@2345||adcbdefhij: ashhfd-asdfasd-dsfasdf |0.129|0.0|0.411 1234567@2345||adcbdefhij: asd-aasd-dasdf |0.223|0.0|0.276 I want to delete the text which... (3 Replies)
Discussion started by: Loy81
3 Replies

8. Shell Programming and Scripting

Need help in splitting a line into fields in shell scripting

I have a line of more than 3000 bytes which will contain & as fields separator..I am using following awk command ..Its working but its not accepting the line more than 3000 bytes...Anyother alternate solution even in othe shell command also fine... awk -F '&' '{for( i=1; i<=NF; i++ ) print $i}'... (2 Replies)
Discussion started by: punithavel
2 Replies

9. Shell Programming and Scripting

End of Line in Shell Scripting

I have a file containing records seperated by delimiter '|'.A record is contained on 2 or 3 lines. Now I need a shell script which could write all records with in two '|' character in one line.....for example..... |R1........................R1 R1..........................R1... (2 Replies)
Discussion started by: 33junaid
2 Replies

10. Shell Programming and Scripting

who to deal with whole line in shell scripting

I have a problem in my HP Unix system , it is briefly : I have two files : Myfile and Script.sh : Myfile contain this lines : String1 string2 string3 string5 String1 string2 string3 string5 String1 string2 string3 string5 ... (2 Replies)
Discussion started by: KSA
2 Replies
Login or Register to Ask a Question