How to create file and file content based existing information?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to create file and file content based existing information?
# 1  
Old 03-16-2017
How to create file and file content based existing information?

Hi Gurus,
I am SQL developer and new unix user.
I need to create some file and file content based on information in two files.

I have one file contains basic information below file1 and another exception file file2. the rule is if "zone' and "cd" in file1 exists in file2, then file name is FILENAME in file2, file content is CONTENT in file2. if "zone" and "cd" doesn't exist in file2, then file name is ZONE_CD.file, file content is DT_dat. below are sample files and expected result.

I want to use while loop read file1, then somehow check file2 the zone and Cd, but I don't know how to use two items check. I want to use "grep", but I need to compare two items.

any input is welcome.

thanks in advance.


file1
Code:
 
  10 ABC 20170202 
 10 AAA 20170101 
 20 BBB 20170303

file2
Code:
 
 10 ABC SPECIALFILENAME.sh SPECIAL_CONTENT

expected result:
Code:
 
  SPECIALFILENAME.sh SPECIAL_CONTENT 1
 10_AAA.file 20170101_dat 
 20_BBB.file 20170303_dat


Last edited by Torhong; 03-17-2017 at 08:14 AM..
# 2  
Old 03-17-2017
Quote:
Originally Posted by Torhong
Hi Gurus,
I am SQL developer and new unix user.
I need to create some file and file content based on information in two files.

I have one file contains basic information below file1 and another exception file file2. the rule is if "zone' and "cd" in file1 exists in file2, then file name is FILENAME in file2, file content is CONTENT in file2. if "zone" and "cd" doesn't exist in file2, then file name is ZONE_CD.file, file content is DT_dat. below are sample files and expected result.

I want to use while loop read file1, then somehow check file2 the zone and Cd, but I don't know how to use two items check. I want to use "grep", but I need to compare two items.

any input is welcome.

thanks in advance.


file1
Code:
 
 ZONE CD DT 
 10 ABC 20170202 
 10 AAA 20170101 
 20 BBB 20170303

file2
Code:
 
 ZONE CD FILENAME CONTENT 
 10 ABC SPECIALFILENAME.sh SPECIAL_CONTENT

expected result:
Code:
 
 filename filecontent 
 SPECIALFILENAME.sh SPECIAL_CONTENT 1
 0_AAA.file 20170101_dat 
 20_BBB.file 20170303_dat

What operating system are you using?

What shell are you using?

What have you tried to solve this problem on your own?

How does combining the two lines:
Code:
 ZONE CD DT 
 ZONE CD FILENAME CONTENT

produce the output:
Code:
 filename filecontent

? How does combining the two lines:
Code:
 10 ABC 20170202 
 10 ABC SPECIALFILENAME.sh SPECIAL_CONTENT

produce the output:
Code:
 SPECIALFILENAME.sh SPECIAL_CONTENT 1

? Why does converting the line:
Code:
 10 AAA 20170101

produce the output:
Code:
 0_AAA.file 20170101_dat

?

Why would you want to use a shell while loop and grep (multiple times) when the REs needed to isolate fields in a file are much more complicated that using awk to perform field splitting and record joining operations?

Is this a homework assignment? Homework and coursework questions can only be posted in the Homework & Coursework forum under special homework rules. If this is not homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.
# 3  
Old 03-17-2017
Hi Don,

I updated the file content. the first line is field name for reference. my os is solaris. shell is ksh.

this is not homework. the reason I want to use while loop is I am not familiar awk.

thanks.
# 4  
Old 03-17-2017
Having assumed that
- you being an sql developer as explained in post#1 NOT posting homework questions
- the original headers of both files shall be eliminated
- the 1 trailing in output line 2 and missing in line 3 is a hyphenation error
- usage of shell and grep are NOT compulsory

you might want to try
Code:
awk '
                {IX = $1 "," $2
                }
NR == FNR       {FN[IX] = $3
                 CT[IX] = $4
                 next
                }
FNR == 1        {print "filename filecontent"
                 next
                }
IX in FN        {print FN[IX], CT[IX]
                 next
                }
                {print $1 "_" $2 ".file", $3 "_dat"
                }
' file2 file1
filename filecontent
SPECIALFILENAME.sh SPECIAL_CONTENT
10_AAA.file 20170101_dat
20_BBB.file 20170303_dat

This User Gave Thanks to RudiC For This Post:
# 5  
Old 03-17-2017
thanks Guys,

the requirement changed a little, we will read from file1, and use field1 and field2 find record in file2 and get filename and file content from file2.

I tried to write with while loop, it works. just wondering if the code can be improved.
I saw all answers use awk.

I know a little bit of awk. I will check the doc to fully understand the syntax, otherwise I will have problem if I just copy and paste.

Code:
 
 #!/bin/ksh
IFS=","
 error_cnt=0
while read zone feed date
do
echo $zone, $feed, $date
        exist_flag=0
        while read zone1 feed1 fname fct
                do
                        if [ $zone == $zone1 ] && [ $feed == $feed1 ]; then
                                echo "export ${fct}=${date}" > ${fname}
                                exist_flag=1
                                break
                        fi
                done<metapull.txt
        if [ exist_flag -eq 0 ]; then
                echo $zone, $feed " doesn't exisit in meta file" >> miss_records.txt
                error_cnt=1
        fi
