Identify the First Column Position in Second Column and add the position value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Identify the First Column Position in Second Column and add the position value
# 1  
Old 02-06-2015
Identify the First Column Position in Second Column and add the position value

Identify the First Column Position in Second Column and add the position value in 3rd column.

Code:
Sample data:
a|c
b|d
c|a
d|b
e|e
f|g
g|f
  |h
  |i
Expected Output:

a|c|1
b|d|2
c|a|3
d|b|4
e|e|5
f|g|6
g|f|7
  |h|
  |i|

# 2  
Old 02-06-2015
Something like this?
Code:
awk -F\| '{NF=3}$1 ~ /[a-zA-Z]/{$3=NR}1' OFS=\| file

# 3  
Old 02-06-2015
No. the current command giving sequence.. of each row I'm not looking like that. The output should be like this
Code:
a|c|3      explanation: means -- column-1 value "a" presents in column-2 at 3 position
b|d|4      explanation: means -- column-1 value "b" presents in column-2 at 4 position
c|a|1      explanation: means -- column-1 value "c" presents in column-2 at 1 position
d|b|2      explanation: means -- column-1 value "d" presents in column-2 at 2 position
e|e|5      explanation: means -- column-1 value "e" presents in column-2 at 5 position
f|g|7       explanation: means -- column-1 value "f" presents in column-2 at 7 position
g|f|6       explanation: means -- column-1 value "g" presents in column-2 at 6 position
  |h|        
  |i|

if the column-1 value doesn't exists in column - put # in it.

# 4  
Old 02-06-2015
Hello BrahmaNaiduA,

Could you please try following and let me know if this helps.
Code:
awk -F"|" '{if($1 !~ /^[[:space:]]+/){$(NF+1)=NR} else {$NF=$NF"|"}} 1' OFS="|" Input_file

Output will be as follows.
Code:
a|c|1
b|d|2
c|a|3
d|b|4
e|e|5
f|g|6
g|f|7
  |h|
  |i|

Thanks,
R. Singh
# 5  
Old 02-06-2015
the output is not in the expected format.
Code:
Expected Output:
a|c|3
b|d|4
c|a|1
d|b|2
e|e|5
f|g|7
g|f|6
k|h|9
 |i|
 |k| 

Column 3: explanation for your understanding:

column-1 value "a" presents in column-2 at 3 position
column-1 value "b" presents in column-2 at 4 position
column-1 value "c" presents in column-2 at 1 position
column-1 value "d" presents in column-2 at 2 position
column-1 value "e" presents in column-2 at 5 position
column-1 value "f" presents in column-2 at 7 position
column-1 value "g" presents in column-2 at 6 position
column-1 value "k" presents in column-2 at 9 position

Thanks,
Brahma
# 6  
Old 02-06-2015
Why, then, don't you specify your request correctly in the first place?

Anyhow, try
Code:
awk 'NR==FNR {T[$2]=NR;next} {print $0 FS (T[$1]?T[$1]:"#")}' FS="|" file file
a|c|3
b|d|4
c|a|1
d|b|2
e|e|5
f|g|7
g|f|6
  |h|#
  |i|#

# 7  
Old 02-06-2015
Hello,

Could you please try following, it may help you.
Code:
awk -F"|" '{split("abcdefghijklmnopqrstuvwxyz", A,"")} {for(i in A){if($2==A[i] && $1 !~ /^[[:space:]]/){$(NF+1)=i;};}if($1 ~ /^[[:space:]]+/){$2=$2"|#"}} 1' OFS="|" Input_file

Output is as follows.
Code:
a|c|3
b|d|4
c|a|1
d|b|2
e|e|5
f|g|7
g|f|6
  |h|#
  |i|#

EDIT: Please always be clear in your requirements, as per your first post I have posted my previous solution.


Thanks,
R. Singh

Last edited by RavinderSingh13; 02-06-2015 at 10:34 AM.. Reason: Added a comment about user's forst post and 4th post requirement of output
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Column to row and position data in a text file

