switch values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting switch values
# 8  
Old 05-27-2011
Quote:
Originally Posted by hernand
Hi,

In reality my file has thousand of rows and columns.
It would be nice to set explicitly which 2 columns to swap. They can appeare same values in different places, so I should be able to swap values just only on the column positions.
For example in this case:
Code:
x  20   x  y 20 x
x  45   x  y  45  x
x  100  x  y 100  x
x  50   x  y  50  x
x  70  x y 70 x
x  80  x y 80 x

I want to swap values on column 2 and column5 so I will receive:

Code:
x  20   x  y 45  x
x  45   x  y  20  x
x  100  x  y 50  x
x  50   x  y  100  x
x  70  x y 80 x
x  80  x y 70 x

Thanks
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!
I don't follow your sample.
Given your latest sample and
Code:
nawk -v c=5 'BEGIN{ARGV[ARGC++]=ARGV[1]} FNR==NR{a[FNR]=$c;next}{$c=(FNR%2)?a[FNR+1]:a[FNR-1]}1' yourLatestSample

produces:
Code:
x 20 x y 45 x
x 45 x y 20 x
x 100 x y 50 x
x 50 x y 100 x
x 70 x y 80 x
x 80 x y 70 x

anything wrong with that?
This User Gave Thanks to vgersh99 For This Post:
# 9  
Old 05-27-2011
Hi Vgersh99,

Sorry but your code seems to be working just for my sample file, here is the original file.
I need the spaces between columns, they are important for further parsing.
Thanks

INPUT:
Code:
TT0006033250                         20110524-07:01:46  GNAGL22            TT   GNAGL22            
TT0006033250                         20110524-07:01:46  BRTPAS44            TT   BRTPAS44            
XX0000120172                        20110524-07:02:08  SORTH21           GG  SORTH21            
XX0000120172                        20110524-07:02:08  TQKUT99          GG  TQKUT99

OUTPUT:
Code:
TT0006033250                         20110524-07:01:46  GNAGL22            TT   BRTPAS44            
TT0006033250                         20110524-07:01:46  BRTPAS44            TT   GNAGL22            
XX0000120172                        20110524-07:02:08  SORTH21           GG  TQKUT99            
XX0000120172                        20110524-07:02:08  TQKUT99          GG  SORTH21

---------- Post updated at 07:24 AM ---------- Previous update was at 06:27 AM ----------

Please check, thanks

Last edited by hernand; 05-27-2011 at 09:09 AM.. Reason: mistake
# 10  
Old 05-27-2011
assuming all the fields are of the constant width....
Code:
nawk -v c=5 '
BEGIN {
   ARGV[ARGC++]=ARGV[1]
}
FNR==NR && NR==1 {
  copy0=$0
  for(i=1;i<=NF;i++){
    match(copy0, FS FS "*")
    fw[i]=RSTART+RLENGTH
    copy0=substr(copy0,RSTART+RLENGTH)
  }
}
FNR==NR{
    a[FNR]=$c
    next
}
{
$c=(FNR%2)?a[FNR+1]:a[FNR-1]
for(i=1;i<=NF;i++)
  printf("%-*s%c", fw[i], $i,(i==NF)?ORS:"")
}' yourLatestSample

This User Gave Thanks to vgersh99 For This Post:
# 11  
Old 05-27-2011
You can also try this:
Code:
perl -ane 'if ($.%2){$x=$_;$y=$F[2]};if ($.%2==0) {$x=~s/(([^\s]+\s+){4})\w+/\1$F[4]/;print $x;s/(([^\s]+\s+){4})\w+/\1$y/;print}' mydata.txt

This User Gave Thanks to bartus11 For This Post:
# 12  
Old 05-27-2011
Hi Bartus ,
Thanks your code seems to be working.
Just one question where can I set columns , which should swap, because
those could change in different files.
# 13  
Old 05-27-2011
Code:
perl -anse 'BEGIN{$c--};if ($.%2){$x=$_;$y=$F[$c]};if ($.%2==0) {$x=~s/(([^\s]+\s+){$c})\w+/\1$F[$c]/;print $x;s/(([^\s]+\s+){$c})\w+/\1$y/;print}' -- -c=5 mydata.txt

c=5 means 5th column.
This User Gave Thanks to bartus11 For This Post:
# 14  
Old 05-27-2011
Thanks Bartus
Works perfectly
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

2. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

3. Shell Programming and Scripting

Query the table and return values to shell script and search result values from another files.

Hi, I need a shell script, which would search the result values from another files. 1)execute " select column1 from table_name" query on the table. 2)Based on the result, need to be grep from .wft files. could please explain about this.Below is the way i am using. #!/bin/sh... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

4. Shell Programming and Scripting

Converting odd values to even values(or vice-versa) located in a column

Hello All, I have a below data in a .csv file where all rows where col1 is A, col2 is odd numbers, similarly even numbers for all rows where col1 is B. Note that my data has some other columns(not shown here) too (around 100) after col2. Tool,Data A,1 A,3 A,5 .... so on B,2 B,4 .... ... (4 Replies)
Discussion started by: ks_reddy
4 Replies

5. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

6. Shell Programming and Scripting

Cat Values from Several files if it meets criteria for column values

I have results from some statistical analyses. The format of the results are as given below: I want to select lines that have a p-value (last column) less than 0.05 from all the results files (*.results) and cat to a new results file. It would be very nice if a new column is added that tells... (2 Replies)
Discussion started by: genehunter
2 Replies

7. Shell Programming and Scripting

how to access console of a switch having rj45 on switch side to db 9 female on pc side console cable

hi, how to access console of a switch having rj45 on switch side to db 9 female on pc side console cable which needs to be connected to one console server having rj11 on its side and db 9 female on other end.i.e. on switch side,console cable has rj45 and db 9 pin female connector on other side of... (1 Reply)
Discussion started by: pankajd
1 Replies

8. Shell Programming and Scripting

AWK: read values from file1; search for values in file2

I have read another post about this issue and am wondering how to adapt it to my own, much simpler, issue. I have a file of user IDs like so: 333333 321321 546465 ...etc I need to take each number and use it to print records wherein the 5th field matches the user ID pulled from the... (2 Replies)
Discussion started by: Bubnoff
2 Replies

9. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies

10. UNIX for Advanced & Expert Users

Converting Binary decimal coded values to Ascii Values

Hi All, Is there any command which can convert binary decimal coded values to ascii values... i have bcd values like below оооооооооооо0о-- -v - Pls suggest a way to convert this. Thanks, Deepti.Gaur (3 Replies)
Discussion started by: gaur.deepti
3 Replies
Login or Register to Ask a Question