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?
# 8  
Old 03-17-2017
Weren't it time to become familiar? With three to four line files you won't notice a difference between messing around with shell scripts and tools designed for text processing (awk is not the only example, look at perl, or sed, or other). Have a look at post1 and post8: 45 min to process a huge file in shell compared to less than 1 min using awk! And that's not the only thread recently citing tremendous performance improvements.

Last edited by RudiC; 03-21-2017 at 05:56 AM.. Reason: typo
# 9  
Old 03-20-2017
Quote:
Originally Posted by RudiC
Weren't it time to become familiar? With three to four line files you won't notice a difference between messing around with shell scripts and tools designed for text processing (awk is not the only example, look at perl, or sed, or other). Have alook at post1 and post8: 45 min to process a huge file in shell compared to less than 1 min using awk! And that's not the only thread recently citing tremendous performance improvements.
Hi Rudic,

I modified the code you provided, it works fine
I tried to add one more logic, if zone and cd doesn't exist in file2, create a error file. I got error. would you please take a look. thanks.




my file is
FILE1
Code:
AAA FIRST 20170203
BBB SECOND 20170204
CCC THIRD 20170205

FILE2
Code:
AAA     FIRST   file1.txt       content1=
BBB     SECOND  file2.txt       content2=

below is my code
Code:
nawk '{IX = $1 "," $2} NR == FNR{FN[IX] = $3; CT[IX] = $4; next}; {if(IX in FN) print CT[IX]$3 > FN[IX]; else {print "error" > abc}}
' FILE2 FILE1

when running above code, it creates two files as expected, but it throw error for the error record

Code:
nawk: null file name in print or getline
 input record number 3, file FILE1
 source line number 1

I use nawk because my OS is solaris. below is my OS infor:
Code:
SunOS xxxxxxxx 5.10 Generic_150400-04 sun4v sparc SUNW,SPARC-Enterprise-T5220


Last edited by Torhong; 03-20-2017 at 09:43 PM..
# 10  
Old 03-20-2017
Quote:
Originally Posted by Torhong
Hi Rudic,

I modified the code you provided, it works fine
I tried to add one more logic, if zone and cd doesn't exist in file2, create a error file. I got error. would you please take a look. thanks.


one more thing I want to add is if zone and cd doesn't exist in file2, create a error file and exit with 1.

my file is
FILE1
Code:
AAA FIRST 20170203
BBB SECOND 20170204
CCC THIRD 20170205

FILE2
Code:
AAA     FIRST   file1.txt       content1=
BBB     SECOND  file2.txt       content2=

below is my code
Code:
nawk '{IX = $1 "," $2} NR == FNR{FN[IX] = $3; CT[IX] = $4; next}; {if(IX in FN) print CT[IX]$3 > FN[IX]; else {print "error" > abc}}
' FILE2 FILE1

when running above code, it creates two files as expected, but it throw error for the error record

Code:
nawk: null file name in print or getline
 input record number 3, file FILE1
 source line number 1

I use nawk because my OS is solaris. below is my OS infor:
Code:
SunOS xxxxxxxx 5.10 Generic_150400-04 sun4v sparc SUNW,SPARC-Enterprise-T5220

In awk (and nawk) abc is a variable name and since you haven't assigned anything to that variables, its contents is an empty string (if a string is expected) or zero (if a number is expected). Assuming that you want the error log file to be named abc, try changing:
Code:
print "error" > abc

to:
Code:
print "error" > "abc"

And, to cover the exit status issue:
Code:
nawk '
{	IX = $1 "," $2
}
NR == FNR {
	FN[IX] = $3
	CT[IX] = $4
	next
}
{	if(IX in FN)
		print CT[IX]$3 > FN[IX]
	else {	print "error" > "abc"
		ev = 1
	}
}
END {	exit ev
}' FILE2 FILE1

Or, if you want to exit immediately if an error is found, change the:
Code:
		ev = 1

to:
Code:
		exit 1

and remove the END clause.
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 03-21-2017
Quote:
Originally Posted by Don Cragun
In awk (and nawk) abc is a variable name and since you haven't assigned anything to that variables, its contents is an empty string (if a string is expected) or zero (if a number is expected). Assuming that you want the error log file to be named abc, try changing:
Code:
print "error" > abc

