Scripting question: Altering 2 field.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scripting question: Altering 2 field.
# 1  
Old 12-08-2009
Scripting question: Altering 2 field.

Hi Experts,

I want to alter two filed of my data file: The _new should come to 2nd column, and _new to be removed from 4rth column, please advise,

datafile.txt
Code:
aa  /dev/vgAA/lvol1       bb /dev/vgAA_new/lvol1
aa  /dev/vgAA1/lvol2      bb /dev/vgAA1_new/lvol2
aa  /dev/vgAC/lvol1       bb /dev/vgAC_new/lvol1
aa  /dev/vgAC12/lvol2    bb /dev/vgAC12_new/lvol2



The result should be:

Code:
aa  /dev/vgAA_new/lvol1       bb /dev/vgAA/lvol1
aa  /dev/vgAA1_new/lvol2      bb /dev/vgAA1/lvol2
aa  /dev/vgAC_new/lvol1       bb /dev/vgAC/lvol1
aa  /dev/vgAC12_new/lvol2    bb /dev/vgAC12/lvol2



Thanks ,
# 2  
Old 12-08-2009
Looks like you just want to swap columns 2 and 4:

Code:
awk '{print $1, $4, $3, $2}' datafile.txt

# 3  
Old 12-08-2009
add and remove or swap?

Code:
awk '{print $1,$4,$3,$2}' datafile.txt

# 4  
Old 12-08-2009
Thanks scottn & rdcwayx,
Well, thats not what I wanted , lets take a real example: I want to swap the _new :

Code:
ssh hpux202 "dd if=/dev/vgoraiD1B/rD1B_g13m2             ibs=1024k" |  dd  of=/dev/vgoraiD1B_new/rD1B_g13m2         obs=1024k
ssh hpux202 "dd if=/dev/vgoraiD1B/rD1B_g15m2             ibs=1024k" |  dd  of=/dev/vgoraiD1B_new/rD1B_g15m2         obs=1024k
ssh hpux202 "dd if=/dev/vgoraiD1B/rD1B_g17m2             ibs=1024k" |  dd  of=/dev/vgoraiD1B_new/rD1B_g17m2         obs=1024k
ssh hpux202 "dd if=/dev/vgoradD1B/rD1Bbidocd_1           ibs=1024k" |  dd  of=/dev/vgoradD1B_new/rD1Bbidocd_1       obs=1024k
ssh hpux202 "dd if=/dev/vgoraiD1B/rD1Bbidoci_1           ibs=1024k" |  dd  of=/dev/vgoraiD1B_new/rD1Bbidoci_1       obs=1024k
ssh hpux202 "dd if=/dev/vgoraiD1B/rD1Bbidoci_2           ibs=1024k" |  dd  of=/dev/vgoraiD1B_new/rD1Bbidoci_2       obs=1024k

to be change to below:




Code:
ssh hpux201 "dd if=/dev/vgoraiD1B_new/rD1B_g13m2             ibs=1024k" |  dd  of=/dev/vgoraiD1B/rD1B_g13m2         obs=1024k
ssh hpux201 "dd if=/dev/vgoraiD1B_new/rD1B_g15m2             ibs=1024k" |  dd  of=/dev/vgoraiD1B/rD1B_g15m2         obs=1024k
ssh hpux201 "dd if=/dev/vgoraiD1B_new/rD1B_g17m2             ibs=1024k" |  dd  of=/dev/vgoraiD1B/rD1B_g17m2         obs=1024k
ssh hpux201 "dd if=/dev/vgoradD1B_new/rD1Bbidocd_1           ibs=1024k" |  dd  of=/dev/vgoradD1B/rD1Bbidocd_1       obs=1024k
ssh hpux201 "dd if=/dev/vgoraiD1B_new/rD1Bbidoci_1           ibs=1024k" |  dd  of=/dev/vgoraiD1B/rD1Bbidoci_1       obs=1024k
ssh hpux201 "dd if=/dev/vgoraiD1B_new/rD1Bbidoci_2           ibs=1024k" |  dd  of=/dev/vgoraiD1B/rD1Bbidoci_2       obs=1024k

I have a formatiing , it displays nicely , and dont want to loose the evenly spaces between the command,

Thanks again.
# 5  
Old 12-08-2009
Code:
sed -e 's/\(if=\S*\)\(\/\S*\s\)/\1_new\2/' -e 's/\(of=\S*\)_new/\1/' urfile

