0

How can determine the position of “&” in the column number 5 to substring the value 2 or substring value 3 by awk functions

column1|column2|column3|column4|variable1:value1 & variable2:value2 & variable3:value3 & variable4:value4|column6|column7| 
2
  • You can use cut -d'|' -f5 to get the column value and pipe it to another cut Commented Oct 13, 2015 at 7:51
  • Can you please give some more sample input and the desired output. For example: Can column5 have an undefined number of variables and values or is it always the same? Commented Oct 13, 2015 at 8:38

2 Answers 2

1

try u.awk as

 BEGIN { FS="|" ; } { n=split($5,A,"&") ; for ( i=1; i <= n ; i++ ) { printf "%d-th & at position %d\n",i,length(A[i])+i ; split(A[i],B,":") i; printf "\t[%s] : [%s]\n",B[1],B[2] ; } } 

given input as

column1|column2|column3|column4|variable1:value1 & variable2:value2 & variable3:value3 & variable4:value4|column6|column7| COLn1|COLn2|COLn3|COLn4||COLn6|COLn7| Tue Oct 13 10:56:50 CEST 2015 

called with awk -f u.awk u

1-th & at position 18 [variable1] : [value1 ] 2-th & at position 20 [ variable2] : [value2 ] 3-th & at position 21 [ variable3] : [value3 ] 4-th & at position 21 [ variable4] : [value4] 
  • note that empty 5th colum is treated in for( .. ) (as n is 0)
  • note also that white space are not treated.
  • Updated to give &'s position
0

Using AWK:

awk -F['&''|'] '{ split($6,a,":");print a[2] }' input_file

1
  • thank, but some time column5 come with null ||so I nead use substr function depend of position of & Commented Oct 13, 2015 at 8:35

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.