UNIX shell scripting - help needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX shell scripting - help needed
# 1  
Old 10-28-2015
UNIX shell scripting - help needed

Can anyone please help me to create the output file based on the attached input files using ksh script.

I have 2 input files
1) inputfile1.txt
2) inputfile2.txt

The script should accept 2 parametes:

1. Inputfile2.txt with 1-10,000 values ( sample file contains only 4 records)
2. Script reads the above file and generates one separate output file(outputfile.txt) .

The only value that needs replaced is the 107 from inputfile1.txt(3rd column - delimter | -search 107=) with the values in the 2nd inputfile (inputfile2.txt). No other value should be replaced from inputfile1.txt.

The output should contain total 4 lines.

Thanks

Last edited by fpmurphy; 10-28-2015 at 09:28 PM.. Reason: Remove code tags
# 2  
Old 10-29-2015
Your inputfile1.txt file has UNIX line terminators.
Your inputfile2.txt and outputfile.txt files have DOS line terminators.

The following seems to do what you want, but uses UNIX line terminators instead of DOS line terminators in the output it produces:
Code:
awk -F '[[.^.]]A107=[^^]+[[.^.]]A' '
{	sub(/\r$/, "")
}
NR == 1 {
	f1 = $1 "^A107="
	f2 = "^A" $2
	next
}
{	print f1 $0 f2
}' inputfile1.txt inputfile2.txt > outputfile.txt

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk. (Note that nawk on a Solaris/SunOS system will not work in this case; you need an awk that correctly processes ERE collating symbol bracket expressions.)
# 3  
Old 10-29-2015
Try also
Code:
awk 'FNR==NR {TMP=$0; next} {sub (/\^A107=[^^]*\^A/, "^A107=" $0 "^A", TMP); print TMP}' /tmp/inputfile1.txt /tmp/inputfile2.txt

If your files contain DOS line terminators, add a sub (/\r/,"") as Don Cragun proposed.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 10-29-2015
Quote:
Originally Posted by RudiC
Try also
Code:
awk 'FNR==NR {TMP=$0; next} {sub (/\^A107=[^^]*\^A/, "^A107=" $0 "^A", TMP); print TMP}' /tmp/inputfile1.txt /tmp/inputfile2.txt

If your files contain DOS line terminators, add a sub (/\r/,"") as Don Cragun proposed.
Unless I'm missing something, the code:
Code:
FNR==NR {TMP=$0; next}

in the above needs to be changed to:
Code:
NR == 1 {TMP=$0; next}

to keep from overwriting the template read from inputfile1.txt with the replacement data found on the first line of inputfile2.txt.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 5  
Old 10-30-2015
Well - yes and no. TMP wouldn't be overwritten by any line in inputfile2, but IF there were more than one line in inputfile1, only the last one were acted upon with my proposal, while only the first one were considered by Don Cragun's,
This User Gave Thanks to RudiC For This Post:
# 6  
Old 10-30-2015
Quote:
Originally Posted by RudiC
Well - yes and no. TMP wouldn't be overwritten by any line in inputfile2, but IF there were more than one line in inputfile1, only the last one were acted upon with my proposal, while only the first one were considered by Don Cragun's,
Yes. Sorry for confusing the issue.

In the first draft of my code, I had a typo that grabbed the 1st line from both files as the template (FNR == 1 instead of NR == 1) and when I saw your post a minute after my post, I thought you had fallen into the same trap. My mistake!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help needed in UNIX scripting

Hi, I have 2 variables which is having like the below data a=x.1 y.2 z.3 m.4 b=No (zero) rows read (0)~ No (zero) rows read (0)~ Complete: 273894 row(s)~ Complete: 729002 row(s)~ I need a file which should contain x.1,No (zero) rows read (0)~ y.2,No (zero) rows read (0)~ ... (2 Replies)
Discussion started by: Ravindra Swan
2 Replies

2. Shell Programming and Scripting

Help needed in Korn Shell scripting

#! /bin/ksh while read line do if ] ; then echo "no data" continue; fi echo "performing operation on $line" done < prg.txt (3 Replies)
Discussion started by: Juhi Kashyap
3 Replies

3. Shell Programming and Scripting

Help needed with Shell scripting

Hi All, I need to split a flatfile based on it's first character, I am using the following script awk '{print > "TEST_substr($0,1,1).txt"}' PROVIDER.txt It is returning files TEST_1 and TEST_2 But I am not getting the .txt file extension. I need the files like TEST_1.txt and ... (1 Reply)
Discussion started by: sam35
1 Replies

4. Shell Programming and Scripting

Help needed for shell scripting for oracle.

Hi, Please see contains both files created for automating the data from oracle through shell. 1)a_p.ksh #!/bin/ksh LOG=/home/A_P.log MESSAGE=/home/MESSAGE_A_P.txt mail_list=/home/AP_MAIL_LIST.txt data=/home/spooled_A_P.log echo "`date` Starting execution for A_P COUNT" > $LOG ... (2 Replies)
Discussion started by: fidelis
2 Replies

5. Shell Programming and Scripting

Help needed in shell scripting

" how to write a script to send an error in a log file to a particular E-Mail ID " Can anyone help me in this ? Regards (3 Replies)
Discussion started by: himvat
3 Replies

6. Shell Programming and Scripting

Help needed - ksh shell scripting

Hi all, i m new to Unix shell scripting(ksh) i have a requirement, can anyone help me out in this.. spec: i need to move all the files landing in "X" directory to "Y" directory automatically everyday at a particular time.. (5 Replies)
Discussion started by: subbu
5 Replies

7. Shell Programming and Scripting

SHell Scripting Help Needed

Dear All, I have an input file like this interface Serial10/0/7:11.1 point-to-point description CLIENT:SA_INSTITUTO ANGLO MEXICANO Sitio Metepec 104452:0,165 bandwidth 64 ip vrf forwarding INSTITUTO-ANGLO ip address 192.168.148.217 255.255.255.252 no ip directed-broadcast frame-relay... (2 Replies)
Discussion started by: cskumar
2 Replies

8. Shell Programming and Scripting

Help needed - shell scripting

I have a file that has text in the format below ABC / Some text / ABC / Some text / ABC / Some text / ABC / Some text / ABC / Some text / How can I seperate the text between a pair of 'ABC' into seperate files ??? any information would be of great help. Thanks (8 Replies)
Discussion started by: garric
8 Replies

9. Shell Programming and Scripting

help needed in shell scripting......urgent

Dear friends, please help me to solve following problem. I'm running a frontend application from which i'll be invoking the shell script with arguments as given below -driver -w -p "ABC XYZ" -S -ds con -dn "abc xyz" i am getting $1=-driver $2=-w $3=-p $4="ABC $5=XYZ" $6=-S $7=-ds... (3 Replies)
Discussion started by: swamymns
3 Replies
Login or Register to Ask a Question