Visit Our UNIX and Linux User Community


changing line on a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting changing line on a file
# 1  
Old 10-19-2009
changing line on a file

Hi, I want to change on a file this line:

vif = [ 'ip=167.112.194.243,mac=00:16:3E:CE:23:14' ]

into:

vif = [ 'ip=167.112.194.243,mac=00:16:3E:CE:23:14,vifname=veth216','ip=10.1.1.1,mac=00:16:3E:FD:58:BB,vifnam e=veth216a']

But there are some conditionals:

1. last part '] of the original line after 00:16:3E:CE:23:14' ] may vary to 00:16:3E:CE:23:14' ] or 00:16:3E:CE:23:14 ' ]
2. The second mac adrress of the line I need, 00:16:3E:FD:58:BB, must be different every time I run sed. Maybe just changing the last two characters BB to other two characters.
3. The [ ' and ' ] of both lines could be separated by some espaces, example [ ' or [' or ' ] or '] so you should not take this characters as a reference.
4. The Ip 167.xx.xx.xx and first mac address 00:16:3E:CE:23:14 are the same in two line. The second ip always is 10.1.1.1, and the second mac address must change every time I run sed command. Smilie

thanks tons
regards,
Israel.
# 2  
Old 10-20-2009
what have you tried till now??
# 3  
Old 10-21-2009
Sorry, but I'm still with the problem... As the source file change, I mean the position of ' ] or [ ', so I'm not able to change to the output I want. I've tried a couple of sed commands.

regards,
Israel.
# 4  
Old 10-21-2009
Hi Israel, would this work for you?
(ksh/bash)
Code:
rndhex() {
  echo "ibase=10;obase=16; $((RANDOM%256))"|bc
}

while read line; do
  case $line in
    vif*)   mac=${line##*mac=}
            mac="${mac%\'*]}"
            randomhex=CC
            newmac=${mac%:*:*:*}:$(rndhex):$(rndhex):$(rndhex)
            echo "${line%\' ]},vifname=veth216','ip=10.1.1.1,mac=$newmac,vifname=veth216a' ]" ;;
    *)      echo $line ;;
  esac
done<infile

Code:
$> cat infile
vif = [ 'ip=167.112.194.243,mac=00:16:3E:CE:23:14' ]

Code:
$> ./test
vif = [ 'ip=167.112.194.243,mac=00:16:3E:CE:23:14,vifname=veth216','ip=10.1.1.1,mac=00:16:3E:E8:3B:8E,vifname=veth216a' ]
$> ./test
vif = [ 'ip=167.112.194.243,mac=00:16:3E:CE:23:14,vifname=veth216','ip=10.1.1.1,mac=00:16:3E:F3:46:99,vifname=veth216a' ]
$> ./test
vif = [ 'ip=167.112.194.243,mac=00:16:3E:CE:23:14,vifname=veth216','ip=10.1.1.1,mac=00:16:3E:FE:51:A3,vifname=veth216a' ]


Last edited by Scrutinizer; 10-21-2009 at 03:28 AM..
# 5  
Old 10-22-2009
Hi Scrutinizer,

The script works perfectly :-)
Thanks tons

regards,
Israel

Previous Thread | Next Thread
Test Your Knowledge in Computers #666
Difficulty: Medium
IEEE 802.3 is a working group and a collection of Institute of Electrical and Electronics Engineers (IEEE) standards produced by the working group defining the physical layer and data link layer's media access control (MAC) of wireless Ethernet.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

2. UNIX for Dummies Questions & Answers

Changing a special line and Byte in a random file

Hello I created 3 files by: dd if=/dev/urandom bs=1024 count=1000000 of=./testfile1 dd if=/dev/urandom bs=1024 count=5000000 of=./testfile2 dd if=/dev/urandom bs=1024 count=10000000 of=./testfile3 Now I want to know how to make a change in a specific byte and/or line of theses files? (2 Replies)
Discussion started by: frhling
2 Replies

3. Shell Programming and Scripting

Changing Line in Txt File

So I have a python program that I run, which runs accordingly to options I have listed in a text file (ie user_prefs). Now there are many options listed in this user_prefs.txt, but the one of most interest to me is that of the file path of the time series. I have over a hundred of these time... (8 Replies)
Discussion started by: Jimmyd24
8 Replies

4. Shell Programming and Scripting

Need Help!!! For changing the content of the line

I need to change the content of the line after I grep it. The line is : Date SNWD.I-1 (in) WTEQ.I-1 (in) PREC.I-1 (in) TOBS.I-1 (degC) TMAX.D-1 (degC) TMIN.D-1 (degC) TAVG.D-1 (degC) I want to change the second column (SNWD.I-1(in)) to SNWD.I-1(m), how could I do that? I use csh... (2 Replies)
Discussion started by: handsonzhao
2 Replies

5. Shell Programming and Scripting

Read file line by line and process the line to generate another file

Hi, i have file which contains data as below(Only sample shown, it may contain more data similar to the one shown here) i need to read this file line by line and generate an output file like the one below i.e based on N value the number of MSISDNs will vary, if N=1 then the following... (14 Replies)
Discussion started by: aemunathan
14 Replies

6. Shell Programming and Scripting

Need help in changing vertical lines to horizontal line in a file

Hi, I have a file like below robert PREF: 3 AVAIL: henry PREF: 234 AVAIL: john PREF: 145,178 AVAIL: 123 matt PREF: 564,932 AVAIL: ten PREF: 389 AVAIL: kill (2 Replies)
Discussion started by: rocky1954
2 Replies

7. Shell Programming and Scripting

changing from command line to perl script

I had posted previously about this problem I had. I have multiple text files with hundreds of lines of the following type: 2000001 34 54 234 2000001 32 545 2000001 -2000001 77 2000001 44 2000001 998 2000001 77 32 2000001 45 23 111 89 98 75 23 34 999 . . . etc... What I wanted was... (2 Replies)
Discussion started by: xchen89x
2 Replies

8. AIX

Yesterday's date without changing anything and in one cmd line ?

I have seen references in the forum about getting yesterday's date but it is either by changing something in the system (date, time zone, ...) or with more then one line of script cmds. How can I get yesterday's date without changing anything in the system and in one single command line ? (4 Replies)
Discussion started by: Browser_ice
4 Replies

9. Shell Programming and Scripting

changing colomn to the line

I have a file which has only one colomn of numbers,ex: 122 173 292 400 979 2152 2339 2376 2387 2446 2450 What ksh / unix command should I use to create a file in which those numbers will be in one line,like this 122 173 292 400 979 .... etc Thanks a lot for help (9 Replies)
Discussion started by: aoussenko
9 Replies

10. Shell Programming and Scripting

Changing Line Number of a File

Example: O o x What I would like to do is to rename the first column of the above file without affecting the format. The output should look like the following: Output: O o x #! /bin/ksh cd $HOME/lib/.Lee #nl = no. of lines. nl=`grep 'X' ex | wc -l` #ln = line no. ln=1 (17 Replies)
Discussion started by: ilak1008
17 Replies

Featured Tech Videos