LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-20-2010, 03:32 AM   #1
eliote
Member
 
Registered: Jun 2010
Distribution: Ubuntu
Posts: 39

Rep: Reputation: 0
inserting some text into a file using 'awk' command


hi all,

I'm gonna replace my machine's ip address and hostname using awk command. the pattern of the file is like the following...

ip address="192.168.1.100"

the script must ask the ip address from the user and replace it with the ip address in the quotation.
any suggestion.
 
Old 10-20-2010, 04:45 AM   #2
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Rep: Reputation: 31
why awk?

Code:
#!/bin/sh

read -p "New IP address" newip

{
grep -v "^ip address=" $your_file
echo "ip address=\"$newip\""
} >$new_file

exit 0
Is this what you need?
 
Old 10-20-2010, 05:08 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I agree that awk would probably not be my first choice here, but this would do what you want:
Code:
#!/usr/bin/awk -f

BEGIN{
	print "Please enter new ip address"
	getline newip < "-"
}

/ip/{

	sub(/"[^"]*/,"\""newip)
}	
1
The downside with awk is that you need to output this to a temp file and then delete the old file and rename temp.
 
Old 10-20-2010, 06:45 AM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

does it have to be awk? If not here is another alternative.
Code:
#!/bin/bash

read -p "New IP address" newip
sed -ri 's/^(ip address=).*/\1"'$newip'"/' file
 
Old 10-20-2010, 07:59 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I too like crts' solution and here is a slight variation to do away with excess quoting:
Code:
#!/bin/bash

read -p "New IP address" newip
sed -i "/ip/s/[0-9].*[0-9]/$newip/" file
 
1 members found this post helpful.
Old 10-20-2010, 08:12 AM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
@grail: nice!

Let's just anchor the 'ip'
Code:
#!/bin/bash

read -p "New IP address: " newip
sed -i "/^ip address=/s/[0-9].*[0-9]/$newip/" file
so that 'my 12th artificial hip' won't get changed
 
Old 10-20-2010, 08:31 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Yeah I wasn't sure where in the line it was
 
Old 10-20-2010, 11:27 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk 'BEGIN{
 print "Please enter new ip address"
 getline newip < "-"
 OFS=FS="="
}
/^ip address/{ $2="\042"newip"\042"}
{ a[++d]=$0 }
END{
  for(i=1;i<=d;i++){ print a[i] > "TEMP" }
  cmd="mv TEMP " FILENAME
  system(cmd)
}' file
 
Old 10-25-2010, 12:56 AM   #9
eliote
Member
 
Registered: Jun 2010
Distribution: Ubuntu
Posts: 39

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crts View Post
@grail: nice!

Let's just anchor the 'ip'
Code:
#!/bin/bash

read -p "New IP address: " newip
sed -i "/^ip address=/s/[0-9].*[0-9]/$newip/" file
so that 'my 12th artificial hip' won't get changed
thanks a lot crts.
but could you please describe what /s/[0-9].*[0-9]/ stands for!?
i don' understand what is that for.
 
Old 10-25-2010, 01:09 AM   #10
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by eliote View Post
thanks a lot crts.
but could you please describe what /s/[0-9].*[0-9]/ stands for!?
i don' understand what is that for.
Code:
s/[0-9].*[0-9]/$newip/
is sed's substitution command. It works in the manner
Code:
s/pattern/replacement/
[0-9] is called a character class. It stands for any digit from 0..9.
The '.' stand for any character and the asterisk is a quantifer to the previous expression. So '.*' stands for any amount of any characters. So in the processed line anything that is enclosed between two digits will be replaced with $newip.
 
Old 10-25-2010, 03:46 AM   #11
eliote
Member
 
Registered: Jun 2010
Distribution: Ubuntu
Posts: 39

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crts View Post
Code:
s/[0-9].*[0-9]/$newip/
is sed's substitution command. It works in the manner
Code:
s/pattern/replacement/
[0-9] is called a character class. It stands for any digit from 0..9.
The '.' stand for any character and the asterisk is a quantifer to the previous expression. So '.*' stands for any amount of any characters. So in the processed line anything that is enclosed between two digits will be replaced with $newip.
thank you so mucn crts, that worked
but what if i want to insert a range of IP address like this;

ip address="192.168.1.10/24"

I've tried to add '/24' at the end of the read parameter ($new ip), but that does'n work.
 
Old 10-25-2010, 04:58 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
I've tried to add '/24' at the end of the read parameter ($new ip), but that does'n work.
Please explain what you mean here?

I did the following which works fine:
Code:
read -p "Enter new ip: " newip

echo "$newip"
Running this:
Code:
$Enter new ip: 1.2.3.4/24
1.2.3.4/24
 
Old 10-25-2010, 05:51 AM   #13
eliote
Member
 
Registered: Jun 2010
Distribution: Ubuntu
Posts: 39

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Please explain what you mean here?

I did the following which works fine:
Code:
read -p "Enter new ip: " newip

echo "$newip"
Running this:
Code:
$Enter new ip: 1.2.3.4/24
1.2.3.4/24
want to add this replacement by script:
ip address="192.168.1.10/24"
----------------------------------------
and i used this command in my script
read -p "IP address:" newip
sed -ri 's/^(ip address=).*/\1"'newip/24'"/' filename

that didn't work for me grail, any idea?
 
Old 10-25-2010, 06:02 AM   #14
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by eliote View Post
want to add this replacement by script:
ip address="192.168.1.10/24"
----------------------------------------
and i used this command in my script
read -p "IP address:" newip
sed -ri 's/^(ip address=).*/\1"'newip/24'"/' filename

that didn't work for me grail, any idea?
change your sed statement to
Code:
sed -ri "/^ip address=/ s|[0-9].*[0-9]|$newip|" file
 
Old 10-26-2010, 12:58 AM   #15
eliote
Member
 
Registered: Jun 2010
Distribution: Ubuntu
Posts: 39

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crts View Post
change your sed statement to
Code:
sed -ri "/^ip address=/ s|[0-9].*[0-9]|$newip|" file
that didn't work crts!
maybe you could help me more interactively...
have you got yahoo ID?!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting blank lines in text file David the H. Linux - General 5 11-24-2007 03:34 PM
Inserting file contents into Sed Command ewingtux Programming 4 11-19-2007 06:59 PM
inserting the data thru php in a text file suchi_s Programming 5 02-02-2005 03:28 AM
inserting/deleting characters into a text file ananthbv Programming 7 07-13-2004 11:40 PM
inserting text into a file DavidPhillips Programming 5 08-15-2003 04:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration