To transpose and replace the values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To transpose and replace the values
# 1  
Old 01-30-2017
To transpose and replace the values

Hi Guys,

I am having a file like below
Code:
AZ_H,NZ_K,IN_F,AZ1_A


output required
AZ_H string,
NZ_K int,
IN_F int,
AZ1_A double

the values will starting like below
_A - double
_F - int
_H - string
_K - int

Code:
code tried
tr -s ' '  '\n'<file.txt > file.tmp
sed -i -- 's/_A/double/g' * <file.tmp

# 2  
Old 01-30-2017
Hello rohit_shinez,

Could you please try following and let me know if this helps.
1st code:
Code:
awk -F, '{for(i=1;i<=NF;i++){sub(/_A$/,"& double",$i);sub(/_H$/,"& String",$i);sub(/_K$/,"& int",$i);sub(/_F$/,"& int",$i);print $i}}'  Input_file
OR
awk -F, '{for(i=1;i<=NF;i++){sub(/_A$/,"& double",$i);sub(/_H$/,"& String",$i);gsub(/_K$|_F$/,"& int",$i);print $i}}'  Input_file

Output will be as follows.
Code:
AZ_H String
NZ_K int
IN_F int
AZ1_A double

2nd code:
Code:
awk '{sub(/_A$\n$/,"_A double",$0);sub(/_H$/,"& String",$0);sub(/_K$/,"& int",$0);sub(/_F$/,"& int",$0);} 1' RS=,   Input_file
OR
awk '{sub(/_A$\n$/,"_A double",$0);sub(/_H$/,"& String",$0);gsub(/_K$|_F$/,"& int",$0);} 1' RS=,   Input_file
OR even more smaller
awk '{sub(/_A$\n$/,"_A double");sub(/_H$/,"& String");gsub(/_K$|_F$/,"& int");} 1' RS=,  Input_file

Output will be as follows.
Code:
AZ_H String
NZ_K int
IN_F int
AZ1_A double

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-30-2017 at 11:15 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-30-2017
Hi, maybe with gnu sed (no tested):
Code:
sed -e 's/_A/& double/g' -e 's/_F/& int/g' -e 's/_H/& string/g' -e 's/_K/& int/g' -e 's/,/&\n/g'

Regards.
# 4  
Old 01-30-2017
Quote:
Originally Posted by RavinderSingh13
Hello rohit_shinez,

Could you please try following and let me know if this helps.
1st code:
Code:
awk -F, '{for(i=1;i<=NF;i++){sub(/_A$/,"& double",$i);sub(/_H$/,"& String",$i);sub(/_K$/,"& int",$i);sub(/_F$/,"& int",$i);print $i}}'  Input_file
OR
awk -F, '{for(i=1;i<=NF;i++){sub(/_A$/,"& double",$i);sub(/_H$/,"& String",$i);gsub(/_K$|_F$/,"& int",$i);print $i}}'  Input_file

Output will be as follows.
Code:
AZ_H String
NZ_K int
IN_F int
AZ1_A double

2nd code:
Code:
awk '{sub(/_A$\n$/,"_A double",$0);sub(/_H$/,"& String",$0);sub(/_K$/,"& int",$0);sub(/_F$/,"& int",$0);} 1' RS=,   Input_file
OR
awk '{sub(/_A$\n$/,"_A double",$0);sub(/_H$/,"& String",$0);gsub(/_K$|_F$/,"& int",$0);} 1' RS=,   Input_file
OR even more smaller
awk '{sub(/_A$\n$/,"_A double");sub(/_H$/,"& String");gsub(/_K$|_F$/,"& int");} 1' RS=,  Input_file

Output will be as follows.
Code:
AZ_H String
NZ_K int
IN_F int
AZ1_A double

Thanks,
R. Singh
Thanks but sorry to mention that i will be having data like below
Code:
AZ_H_AZ1
NZ_K_NZ111213_11

I mean i need to consider _A_,_K_

Hi,

Basically what i require is for eg

AZ_A_AZ1,NZ_F_NZ123,.. ..

Output required

Code:
AZ_A_AZ1 double,
NZ_F_NZ123 int


Last edited by rohit_shinez; 01-30-2017 at 12:01 PM.. Reason: Formatting not done
# 5  
Old 01-30-2017
Hello rohit_shinez,

Could you please try following and let me know if this helps you.
Let's say we have following Input_file(as an example):
Code:
cat  Input_file
AZ_A_AZ1,NZ_F_NZ123,AZ_H_ABD1,NZ_K_WQ1231

1st code:
Code:
awk '{sub(/$\n$/,"$");gsub(/_K_.*|_F_.*/,$0" int");sub(/_H_.*/,$0" string");sub(/_A_.*/,$0" double")} 1' RS=,  Input_file

Output will be as follows.
Code:
AZAZ_A_AZ1 double
NZNZ_F_NZ123 int
AZAZ_H_ABD1 string
NZNZ_K_WQ1231$ int

