LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-23-2010, 11:50 AM   #1
zwierzq
LQ Newbie
 
Registered: Jan 2010
Posts: 2

Rep: Reputation: 0
SED replace specyific line with variable


Hi!

I have a little problem. i have file like this..

1 (130.216.1.125)
2 (210.7.32.2)
3 (210.7.36.67)
4 (210.7.47.22)
5 (207.231.240.8)
6 (64.57.28.27)
7 (64.57.28.25)
8 (64.57.28.37)
9 (64.57.28.13)
10 *
11 (62.40.112.37)
12 (62.40.112.5)
13 (62.40.124.2)
14 (193.171.15.38)
15 (193.171.15.41)
16 *
17 (143.205.170.1)
18 (143.205.172.11)
1 (130.216.1.125)
2 *
3 (210.7.36.67)
4 (210.7.47.22)
5 (207.231.240.8)
6 (64.57.28.27)

and i need to replace * to this this format (0.0.0.0) where last zero is a variable
output file mus be like that

1 (130.216.1.125)
2 (210.7.32.2)
3 (210.7.36.67)
4 (210.7.47.22)
5 (207.231.240.8)
6 (64.57.28.27)
7 (64.57.28.25)
8 (64.57.28.37)
9 (64.57.28.13)
10 (0.0.0.1)
11 (62.40.112.37)
12 (62.40.112.5)
13 (62.40.124.2)
14 (193.171.15.38)
15 (193.171.15.41)
16 (0.0.0.2)
17 (143.205.170.1)
18 (143.205.172.11)
1 (130.216.1.125)
2 (0.0.0.3)
3 (210.7.36.67)
4 (210.7.47.22)
5 (207.231.240.8)
6 (64.57.28.27)

i have created bash scrip like this:

amount_of_lines=`wc -l < zlit`

for((x=1;x<$amount_of_lines+1;x++))
do
sed -e "$x s/*/(0.0.0.$x)/g" zlit
done

but it does not work i think that he problem is in $x but i can't find the way to mask $x
 
Old 01-23-2010, 12:27 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
One problem is that you need to escape the asterisk character. The other is that sed will read each line and all of the asterisks will be replaced the first time through the loop.

It would be better to use awk for this problem. You can initialize a variable in the BEGIN block and increment it if you have a match.
If you want the 4th octet to equal the line number in the zlit file, you can use the NR awk variable which tracks the record number.

awk '/\*/{ printf "%d (0.0.0.%s)\n",$1,NR };!/\*/{print}' zlt
 
1 members found this post helpful.
Old 01-23-2010, 12:46 PM   #3
zwierzq
LQ Newbie
 
Registered: Jan 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jschiwal View Post
One problem is that you need to escape the asterisk character. The other is that sed will read each line and all of the asterisks will be replaced the first time through the loop.
Yeach i know so i want to insert a variable
if i do like this: sed -e "1s/*/(0.0.0.$x)/g" zlit
it changes none because first line of file is address
if i do like this sed -e "10s/*/(0.0.0.$x)/g" zlit
it chnges * to (0.0.0.10) in 10th line.
So i want to insert at 1 and 10 position a vriable from loop.


I am a beginer so i do not know AWK well. It is too complicated to me at this level
 
Old 01-23-2010, 01:08 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The problem is what jschiwal already pointed out: sed will parse the entire file at every call and if you put the sed command inside a loop, you will get the entire file duplicated multiple times (with some substitution made at some point). The problem is how to increment a numeric variable in sed and use it as the replacement string in the substitution. Since there is not an easy way to do that in sed, the suggested awk code is more handy.

Anyway, to stick with the shell, you can do something like this:
Code:
#!/bin/bash
while read line
do
  if [[ $line =~ '*' ]]
  then
    ((count++))
    echo ${line/\*/(0.0.0.$count)}
  else
    echo $line
  fi
done < zlit
The if statement check if the line matches a literal asterisk and use substring replacement accordingly. Note that the =~ operator is available from bash version 3 and you may have compatibility issues if you use the code with an older version.

Last edited by colucix; 01-23-2010 at 05:39 PM. Reason: Corrected a typo
 
1 members found this post helpful.
Old 01-23-2010, 09:23 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
To the general question about inserting variables, you could do something like:
sed '...'"$variable"'...' filename

The variable is evaluated by the bash shell before the sed command is executed.

license=GPL
set sed -n '/'$license'/p' README.txt
> echo $*
sed -n /GPL/p README.txt

Sed is usually used by putting it's command in single quotes. There are fewer bash meta-characters you would need to escape.

You would have to escape the \$ in regular expressions otherwise. It precedes a bash variable. In a regular expression it is a meta-character representing the end of a line.

Last edited by jschiwal; 01-23-2010 at 09:46 PM. Reason: fix up grammer a bit
 
  


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
sed - How do you replace end of line with a space pppaaarrrkkk Programming 7 02-07-2011 11:27 AM
SED - replace line after substing rany Linux - Newbie 2 07-02-2009 01:13 AM
replace a pattern with a line using sed/awk lokeshn05 Linux - Newbie 3 05-06-2009 03:01 PM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM
how to replace with variable using sed? babag Programming 4 09-17-2008 03:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:47 PM.

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