name the column based on symbols in another column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting name the column based on symbols in another column
# 1  
Old 08-26-2009
name the column based on symbols in another column

Hi
if the input columns (both 2nd and 3rd) are either + or - give the name 1stname. if the 2nd one + and 3rd one - give the name 2ndname and if the 2nd one - and 3rd one + give the name 3rd one as showed in output

Thanx


input
Code:
x  +  + some values in other columns
t  -   -  .....
t  +   - .....
g  -  + .....

Code:
x  +  + ....   1stname
t  -   -  .....   1stname
t  +   - .....   2ndname
g  -  + .....   3rdname

# 2  
Old 08-26-2009
If you want real quick response, spend some time and give proper data that every one can understand.
Give the input and the expected output.
Is this what you are looking for:
Code:
 
echo "x  +  + some values in other columns" | sed "s/..*+  +..*/& 1stname/"

Output
Code:
x  +  + some values in other columns 1stname

# 3  
Old 08-26-2009
I think you misunderstood me. I was being simple and precise.


input
Code:
x  +  + 100  200
t  -   -  344  333
t  +   - 565  777
g  -  + 789   345

output

Code:
x  +  + 100  200  1stname
t  -   -  344  333   1stname
t  +   - 565  777   2ndname
g  -  + 789   345  3rdname

