Reading multiple lines in a file

Hello, I am new in shell scripting. I need help regarding following.

I have 4 files generated by backups daily. I have stored the names of these 4 files into one file. i.e I have 4 files names as a, b, c & d and these names have been put into one file abcd.txt.

Now I want to cat each file in abcd.txt and grep for a string and put the output in another file.

I am not able to sort it out for myself and would appreciate if anyone can help me in this.

Regards,
Ali

Hello Ali Sarwar,

Welcome to forums, enjoy learning/knowledge sharing here. For your query could you please try following and let me know if this helps you.

while read line
do
 find -type f -name $line -exec grep "Your_String" >> Output_file
done < "abcd.txt"

Also above code is NOT tested as your requirement was not that clear, so if above doesn't help you please come with more details and all conditions which you have in your requirement too, hope this helps you.

Thanks,
R. Singh

You seem to have at least a vague idea on how to tackle your problem, which is good! Please show your attempts and where you're stuck.

Thank you Ravinder for your help. Following is the detailed info for which I seek help. I have 4 log files auto generated by some binary with the names as given below.

-rw------- 1 nz   nz   7.9K May  8 14:07 backupsvr.16897.2016-05-08.log
-rw------- 1 nz   nz   4.1K May  8 14:23 backupsvr.7474.2016-05-08.log
-rw------- 1 nz   nz   162K May  8 14:38 backupsvr.25848.2016-05-08.log
-rw------- 1 nz   nz    47K May  8 18:40 backupsvr.29230.2016-05-08.log

What I did is that I listed the files of my choice of date and put the names into another file by using the following command.

ls *`date +%Y-%m-08`* > output.txt

Not the new file "output.txt" contains all 4 file names from 08th May 2016.

#cat output.txt
backupsvr.16897.2016-05-08.log
backupsvr.25848.2016-05-08.log
backupsvr.29230.2016-05-08.log
backupsvr.7474.2016-05-08.log

Now I want to fetch 2nd last line from each file and put them into one new file. i.e 2nd last line of 1st file is as "a completed successfully". 2nd file's 2nd last line is "b completed successfully".

I want these 2nd last line into one final file which output would be as below.

#cat finalfile.txt
a completed successfully
b completed successfully
c completed successfully
d completed successfully

I hope my requirement is clear now. If not please let me know and I will try to explain it more.

Regards,
Ali

Hello Ali Sarwar,

Could you please use following and let me know if this helps you.

while read line
do
    tail -2 $line | head -1 >> finalfile.txt
done < "output.txt"

Thanks,
R. Singh

1 Like

Yes, that worked perfectly as I required. May God bless you Ravinder. Thanks a lot for your time and help. Much appreciated.

Regards,
Ali

1 Like

Best practise is to produce the output file in one stroke,
and to quote command arguments

while read line
do
    tail -2 "$line" | head -1
done < output.txt > finalfile.txt

Thank you everyone for your valuable time. Both Ravinder & MadeinGermany fulfilled my requirement.

**Thumbs Up

---------- Post updated at 03:35 PM ---------- Previous update was at 03:34 PM ----------

Thank you everyone for your valuable time. Both Ravinder & MadeinGermany fulfilled my requirement.

**Thumbs Up:b: