Alternate for sed -i in AIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Alternate for sed -i in AIX
# 1  
Old 01-07-2013
Alternate for sed -i in AIX

Experts,

At the moment I am working in AIX box where sed -i is not available.
My requirement is as below

Two files file1 and file2. file1 contains the IP address, its count. file2 contains the Hostname and its corresponding IP address. I would like get the IP address replaced with the apt Hostname in file1.

Code:
# cat file1
192.168.20.8    => 1
192.168.20.24   => 2
192.168.20.6    => 2
192.168.20.7    => 3
192.168.20.9    => 3
192.168.20.25   => 4
192.168.20.26   => 4
192.168.20.23   => 6

Code:
# cat file2
192.168.20.6    ser70
192.168.20.7    ser71
192.168.20.8    ser72
192.168.20.9    ser73
192.168.20.23   ser80
192.168.20.24   ser81
192.168.20.25   ser82
192.168.20.26   ser83

My code is of:
Code:
for i in `awk '{print $1}' file1`
	do
	svr=$(grep $i file2 | awk '{print $2}')
	sed -i "s/$i/$svr/" file1
done

Expected Output:
Code:
# cat file1
ser72    => 1
ser81   => 2
ser70    => 2
ser71    => 3
ser73    => 3
ser82   => 4
ser83   => 4
ser80   => 6

But unfortunately sed -i is not present in my AIX box, please shoot me an alternative. Smilie
Also if the IP address is not found in file2, that row should be left untouched in file1.
# 2  
Old 01-07-2013
Doubtless you can simplify the code further, i.e. by using awk to read both files and substitute the correct values. You would then write this to a temporary file and copy that over the original.

Otherwise, using ed is an easy way to begin with:

Code:
ed file1 << !
, s/$i/$svr/
w
q
!

This User Gave Thanks to Scott For This Post:
# 3  
Old 01-07-2013
You could use use awk:

Code:
awk 'NR == FNR {
  iton[$1] = $2
  next
  }
($1 in iton && $1 = iton[$1]) || 1
  ' file2 file1 > _tmp_file_ &&
      mv -- _tmp_file_ file1

If the original order is not important and your shell supports process substitution (zsh, ksh93 and bash):

Code:
join <(sort file2) <(sort file1) -o1.2,2.2,2.3


Last edited by radoulov; 01-07-2013 at 10:07 AM..
This User Gave Thanks to radoulov For This Post:
# 4  
Old 01-07-2013
Thanks for the prompt response Scott
Is the below right ?

Code:
for i in `awk '{print $1}' file1`
	do
	svr=$(grep $i file2 | awk '{print $2}')
	ed file1 << EOD
		, s/$i/$svr/
		w
		q
	EOD
done

# 5  
Old 01-07-2013
Only if the closing EOF is indented with a TAB (or nothing at all - it won't work if those are spaces before it).

A note to radoulov's solution (which is nice Smilie). Moving a temporary file over an existing file can be dangerous.

I did that once, and it caused no end of grief!

Code:
# ls -l rc.tcpip
-rwxrwxr-- 1 root system 6409 Aug 21 2009 rc.tcpip
 
# sed "s/change_this/to_this/" rc.tcpip > _tmp_rc_tcpip
# mv _tmp_rc_tcpip rc.tcpip
# ls -l rc.tcpip
-rw-r--r-- 1 root system 6406 Jan 7 2013 rc.tcpip

rc.tcpip won't get executed after a reboot. You can guess what happened next Smilie
These 2 Users Gave Thanks to Scott For This Post:
# 6  
Old 01-07-2013
WOW!!

Quote:
Originally Posted by radoulov
You could use use awk:

Code:
awk 'NR == FNR {
  iton[$1] = $2
  next
  }
($1 in iton && $1 = iton[$1]) || 1
  ' file2 file1 > _tmp_file_ &&
      mv -- _tmp_file_ file1

