in the input fields are separated by pipe sign:

CCCC|Sess C1|s1 DA=yy07:@##;/u/t/we
DDDDD|Sess C2|s4 DB=yy8:@##;/u/ba

I want to get output where last field is changed (extracted only what is between first = and : in this field

expected output is:

CCCC|Sess C1|yy07
DDDDD|Sess C2|yy8
  • What do you mean by "I want to get output where last field is changed"? What exactly defines the expected output? Is it the part before the second |, plus the part between = and :? Please edit your question to add this information. – Sparhawk Sep 10 '18 at 12:23
  • output columns are separated also with | (pipe) - only in the last column I need to print only what is between first = and first : in original last column – Chris Sep 10 '18 at 12:28

standard awk is not very good at extracting data out of fields based on patterns. Some options include:

  • split() to split the text into an array based on specified delimiters.
  • match() which sets the RSTART and RLENGTH variables to indicate where the match occurred, and then use subtr() to extract the matched portion.

So here:

awk -F'|' -v OFS='|' '
  split($3, a, /[=:]/) >= 2 {print $1, $2, a[2]}' < file.txt

So returns the portion between the first and second occurrence of a = or : in $3.


awk -F'|' -v OFS='|' '
  match($3, /=[^:]*/) {
    print $1, $2, substr($3, RSTART+1, RLENGTH-1)
  }' < file.txt

GNU awk has a gensub() extension which brings the functionality of sed's s command into awk:

gawk -F'|' -v OFS='|' '
  $3 ~ /=/ {
    print $1, $2, gensub(/^[^=]*=([^:]*).*/, "\\1", 1, $3)
  }' < file.txt

Looks for = followed by any number of non-:s and extracts the part after =. The problem with gensub() is that you can't easily tell if the substitution was successful or not, hence the check that $3 contains = first.

With sed:

sed -n 's/^\([^|]*|[^|]*|\)[^=|]*=\([^:|]*\).*/\1\2/p' < file.txt

With perl:

perl -F'[|]' -lane 'print "$F[0]|$F[1]|$1" if $F[2] =~ /=([^:]*)/' < file.txt
  • Damn, you were faster. I tried with gawk: awk -F '|' -v OFS='|' '{print $1,$2,gensub(/^[^=]*=([^:]*).*$/, "\\1", "1", $3)}' < file.txt which is pretty much the same as your suggestion. – rexkogitans Sep 10 '18 at 14:04
  • @rexkogitans, thanks. made me realise that my using of $3 = gensub(... as the condition was wrong. – Stéphane Chazelas Sep 10 '18 at 14:28
  • The OP does not mention a condition for the 3rd column at all. I assume they are all formatted like this, so I suggest to drop the condition for the main block at all. – rexkogitans Sep 10 '18 at 15:06
  • @rexkogitans, I've all made them to print only those 3 fields if the 3rd field of input was in the expected format. I'll leave it as is unless the OP clarifies what to do when the input is not in the expected format. – Stéphane Chazelas Sep 10 '18 at 15:33

I would try

awk -F\|  'BEGIN {OFS="|";} 
    print ; }' se


  • -F\| tell awk to use | as input separator
  • equ=index($3,"="); get index of = in third field
  • $3=substr($3,equ+1,col-equ-1); do actual substitution

The first sub removes the first sixth characters in field 3 and second sub removes everything after colon including.

awk -F\| '{sub(/.{6}/,"",$3)sub(/:.*/,"")}1' OFS=\| file

Not the answer you're looking for? Browse other questions tagged or ask your own question.