Constructing a string by reading the contents of 2 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Constructing a string by reading the contents of 2 files
# 1  
Old 04-28-2010
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:

File 1 - this is simply a list of decimal values that refer to parameters the remote device needs to refer to. There are 100 records in this file. The format is:
Code:
17253
17927
18291
18485
18676
...

File 2 - this is a CSV list of users and extension numbers. There could be anything from 1 to 100 records in this file. The format is:
Code:
Bob Smith,0501234567
Steve Gray,0507654321
Craig Hancox,0506515272
...

What I need to do is this define a string which will ultimately get sent to the phone using wget. This is construced in the following way:

1) Read the first record in File 2 (Name/Number)

2) If the record exists (i.e., not EOF), read the first record of file 1

3) Construct a string which is basically File 1: Data plus 'some text' plus File 2: Field 1 plus 'some text' plus File 2: Field 2 plus 'some text'

(note that File 2:Field 1 may contain spaces and it would really be useful of I could replace these with %20).

4) Repeat the above sequence and concatenate the results into the primary string

5) Exit the string construction phase once EOF has been reached of File 2

I can do the wget side fine - this is working... its just the string construction thing I am having real issues with. Problems I have had have been:
  • Getting the script to exit after EOF of File 2
  • Using awk to do the string manipulation on what it reads out of File 2

Any help or pointers would be much appreciated and i would be eternally grateful!

Best regards,

Sean

Last edited by Franklin52; 04-28-2010 at 03:11 AM.. Reason: adding code tags
# 2  
Old 04-28-2010
one way:

Code:
exec 4<22
while read record
do
 IFS="," read user extn <&4
 if [ "$user" ] && [ "$extn" ]; then
 echo "$record some text $user some text $extn some text"
 fi
done < 11
4<&-

11 is the file1, 22 is the file2
# 3  
Old 04-28-2010
Hi,

Try this,

Code:
paste -d, 11 22 | sed 's/.*,$/d' | sed 's/,/ SOME TEXT /g'

11 is the file1, 22 is the file2
# 4  
Old 05-02-2010
Thanks guys - this is exactly what I wanted - obviously I have some way to go! Anyway thanks a million...

Cheers

Sean
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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 #!/bin/ksh for i in $( cat $a/b/c.txt ) do echo "... (1 Reply)
Discussion started by: Aditya_001
1 Replies

2. Shell Programming and Scripting

Reading the contents of the file and splitting using ksh

We're using a ksh script for installing one product. I've another config file, I'd need to read this configuration file from my main script Content of the Configuration file:... (2 Replies)
Discussion started by: bittu129
2 Replies

3. UNIX for Dummies Questions & Answers

Looping/Reading file contents not working

Hi, I am doing something basic, but I am missing something. Im trying to read the contents of a file and taking those values and connecting to a database. However, it only connect to one (or reads in) value and then exists. Here is what it looks like: listname.txt db1 db2 db3 Script:... (15 Replies)
Discussion started by: DBnixUser
15 Replies

4. Shell Programming and Scripting

Reading file contents until a keyword

Hi Guys, I need to read a file until I find a blank line. and in the next iteration I want to continue reading from the line I find a keyword. For ex: my file looks like PDS_JOB_ALIAS CRITERIA_ITEM_TYPE PDS_JOB_CRITERIA_ITEM CRITERIA_ITEM_TYPE First I want to read the file... (2 Replies)
Discussion started by: infintenumbers
2 Replies

5. Shell Programming and Scripting

Formatting Report and Reading data and fetching the details from contents file

Data I was trying to write shell script which will be return the output in the below format First i was trying to do these using sed. sed -n '/.ksh/p' mainksh.ksh sed -e 's/*\(.*\)/\1/g' mainksh.ksh $RUN_DIR, $SUB_DIR and the variables which will be defined in the profile file. when i am... (0 Replies)
Discussion started by: rameshds
0 Replies

6. UNIX for Dummies Questions & Answers

Replacing a particular string in all files in folder and file contents

I need to replace all filesnames in a folder as well as its content from AK6 to AK11. Eg Folder has files AK6-Create.xml, AK6-system.py etc.. the files names as well as contents should be changes to AK9-Create.xml, AK9-system.py etc All files are xml and python scripts. ---------- Post... (0 Replies)
Discussion started by: Candid247
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 and printing one by one contents of a file

I have a file which has following contents: localhost_IP_SIP_1233026552455.xml localhost_IP_SIP_1233026552460.xml localhost_IP_SIP_1233026552467.xml localhost_IP_SIP_1233026552759.xml localhost_IP_SIP_1233026552969.xml localhost_IP_SIP_1233026552975.xml ... (2 Replies)
Discussion started by: Aditya.Gurgaon
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. AIX

reading contents on a 4mm tape

I am experimenting with one of my TSM processes, and I am trying to write something to a 4mm tape. I tried to use the tctl command, but, I was unsuccessful. what AiX command can I run in order to see if my output is on the tape ? Thank You ! (0 Replies)
Discussion started by: DATDANGGADUDE
0 Replies
Login or Register to Ask a Question