This works out, can you please explain the code.
# 7  
Old 01-07-2013
Quote:
Originally Posted by Scott
Only if the closing EOF is indented with a TAB (or nothing at all - it won't work if those are spaces before it).

A note to radoulov's solution (which is nice Smilie). Moving a temporary file over an existing file can be dangerous.
Great point Scott!
sed's -i is far more sophisticated (if I recall correctly, it handles even extended acls).

To the OP: with this manual method you'll need to handle the file permissions/inode changes manually ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Alternate to copy + remove

is there any single command for cat file1 > file2 cp /dev/null file1 The original file must retain intact but it should get empty. Actually, the contents needs to be processed by another process and for new entries, the file must remain there with zero records. (4 Replies)
Discussion started by: busyboy
4 Replies

2. Shell Programming and Scripting

Process alternate lines in awk/sed/perl

hi.. i have a fasta file with the following format >sequence1 CCGGTTTTCGATTTGGTTTGACT >sequence2 AAAGTGCCGCCAGGTTTTGAGTGT >sequence3 AGTGCCGCAGAGTTTGTAGTGT Now, i want to read alternate line and add "GGGGGGGGGGG" to end of every sequence Desired output: >sequence1... (4 Replies)
Discussion started by: empyrean
4 Replies

3. Shell Programming and Scripting

AIX sed/awk

I'm trying to change the pam.conf file like below. ssh auth required /usr/lib/security/pam_prohibit ssh account required /usr/lib/security/pam_prohibit emagent auth required /usr/lib/security/pam_prohibit emagent account required ... (4 Replies)
Discussion started by: pjeedu2247
4 Replies

4. HP-UX

Alternate for wget

Hi, Whats the alternate for wget in HP-UX ? (4 Replies)
Discussion started by: mohtashims
4 Replies

5. Shell Programming and Scripting

SED on AIX Limitation

Hello, I have a problem running a script created in ksh for Linux (Tested on Debian 5.0, Ubuntu Server 10.04 and RHEL 5.1), it works properly. :b: I trying to pass it to a AIX 5.3. :wall: The problem is the character limit of 256 on a command system and SED. I need to cut the contents of... (8 Replies)
Discussion started by: nemesis.spa
8 Replies

6. Shell Programming and Scripting

Alternate work out for sed command

Iam trying to insert a line after #Cluster in the property file shown below #Node=Nodehostname:NodeProfilename Node=testNode:test_profile #Cluster=Cluster_Name:nodeName@ClusterMem1,nodeName@ClusterMem2,.... #DC=DCname:DCnodegrp:DCtemp DC=test_DC:test_NG:test_template i was using sed command... (12 Replies)
Discussion started by: SSSB
12 Replies

7. Shell Programming and Scripting

sed: appending alternate line after previous line

Hi all, I have to append every alternate line after its previous line. For example if my file has following contents line 1: unix is an OS line 2: it is open source line 3: it supports shell programming line 4: we can write shell scripts Required output should be line1: unix is an OS it is... (4 Replies)
Discussion started by: rish_max
4 Replies

8. Shell Programming and Scripting

alternate lines

Hi, I'm new to Unix. I want to read the all the lines from a text file and write the alternate lines into another file. Please give me a shell script solution. file1 ----- one two three four five six seven newfile(it should contain the alternate lines from the file1) ------- one... (6 Replies)
Discussion started by: pstanand
6 Replies

9. Shell Programming and Scripting

Alternate way for echo.

Hi, Is there any other command echo does. if I am doing this operation for each line in my file. So its taking very long time to process more than 1000 records. Is there any alternative way to write the above if statement (5 Replies)
Discussion started by: senthil_is
5 Replies

10. Shell Programming and Scripting

sed -r problem in AIX

Hi All, I have one doubt in the AIX. I executed the below command in the LINUX but i could not execute same thing in the AIX. Please see the details of my issue in below. # cat test line 1 (one) line 2 (two) build=test line 3 (three) # sed -r 's/build=\w+/build=new_build/' test line 1... (7 Replies)
Discussion started by: sridhusha
7 Replies
Login or Register to Ask a Question