Reading input files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading input files
# 1  
Old 01-14-2009
Reading input files

Okay, so I've looked on here and found some similar things, but not exactly what I am looking for. I am working on creating a script that can back up some files, based on the contents of another file - the configuration file.

First file contains the files to back up - we'll call this backup.conf:

(Format of file: SERVICE:/path/to/file:destination)

ETC:/etc/hosts:./etc
ETC:/etc/netmasks:./etc
DHCP:/etc/default/dhcp:./etc

The next file is the backup script. It contains an "awk" statement to break down the backup.conf file so that it only needs to gather specific parts, based on what needs to be backed up. We'll call this backup:

#awk statement - creates temporary file with targets and destinations
sed '/^ *#/d;s/#.*//' backup.conf | grep -v ^$ | grep ETC | awk -F: '{print $2" "$3}' >! /tmp/backupfiles

#while loop to process output
while read line
do
if [ -f $line ]
then
/usr/bin/cp $line $target
fi
done < /tmp/backupfiles

I realize that $target is not set here. This is the problem that I am running into, and this is just to give you an idea of what I am attempting to do. Basically, the while statement needs to be able to take each line from /tmp/backupfiles and as it reads it, break it down into two variables, the file to back backed up ($line) and the location to back it up to ($target). Using $line is can also verify that the file exists and is available before trying to back it up.

Any suggestions? I am using "ksh" for this on Solaris systems.
# 2  
Old 01-14-2009
Quote:
Originally Posted by pdxwarrior
Okay, so I've looked on here and found some similar things, but not exactly what I am looking for. I am working on creating a script that can back up some files, based on the contents of another file - the configuration file.

First file contains the files to back up - we'll call this backup.conf:

(Format of file: SERVICE:/path/to/file:destination)

ETC:/etc/hosts:./etc
ETC:/etc/netmasks:./etc
DHCP:/etc/default/dhcp:./etc

The next file is the backup script. It contains an "awk" statement to break down the backup.conf file so that it only needs to gather specific parts, based on what needs to be backed up. We'll call this backup:

Please put code inside [code] tags.
Quote:
Code:
#awk statement - creates temporary file with targets and destinations
sed '/^ *#/d;s/#.*//' backup.conf | grep -v ^$ | grep ETC | awk -F: '{print $2" "$3}' >! /tmp/backupfiles


You are using 4 commands where one will suffice:

Code:
awk '/^ *#/ { next }
{ sub( /#.*/, "") }
/^$/ {next}
!/ETC/ { next }
{print $2" "$3}'  backup.conf >! /tmp/backupfiles

Quote:
Code:
#while loop to process output
while read line
do
   if [ -f $line ]
   then
       /usr/bin/cp $line $target
   fi
done < /tmp/backupfiles

I realize that $target is not set here. This is the problem that I am running into, and this is just to give you an idea of what I am attempting to do. Basically, the while statement needs to be able to take each line from /tmp/backupfiles and as it reads it, break it down into two variables, the file to back backed up ($line) and the location to back it up to ($target). Using $line is can also verify that the file exists and is available before trying to back it up.

Any suggestions? I am using "ksh" for this on Solaris systems.
Code:
while IFS=: read service file target
do
   if [ -f "$file" ]
   then
       /usr/bin/cp "$line" "$target"
   fi
done < /tmp/backupfiles

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Open Source

Splitting files using awk and reading filename value from input data

I have a process that requires me to read data from huge log files and find the most recent entry on a per-user basis. The number of users may fluctuate wildly month to month, so I can't code for it with names or a set number of variables to capture the data, and the files are large so I don't... (7 Replies)
Discussion started by: rbatte1
7 Replies

2. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

I have a environment property file which contains: Input file: value1 = url1 value2 = url2 value3 = url3 and so on. I need to search all *.xml files under directory for value1 and replace it with url1. Same thing I have to do for all values mentioned in input file. I need script in unix bash... (7 Replies)
Discussion started by: Shamkamde
7 Replies

3. Shell Programming and Scripting

Help with reading two input files in awk

Hello, I'm trying to write an awk program that reads two files inputs. example, file 1: 0.00017835 0.000176738 0.00018811 0.000189504 0.000188155 0.000180065 0.000178991 0.000178252 0.000182513 file 2: 1.7871769E-05 1.5139576E-16 1.5140196E-16 1.5139874E-16 1.7827407E-04 ... (5 Replies)
Discussion started by: joseamck
5 Replies

4. Shell Programming and Scripting

Reading from standard input

So, I am new to shell scripting and have a few problems. I know how to read from standard input but I do not know how to really compare it to say, a character. I am trying to compare it to a character and anything exceeding just a character, the user will get an output message, but the program... (7 Replies)
Discussion started by: Bungkai
7 Replies

5. Shell Programming and Scripting

Perl Script Not Reading Input Files Correctly

This is one of the strangest things that's happening to me. I'm writing a new Perl script that is trying to read a file. The file is originally in .mof format, but I also saved the contents into a .txt file. As a simple test, I wrote this: #!/user/bin/perl -w use strict; ... (3 Replies)
Discussion started by: kooshi
3 Replies

6. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

Hi, I have a folder which contains multiple config.xml files and one input file, Please see the below format. Config Files format looks like :- Code: <application name="SAMPLE-ARCHIVE"> <NVPairs name="Global Variables"> <NameValuePair> ... (0 Replies)
Discussion started by: haiksuresh
0 Replies

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

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

9. Shell Programming and Scripting

awk reading 2 input files but not getting expected value

I'm reading 2 input files but not getting expected value. I should get an alpha value on file_1_data but not getting any. Please help. >cat test6.sh awk ' FILENAME==ARGV { file_1_data=$0; print "----- 1 Line " NR " -----" $1; next } FILENAME==ARGV { file_2_data=$0; print "----- 2... (1 Reply)
Discussion started by: pdtak
1 Replies

10. Shell Programming and Scripting

reading from input

Hi guys , As you know normally ' read ' statement waits for the user to press enter and then terminates the input ............. Can anyone of u tell me how do i read a single character from input without waiting for the user to press enter ................ Thanks, Nagesh. (1 Reply)
Discussion started by: nageshrc
1 Replies
Login or Register to Ask a Question