Hi everyone.. I have a list of values in a file... a, b, c, 1, 2, 3, aaaa, bbbbb, I am interested in converting this column to a row.. "text",aaaa, bbbb a,1 (7 Replies)
Discussion started by: manihi
7 Replies

2. Linux

Removing a character at specific position in a column

Hi, I have a file like this (about 8 columns in total, this being the 2nd column) gi_49482297_ref_YP_039521.1_ gi_49482297_ref_YP_039521.1_ gi_49482315_ref_YP_039539.1_ gi_49482315_ref_YP_039539.1_I want to remove the _ at the end of the line. And at later stages I would want to replace the... (5 Replies)
Discussion started by: Syeda Sumayya
5 Replies

3. Shell Programming and Scripting

Need command or script to print all lines from 2nd position to last but one position

hi guys, i want command or script to display the content of file from 2nd position to last but one position of a file abcdefghdasdasdsd 123,345,678,345,323 434,656,656,656,656 678,878,878,989,545 4565656667,65656 i want to display the same above file without first and... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

4. Shell Programming and Scripting

Regex to identify word in second position on a line

I am interested in finding a regex to find a word in second position on a line. The word in question is या I tried the following PERL EXPRESSION but it did not work: ] या or ^\W या But both gave Null results I am giving below a Sample file: देना या सौंपना=delegate तह जमना या... (8 Replies)
Discussion started by: gimley
8 Replies

5. Shell Programming and Scripting

Trying to move a column into another position within a sed script

Currently the table looks like this student-id,last,first,hwk1,hwk2,hwk3,exam1,hwk4,hwk5,exam2 pts-avail,,,100,150,100,200,100,150,300 991-78-7872,Thompson,Ken,95,143,79,185,95,135,259 123-45-6789,Richie,Dennis,99,123,89,189,97,139,279 234-56-7891,Aho,Al,78,146,75,176,88,128,285... (3 Replies)
Discussion started by: ertang
3 Replies

6. Shell Programming and Scripting

Find the position of a field/column in a flat file

Hi, Let say I have a file which has around 400 fields. SampleFile ========= PATIENTID|FACILITY|................|TIME_LAST_VISITED_BY_MD|.....|STATUS| How is it possible to find out which field is TIME_LAST_VISITED_BY_MD?fro example by seeing the above structure we can saw FACILITY... (5 Replies)
Discussion started by: machomaddy
5 Replies

7. Shell Programming and Scripting

Identify the position of character

Hi, Can some one guide me to identify the position of a character using index in UNIX. I have a record like "17/11/2010 15:16:39;reject;10.44.48.65;daemon alert; src: 10.44.48.112; dst: 172.21.52.88" . I need to identify the value which comes after _src:_ (_ denotes space). I am able to... (15 Replies)
Discussion started by: suneel.mekala
15 Replies

8. Shell Programming and Scripting

row to column and position data in to fixed column width

Dear friends, Below is my program and current output. I wish to have 3 or 4 column output in order to accomodate in single page. i do have subsequent command to process after user enter the number. Program COUNT=1 for MYDIR in `ls /` do VOBS=${MYDIR} echo "${COUNT}. ${MYDIR}" ... (4 Replies)
Discussion started by: baluchen
4 Replies

9. Shell Programming and Scripting

How to add a column numbers at a particular position?

Problem discription: I have many files which contain the same lines. for instance, (15 lines) file1 ..last column add by hand arbitrarily. 1.78116800 0.68396600 0.00061900 0.47641600 -0.49794500 -0.00024000 -1.70662800 0.29577100 0.67863600 -1.70647600 0.29654600 ... (9 Replies)
Discussion started by: liuzhencc
9 Replies

10. Shell Programming and Scripting

File Manipulation (Move Column Position)

Hi All, I have a comma separated value (.CSV) file like the one below. The file contains about 20000 lines. FileName EmpNo,Name,Age,Sex,Band,Spouse,Children,Salary, 1000,Arnold,24,M,B,N.A.,No,10000, 1001,Jenny,27,F,C,John,2,20000, ................................... What i need is to... (1 Reply)
Discussion started by: ultimate
1 Replies
Login or Register to Ask a Question