Read the file line by line and do something with lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read the file line by line and do something with lines
# 1  
Old 06-23-2018
Read the file line by line and do something with lines

I have a file

Code:
file_name_O.txt


The file can have different number of other files names or nothing
I will check

Code:
cnt=`wc -l file_name_0.txt`
if [ "cnt" -eq "0" ];then
    exit 1
fi


Now I have to start checking file names, i.e. read txt file line by line. If amount of ,lines equal 1, I can do it
Otherwise I think I have to put it in for loop


How can I do it? while (1) is not working.
I don't know if ksh support for loop


Thanks for contribution
# 2  
Old 06-24-2018
Quote:
Originally Posted by digioleg54
. . .I don't know if ksh support for loop . . .

The first choice for info on ksh is man ksh:
Quote:
for vname [ in word ... ] ;do list ;done
Each time a for command is executed, vname is set to the next word taken from the in word list.
You don't need the wc upfront - assign a logical variable in the loop; if the file is empty, it won't have changed its value.
# 3  
Old 06-24-2018
while in combination with read works fine
Code:
while read line
do
  echo "do somthing with $line"
done < file_name_0.txt

# 4  
Old 06-24-2018
The last loop doesn't support KSH.
Better is the following:


Code:
for file in $(cat file_name.txt)
do
        print "FILE is $file"
done


Thanks for contribution
# 5  
Old 06-24-2018
Quote:
Originally Posted by digioleg54
The last loop doesn't support KSH.
Better is the following:


Code:
for file in $(cat file_name.txt)
do
        print "FILE is $file"
done


Thanks for contribution
Hi digioleg54,
I have no idea what you mean by "The last loop doesn't support KSH." Any POSIX shell and any shell based on Bourne shell syntax does support the while loop MadeInGermany suggested:
Code:
while read line
do
  echo "do somthing with $line"
done < file_name_0.txt

And, it is much more efficient and more likely to work than the loop you suggested above. Your has to start up another shell for the command substitution, run the cat utility to get the contents of file_name_0.txt, and will not work correctly if any filename contained in file_name_0.txt contains any IFS characters (by default, including <space> and <tab>). The code MadeInGermany works correctly in this case while your code does not.

While it is true that the code MadeInGermany suggested will not with if you are using csh or one of its derivatives, the code you suggested will not work with those shells either.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Read file lines and pass line values as arguments.

Dears, Need help to implement below requirement A file (detail.txt)contain : 1st column: Stream 2nd column: PathAddress 3rd column: Counterlimit 4th column: TransactionDateColumn 5th column: DateType 6th column: SleepValue 7th column: Status Need to write a... (1 Reply)
Discussion started by: sadique.manzar
1 Replies

3. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

4. 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

5. Shell Programming and Scripting

read one line file and separate into multiple lines

I have one long line text with semicolon used as separator between values in that line. Now, I want to separate the line into multiple line right after every 29th field. example input line: ... (1 Reply)
Discussion started by: erlanq
1 Replies

6. Shell Programming and Scripting

read one line file and separate into multiple lines

I have one long line text with semicolon used as separator between values in that line. Now, I want to separate the line into multiple line right after every 29th field. example input line: ... (2 Replies)
Discussion started by: erlanq
2 Replies

7. Shell Programming and Scripting

Read file line by line and process the line to generate another file

Hi, i have file which contains data as below(Only sample shown, it may contain more data similar to the one shown here) i need to read this file line by line and generate an output file like the one below i.e based on N value the number of MSISDNs will vary, if N=1 then the following... (14 Replies)
Discussion started by: aemunathan
14 Replies

8. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

9. Shell Programming and Scripting

Read the lines within file and send each line to different new file

I need help.....I have number of data files and I need to read the contains of each file which is three lines by awk commands or script, and send each line within the data file to different new file . That means I will have three files as a result, the first file will contains the first lines for... (4 Replies)
Discussion started by: aldreho
4 Replies

10. Shell Programming and Scripting

cat file1 read line-per-line then grep -A 15 lines down in fileb

STEP 1 # Set variable FILE=/tmp/mainfile SEARCHFILE =/tmp/searchfile # THIS IS THE MAIN FILE. cat /tmp/mainfile Interface Ethernet0/0 "outside", is up, line protocol is up Hardware is i82546GB rev03, BW 100 Mbps Full-Duplex(Full-duplex), 100 Mbps(100 Mbps) MAC address... (6 Replies)
Discussion started by: irongeekio
6 Replies
Login or Register to Ask a Question