to:
Code:
print "error" > "abc"

And, to cover the exit status issue:
Code:
nawk '
{    IX = $1 "," $2
}
NR == FNR {
    FN[IX] = $3
    CT[IX] = $4
    next
}
{    if(IX in FN)
        print CT[IX]$3 > FN[IX]
    else {    print "error" > "abc"
        ev = 1
    }
}
END {    exit ev
}' FILE2 FILE1

Or, if you want to exit immediately if an error is found, change the:
Code:
        ev = 1

to:
Code:
        exit 1

and remove the END clause.
thank you Don Cragun. this is exactly I want.
SmilieSmilieSmilie

---------- Post updated 03-21-17 at 12:38 PM ---------- Previous update was 03-20-17 at 09:48 PM ----------

Quote:
Originally Posted by Don Cragun
In awk (and nawk) abc is a variable name and since you haven't assigned anything to that variables, its contents is an empty string (if a string is expected) or zero (if a number is expected). Assuming that you want the error log file to be named abc, try changing:
Code:
print "error" > abc

to:
Code:
print "error" > "abc"

And, to cover the exit status issue:
Code:
nawk '
{    IX = $1 "," $2
}
NR == FNR {
    FN[IX] = $3
    CT[IX] = $4
    next
}
{    if(IX in FN)
        print CT[IX]$3 > FN[IX]
    else {    print "error" > "abc"
        ev = 1
    }
}
END {    exit ev
}' FILE2 FILE1

Or, if you want to exit immediately if an error is found, change the:
Code:
        ev = 1

to:
Code:
        exit 1

and remove the END clause.
Hi Don Cragun,

the code works perfectly. I need to add some parameters for file path and file name,

I tried below script, but it give me error as below.

would you please take a look the issue if you are free.

thanks in advance

Code:
 error message:
 nawk: division by zero
 input record number 1, file sample.txt
 source line number 10

Code:
 
 tfiledir=/home
errdir=/home
errfilename=miss_records.txt
 nawk -F","  -v tdir="$tfiledir" -v edir="$errdir" -v efilename="$errfilename" '
{       IX = $1 "," $2
}
NR == FNR {
        FN[IX] = $3
        CT[IX] = $4
        next
}
{       if(IX in FN)
                print CT[IX]$3 > tdir/FN[IX]
        else {  print "error" > edir/efilename
                ev = 1
        }
}
END {   exit ev
}' $1 $2

# 12  
Old 03-21-2017
You fell into the same trap as you did in post#9. In programming, you need to differentiate between strings, and variables and operators. And, you need to know how e.g. awk deals with variables' types, converting strings into numerics and vice versa if need be. Alphabetic only strings result in a numeric value of 0.

When you're writing tdir/FN[IX] in line 10 (as indicated in the error message), awk tries to divide tdir (a string, i.e. 0 which doesn't hurt) by FN[IX] (another string, i.e. 0, but hurts in this case).

Try, as you've beed told before, to enclose the / in double quotes: "/".
# 13  
Old 03-21-2017
Quote:
Originally Posted by RudiC
You fell into the same trap as you did in post#9. In programming, you need to differentiate between strings, and variables and operators. And, you need to know how e.g. awk deals with variables' types, converting strings into numerics and vice versa if need be. Alphabetic only strings result in a numeric value of 0.

When you're writing tdir/FN[IX] in line 10 (as indicated in the error message), awk tries to divide tdir (a string, i.e. 0 which doesn't hurt) by FN[IX] (another string, i.e. 0, but hurts in this case).

Try, as you've beed told before, to enclose the / in double quotes: "/".
Hi RudiC,
thanks for help.

I tried "/", it failed with below error message:

Thanks.

Code:
nawk: syntax error at source line 10
 context is
                        print CT[IDX]$3 > >>>  tdir"/" <<< 
nawk: illegal statement at source line 10
nawk: syntax error at source line 11


Last edited by Torhong; 03-21-2017 at 03:45 PM..
# 14  
Old 03-21-2017
Some awk versions don't like concatenated redirection targets - try parenthesizing them.
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