2nd code:
Code:
awk -F, '{for(i=1;i<=NF;i++){gsub(/_K_.*|_F_.*/,$i" int",$i);sub(/_H_.*/,$i" string",$i);sub(/_A_.*/,$i" double",$i);print $i}}'  Input_file

Output will be as follows.
Code:
AZAZ_A_AZ1 double
NZNZ_F_NZ123 int
AZAZ_H_ABD1 string
NZNZ_K_WQ1231 int

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-31-2017 at 02:02 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 01-30-2017
Code:
awk '
BEGIN {
   ar["A"]="double";
   ar["F"]="int";
   ar["H"]="string";
   ar["K"]="int";
}
{
 for (i=1; i<=NF; i++) {
   split($i, av, "_");
   print $i, ar[av[2]] ((i==NF) ? "" : ",")
 }
}
' FS="," infile

This User Gave Thanks to rdrtx1 For This Post:
# 7  
Old 01-31-2017
Thanks guys it worked perfectly
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace values on file

Gents, Please i need your help. Using the file2.txt i will like to replace values in file3.txt. Example in file 2 column 1 is the value to find in file3.txt and replace with value in colunm2 (file2.txt). Example file2.txt 21 1209 22 1210file3.txt SCI TB Timestamp Local : 8/30/17... (2 Replies)
Discussion started by: jiam912
2 Replies

2. Shell Programming and Scripting

Replace values

Gents, Can you please help with this. Input file 49955 2009 2 49957 2010 2 49959 2010 2 49961 2010 2 49963 2010 2 49789 2011 -174 49791 2011 2 49793 2011 2 49795 2011 2 49965 2011 170 49967 2011 2 49969 2011 2 49971 2011 2 49797 2012 -174 49799 2012 2 (8 Replies)
Discussion started by: jiam912
8 Replies

3. Shell Programming and Scripting

Replace values

Gents, Please can you help me with this. When column 49 == 2 Need to do the following changes; Change previous row field (substr$0,45,4)-1 Change previous row field (substr$0,72,5)+2 Change actual row field (substr$0,40,4)+1 Change actual row field (substr$0,49,1)-1 Change actual... (6 Replies)
Discussion started by: jiam912
6 Replies

4. Shell Programming and Scripting

Replace values in columns

I have the following input format: AA00000712000 -0.17 0.90 -1.04 -0.37 -1.45 -1.13 -0.22 -0.34 -0.55 2.37 0.67 -0.48 -0.48 AA00000712001 0.15 0.03 0.47 0.62 2.04 1.14 0.29 -0.81 0.85 0.53 1.00 -0.10 -0.48 BB00000712000 1.32 -0.47 0.44 0.00 0.98 ... (4 Replies)
Discussion started by: ncwxpanther
4 Replies

5. Shell Programming and Scripting

Find and replace many values

Dear Friends, I did the same question before in other thread, but I want to explain a little better my request. I am looking for a way how to find and replace a values in two files using a reference a file where are the key to replace. Basically, I want to keep a copy of the original file... (1 Reply)
Discussion started by: jiam912
1 Replies

6. Shell Programming and Scripting

Transpose timestamp based on column values and calculate time difference

Hello Expert, I need to transpose Date-Timestamp based on same column values and calculate time difference. The input file would be as below and required output is mentioned in the bottom INPUT File ======== 08/23/2012 12:36:09 JOB_5340 08/23/2012 12:36:14 JOB_5340 08/23/2012... (2 Replies)
Discussion started by: asnandhakumar
2 Replies

7. Shell Programming and Scripting

Transpose field names from column headers to values in one column

Hi All, I'm looking for a script which can transpose field names from column headers to values in one column. for example, the input is: IDa;IDb;IDc;PARAM1;PARAM2;PARAM3; a;b;c;p1val;p2val;p3val; d;e;f;p4val;p5val;p6val; g;h;i;p7val;p8val;p9val; into the output like this: ... (6 Replies)
Discussion started by: popesk
6 Replies

8. UNIX for Dummies Questions & Answers

using sed to replace values...

I have a file: $somevar=somevalue $anothervar= $someothervar=45 I'd like to be able to replace the values in the file. I just don't know how exactly to use sed. I was thinking along the lines of: cat file | sed "s/$somevar=/anotherval/g" I was hoping this would make the... (2 Replies)
Discussion started by: mrwatkin
2 Replies

9. Web Development

Replace xml values

Hallo all, I try to create a bash script but till now without any positiv results. The script should replace different variables in a text file with the right xml values Look at the following xml file: file.xml =================================== <?xml version="1.0"... (1 Reply)
Discussion started by: research3
1 Replies

10. Shell Programming and Scripting

replace the column values.

I have the below file ...where some of the column values should replaced with desired values ....below file u can find that 3 column where ever 'AAA' comes should replaced with ' CC ' NOTE : we have to pass the column number ,AAA,CC (modified value) as the parameters to the code. ... (6 Replies)
Discussion started by: charandevu
6 Replies
Login or Register to Ask a Question