# 6  
Old 12-08-2009
try below perl:

Code:
while(<DATA>){
	my @tmp = split;
	$tmp[1]=~s/(.*\/)([^\/]*)(\/[^\/]*)/$1.$2.'_new'.$3/e;
	$tmp[3]=~s/(.*\/)([^\/]*)_new(\/[^\/]*)/$1.$2.$3/e;
	print "@tmp\n";
}
__DATA__
aa  /dev/vgAA/lvol1       bb /dev/vgAA_new/lvol1
aa  /dev/vgAA1/lvol2      bb /dev/vgAA1_new/lvol2
aa  /dev/vgAC/lvol1       bb /dev/vgAC_new/lvol1
aa  /dev/vgAC12/lvol2    bb /dev/vgAC12_new/lvol2

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Efficiently altering and merging files in perl

I have two files fileA HEADER LINE A CommentLine A Content A .... .... .... TAILER AfileB HEADER LINE B CommentLine B Content B .... .... .... TAILER BI want to merge these two files as HEADER LINE A CommentLine A Content A (4 Replies)
Discussion started by: sam05121988
4 Replies

2. Programming

Altering a jar file

I have a script I am trying to test and run but it runs against a jar file. I wrote an external property file so it would redirect with my script, but it keeps going in search of the previous property file. Is there any way to externally over write the jar file and if not how do you go about... (7 Replies)
Discussion started by: risarose87
7 Replies

3. Shell Programming and Scripting

Altering a variable

Can I take an argument input, lets say it's, hg0000_xy1_v2, in the script it becomes f ... then hack off the end of the filename to change the variable to hg0000 only. I tried using sed but can't figure it out. f="$f" | sed 's/_fg_v//' I could change the variable label if necessary to... (4 Replies)
Discussion started by: scribling
4 Replies

4. Shell Programming and Scripting

simple awk question: split field with :

Hi, Probably a very weak question.. but I have tried all I know.. BPC0001:ANNUL_49542 0.0108 -0.0226 -0.0236 0.0042 0.0033 -0.0545 0.0376 0.0097 -0.0093 -0.032 Control BPC0002:ANNUL_49606 0.0190 -0.0142 -0.0060 -0.0217 -0.0027 ... (3 Replies)
Discussion started by: genehunter
3 Replies

5. SuSE

Tape Loader Device /dev/sgN is altering after each reboot

Dear all, we are running SLES 11 where an iscsi tape library is attached. To load the tape in a slot we are using for instance the command #mtx -f /dev/sg1 load 2 which is doing very well. But after a server's reboot the device name of the loader is now /dev/sg4 or /dev/sg5 I mean it is... (1 Reply)
Discussion started by: xunil321
1 Replies

6. Shell Programming and Scripting

altering numbers in files

I want to change a number in a file into number -1.. for instance file_input is fdisdlf_s35 fdjsk_s27 fsdf_s42 jkljllljkkl_s57 ... etc now i want the output to be fdisdlf_s34 fdjsk_s26 fdsf_s41 jkljllljkkl_s56 ... etc I was think of using "sed -e 's/2/1/g' -e 's/3/2/g' -e... (4 Replies)
Discussion started by: bigboizvince
4 Replies

7. Shell Programming and Scripting

yet another awk field syntax question

I am trying to print the remaing fields and field numbers beginning with a field 'xyz' #cat abc test1:test2:xyz:test3:test4:test5 #cat def test1:test2:test3:xyz:test4:test5 desired output is to be able to print NF and any trailing fields separated by':' test3 3 or test4 3 or test5... (4 Replies)
Discussion started by: prkfriryce
4 Replies

8. Shell Programming and Scripting

Question about sed. Inserting text in field?

Hi, I have tried to develop a sed script that inserts date and time in the third field in the first and second row below. The third row is an example and it shows where the date and time should be inserted. The script should check if the row already has date and time in the third field and if it... (2 Replies)
Discussion started by: pcrs
2 Replies

9. Shell Programming and Scripting

Issue altering end data

I have an inventory program that I would like to have the ability to go and change or alter the field data based on the item number as a key. I have the menu option set but at the end of the script process it just appends the changed data to the database rather than what I would like; which is to... (5 Replies)
Discussion started by: stlitguru
5 Replies
Login or Register to Ask a Question