Reading contents of files from a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading contents of files from a directory
# 1  
Old 02-25-2014
Reading contents of files from a directory

I have a directory with the files,

1st: I want to Diplay each filename, underline it
2nd: Display the contents under the filename and
3rd: Redirect the above content to other file
4th: Remove the files from the directory

Code:
 
#!/bin/ksh
for i in $( cat $a/b/c.txt )
do
echo " Contents of the files "  > $t1/t2/content.txt
echo " ===================================================== " >> $t1/t2/content.txt
echo $i >> $t1/t2/content.txt; cat $i >> $t1/t2/content.txt
rm $i
done



Code:
 
 
Desired out put in redirected file:
 
Filename1:
==========
Content of Filname1
Filename2:
=========
Content of Filename2


Thanks
# 2  
Old 02-25-2014
If you want to display the output on stdout and save it in file as well, you can use the tee command.
Try:
Code:
for file in /path/to/your/directory/*.txt
do
  echo "$file" |  tee -a out_file.txt
  echo "======================================"
  cat $file | tee -a out_file.txt
  echo | tee -a out_file.txt
  mv $file ${file}.old  # change this to rm to remove the file.
done

When you test this script make sure that the correct files are removed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to find and replace contents of files in directory

Hi all This is my first post. Please bear with me with all my mistakes. I started learning shell since couple of days now and this might be quite basic for all, i want to search for files in a directory containing specific string and replace it with new string. The code i wrote is quite bulky... (2 Replies)
Discussion started by: theprogrammer
2 Replies

2. Shell Programming and Scripting

Need Help with Bash - comparing directory contents with list of files

Hey guys, need help with a script I'm trying to write. Basically I need to compare the contents of a folder called "profiles" with a list of files called "template". when the file matches the contents of the folder it needs to set a variable called "checked" to "1" Cookies to anyone... (4 Replies)
Discussion started by: Scriporium
4 Replies

3. UNIX for Dummies Questions & Answers

Need Help in reading N days files from a Directory & combining the files

Hi All, Request your expertise in tackling one requirement in my project,(i dont have much expertise in Shell Scripting). The requirement is as below, 1) We store the last run date of a process in a file. When the batch run the next time, it should read this file, get the last run date from... (1 Reply)
Discussion started by: dsfreddie
1 Replies

4. Shell Programming and Scripting

reading files from a directory.

Can some body help me to code this? go to a specific directory.(/home/abcd/test) file1.txt, file2.txt, ... .. filen.txt read the files in side the folder 'test' and print the content of each file into other folder in the same directory lets say(testresult) with the same file name... (4 Replies)
Discussion started by: rocking77
4 Replies

5. Shell Programming and Scripting

Constructing a string by reading the contents of 2 files

Hi all - I am trying to write a script to generate a string that gets sent to an IP phone using wget. This is to upload a phone book of up to 100 entries so the string needs to be constructed in one 'hit'. I am a relative newbie to shell scripts but willing to try anything! I have 2 files: ... (3 Replies)
Discussion started by: cakerack
3 Replies

6. UNIX for Dummies Questions & Answers

Reading files in script from another directory

Hi I'm trying to call my files from different directories in my script. Can you please help me. Here is my script: #!/bin/bash #---------------------------------------------------------------------------------------------------------------------- #This script allows the user... (1 Reply)
Discussion started by: ladyAnne
1 Replies

7. AIX

reading files from a directory.

Hi all, I have a shell script where it processes a set of files from a particular directory (shared location among 4 servers). i.e. under this directory /shared/work/ I have a set of files that needs to be processed. Since the number of files are alot, I have this script to be run from 4... (2 Replies)
Discussion started by: haroon_a
2 Replies

8. Shell Programming and Scripting

Create Multiple files by reading a input file and changing the contents

Being new to this area .I have been assigned a task which i am unable to do . Can any one please help me . Hi I have requirement where i have input file XYZ_111_999_YYYYMMDD_1.TXT and with header and series of Numbers and Footer. I want to create a mutiple output files with each file having a... (2 Replies)
Discussion started by: bhargavkr
2 Replies

9. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies

10. Shell Programming and Scripting

Reading files in directory

Hi Everyone , have a nice day i need a help on this thing algo is something like in certain path like /root/user1 i have many files , i need a code which could open every file one by one and then each file has contents like this <moid>CcnCounters=CAPv3-Received-Total-Requests, Source =... (3 Replies)
Discussion started by: Dastard
3 Replies
Login or Register to Ask a Question