Is it possible to define the columns with specified symbols like
The script is just written roughly so that you guyz can understand
Code:
awk '{if ($2 == "+" && $3  == "+") print $0, "1stname"} 
elseif {($2 == "-" && $3  == "-" print $0, "1stname"} 
elseif {($2 == "+" && $3  == "-" print $0, "2ndname"} 
elseif {($2 == "-" && $3  == "+" print $0, "3rdname"}' inputfile


Last edited by repinementer; 08-26-2009 at 01:34 AM..
# 4  
Old 08-26-2009
a bit long..
Code:
awk '{if ($2$3 == "++" || $2$3 == "--") print $0,"1stname"; 
else if ($2$3 == "+-") print $0,"2ndname";else print $0,"3rdname"}' file

# 5  
Old 08-26-2009
Never mine I solved it myself
Code:
$ awk '{if ($2 == "+" && $3  == "+")  print $0, "1stname"}' input.txt && 
awk '{if ($2 == "-" && $3 == "-") print $0, "1stname"}' input.txt && 
awk '{if ($2 == "+" && $3 == "-") print $0, "2ndname"}' input.txt && 
awk '{if ($2 == "-" && $3 == "+") print $0, "3rdname"}'input.txt



---------- Post updated at 08:34 PM ---------- Previous update was at 08:31 PM ----------

Thanx

ryandegreat25
# 6  
Old 08-26-2009
Ok. Then that is exactly what the code does.
I have covered only one part of it, you figure out the rest.
instead of just "sed" use "sed -e " and add the rest.
# 7  
Old 08-27-2009
Shorter Smilie
Code:
awk '{$0=$0FS (($2==$3)?1:(($2=="+")?2:3)) "stname"}1' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed summation of one column based on some entry in first column

Hi All , I am having an input file as stated below Input file 6 ddk/djhdj/djhdj/Q 10 0.5 dhd/jdjd.djd.nd/QB 01 0.5 hdhd/jd/jd/jdj/Q 10 0.5 512 hd/hdh/gdh/Q 01 0.5 jdjd/jd/ud/j/QB 10 0.5 HD/jsj/djd/Q 01 0.5 71 hdh/jjd/dj/jd/Q 10 0.5 ... (5 Replies)
Discussion started by: kshitij
5 Replies

2. Shell Programming and Scripting

Sum of a column as new column based on header in a script

Hello, I am trying to store sum of a column as a new column inside a file but have to find the column names dynamically I/p c1,c2,c3,c4,c5 10,20,30,40,50 20,30,40,50,60 If i want to find sum only column c1, c3 and output it as c6,c7 O/p c1,c2,c3,c4,c5,c6,c7 10,20,30,40,50,30,70... (6 Replies)
Discussion started by: mkathi
6 Replies

3. Shell Programming and Scripting

Get maximum per column from CSV file, based on date column

Hello everyone, I am using ksh on Solaris 10 and I'm gathering data in a CSV file that looks like this: 20170628-23:25:01,1,0,0,1,1,1,1,55,55,1 20170628-23:30:01,1,0,0,1,1,1,1,56,56,1 20170628-23:35:00,1,0,0,1,1,2,1,57,57,2 20170628-23:40:00,1,0,0,1,1,1,1,58,58,2... (6 Replies)
Discussion started by: ejianu
6 Replies

4. Shell Programming and Scripting

Sum column values based in common identifier in 1st column.

Hi, I have a table to be imported for R as matrix or data.frame but I first need to edit it because I've got several lines with the same identifier (1st column), so I want to sum the each column (2nd -nth) of each identifier (1st column) The input is for example, after sorted: K00001 1 1 4 3... (8 Replies)
Discussion started by: sargotrons
8 Replies

5. Shell Programming and Scripting

awk to sum a column based on duplicate strings in another column and show split totals

Hi, I have a similar input format- A_1 2 B_0 4 A_1 1 B_2 5 A_4 1 and looking to print in this output format with headers. can you suggest in awk?awk because i am doing some pattern matching from parent file to print column 1 of my input using awk already.Thanks! letter number_of_letters... (5 Replies)
Discussion started by: prashob123
5 Replies

6. Shell Programming and Scripting

Pick the column value based on another column using awk or CUT

My scenario is that I need to pick value from third column based on fourth column value, if fourth column value is 1 then first value of third column.Third column (2|3|4|6|1) values are cancatenated. Please someone help me to resolve this issue. Source column1 column2 column3 column4... (2 Replies)
Discussion started by: Ganesh L
2 Replies

7. Shell Programming and Scripting

Filtering lines for column elements based on corresponding counts in another column

Hi, I have a file like this ACC 2 2 21 aaa AC 443 3 22 aaa GCT 76 1 33 xxx TCG 34 2 33 aaa ACGT 33 1 22 ggg TTC 99 3 44 wee CCA 33 2 33 ggg AAC 1 3 55 ddd TTG 10 1 22 ddd TTGC 98 3 22 ddd GCT 23 1 21 sds GTC 23 4 32 sds ACGT 32 2 33 vvv CGT 11 2 33 eee CCC 87 2 44... (1 Reply)
Discussion started by: polsum
1 Replies

8. Shell Programming and Scripting

to add special tag to a column based on column condition

Hi All, I have following html code <TR><TD>9</TD><TD>AR_TVR_TBS </TD><TD>85000</TD><TD>39938</TD><TD>54212</TD><TD>46</TD></TR> <TR><TD>10</TD><TD>ASCV_SMY_TBS </TD><TD>69880</TD><TD>33316</TD><TD>45698</TD><TD>47</TD></TR> <TR><TD>11</TD><TD>ARC_TBS ... (9 Replies)
Discussion started by: ckwan
9 Replies

9. Shell Programming and Scripting

need to remove duplicates based on key in first column and pattern in last column

Given a file such as this I need to remove the duplicates. 00060011 PAUL BOWSTEIN ad_waq3_921_20100826_010517.txt 00060011 PAUL BOWSTEIN ad_waq3_921_20100827_010528.txt 0624-01 RUT CORPORATION ad_sade3_10_20100827_010528.txt 0624-01 RUT CORPORATION ... (13 Replies)
Discussion started by: script_op2a
13 Replies

10. Shell Programming and Scripting

Change names in a column based on the symbols in another column

If the 4th column has - sign then the names in 3rd column has to change to some user defined names (as shown in output). Thanx input1 1 a aaaaa + 2 b bbbbb + 3 c ccccc + 4 d ddddd + 5 e eeeee + 6 f xxxxx + 8 h hhhhh +... (8 Replies)
Discussion started by: repinementer
8 Replies
Login or Register to Ask a Question