done<sample.txt
 if [ error_cnt -ne 0 ]; then
        echo "Some feeds missing detail information, please check file " miss_records.txt
        exit 1
fi


Last edited by Torhong; 03-17-2017 at 02:07 PM..
# 6  
Old 03-17-2017
With just glance at your script I see that
- both input files being read via what seems to be the same file descriptor - might work by accident but is sloppy programming; use different fd explicitly
- if it would work, for every line in file1 the entire file2 is being read - highly inefficient
- every time fname is found in the input files, its hitherto existing contents will be overwritten - mayhap undesired

Last edited by RudiC; 03-17-2017 at 01:50 PM..
# 7  
Old 03-17-2017
Quote:
Originally Posted by RudiC
With just glance at your script I see that
- both input files being read via what seems to be the same file descriptor - might work by accident but is sloppy programming; use different fd explicitly
- if it would work, for every line in file1 the entire file2 is being read - highly inefficient
- every time fname is found in the input files, its hitherto existing contents will be overwritten - mayhap undesired
Thanks RudiC.

you are right, the second loop is very inefficient. is there any way to fix it without using awk as I am not familiar with awk.

thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Copy the content from txt file and create a html file

I have a txt file with a list of error messages in a xml tag format, and each error message is separated with a identifier(endresult).Need to split that and copy and create a new html file.Error message has some special character. how to escape the special character and insert my data into the... (7 Replies)
Discussion started by: DevAakash
7 Replies

2. Shell Programming and Scripting

Shell Script to Dynamically Extract file content based on Parameters from a pdf file

Hi Guru's, I am new to shell scripting. I have a unique requirement: The system generates a single pdf(/tmp/ABC.pdf) file with Invoices for Multiple Customers, the format is something like this: Page1 >> Customer 1 >>Invoice1 + invoice 2 >> Page1 end Page2 >> Customer 2 >>Invoice 3 + Invoice 4... (3 Replies)
Discussion started by: DIps
3 Replies

3. Shell Programming and Scripting

Create shell script to extract unique information from one file to a new file.

Hi to all, I got this content/pattern from file http.log.20110808.gz mail1 httpd: Account Notice: close igchung@abc.com 2011/8/7 7:37:36 0:00:03 0 0 1 mail1 httpd: Account Information: login sastria9@abc.com proxy sid=gFp4DLm5HnU mail1 httpd: Account Notice: close sastria9@abc.com... (16 Replies)
Discussion started by: Mr_47
16 Replies

4. Shell Programming and Scripting

generate a file from existing file based on some rule

Hi guys, i have a file in below format.let me explain first what i am trying to do . i am making a file from this file , will rearrange these columns, then i will run several command(start/stop mentioned in this file) on unix environment. my requirements : 1. i have 27 servers , on each server... (4 Replies)
Discussion started by: deepakiniimt
4 Replies

5. Shell Programming and Scripting

Creating a new file based on existing file

Hello Guys , I need an another help regarding the below problem. I want to create a new file based on the existing file ,where two columns will be changed according to user input .(say column 4 and column 5) Please let me know how to proceed with Thanks (3 Replies)
Discussion started by: Pratik4891
3 Replies

6. Shell Programming and Scripting

how to create file.txt and add current date in file content

Hey guy, how to make bash script to create foo.txt file and add current date into file content and that file always append. example: today the script run and add today date into content foo.txt and tomorrow the script will run and add tomorrow date in content foo.txt without remove today... (3 Replies)
Discussion started by: chenboly
3 Replies

7. Shell Programming and Scripting

Script to Edit the file content and create new file

I have a requirement, which is as follows *. Folder contains list of xmls. Script has to create new xml files by copying the existing one and renaming it by appending "_pre.xml" at the end. *. Each file has multiple <Name>fileName</Name> entry. The script has to find the first occurance of... (1 Reply)
Discussion started by: sudesh.ach
1 Replies

8. UNIX for Advanced & Expert Users

Reading a file and sending mail based on content of the file

Hi Gurus, I am having an requirement. i have to read a list file which contains file names and send mail to different users based on the files in the list file. eg. if file a.txt exists then send a mail to a@a.com simillary for b.txt,c.txt etc. Thanks for your help, Nimu (6 Replies)
Discussion started by: nimu1979
6 Replies

9. Shell Programming and Scripting

Creating a csv file based on Existing file

Hi I am Newbie to Unix.Appreciate Help from forum user would loada b.Csv File(Below example) in /data/m/ directory.Program need to read the b.csc to extract certain column and create a new file /data/d/ directory as csv file with new name. User File Format 1232,samshouston,12345... (3 Replies)
Discussion started by: skywayterrace
3 Replies

10. Shell Programming and Scripting

i want to delete a file based on existing file in a directory

hi i am having four files in a directory.like 1)sampleRej 2)exampleRej 3)samplemain 4)examplemain my requirement is i have to search for the rejected files (sampleRej,exampleRej) in a directory.if these files in that directory then i have to delete the main files... (3 Replies)
Discussion started by: srivsn
3 Replies
Login or Register to Ask a Question