I'm trying to add a zero at the end of the data that start with 62018 (it will always start with 62018)

UNB+UNOA:1+5030917029608:14+5000119000006:14+070509:0850+620180001000200++INVOIC

any help would be appreciated.

Thank you
JG

Dani AI

Generated

The sample line needs a trailing "0" added to the plus-separated field that begins with 62018. supplied the example, diagnosed a regex issue in the sed attempt, and offered a field-based awk fix that targeted field 6. If the 62018 token can appear in different positions or more than once per line, scanning all +-separated fields is safer than editing a single numbered field.

awk -F'+' '{
  OFS="+"
  for (i=1; i<=NF; i++)
    if ($i ~ /^62018/ && $i !~ /0$/) $i = $i "0"
  print $0
}' infile > outfile

This variant preserves + separators, handles multiple matches per line, and avoids adding a second zero if the field already ends with 0. It is portable awk and does not rely on nonstandard in-place flags; writing to a new file and replacing it is recommended for safety. For users preferring sed: the original problem was sed's basic-RE behavior (bare + is literal unless escaped or using extended regex with -E/-r), and macOS vs GNU sed differ in options and -i semantics, so testing on a copy is advised.

Recommended Answers

All 3 Replies

sed -e 's/\+62018\([0-9]+\)\+\+/+62018\10++/' infile > outfile

sed -e 's/\+62018\([0-9]+\)\+\+/+62018\10++/' infile > outfile

it doesn't work at my side:

# sed -e 's/\+62018\([0-9]+\)\+\+/+62018\10++/' file
UNB+UNOA:1+5030917029608:14+5000119000006:14+070509:0850+62018000100020++INVOIC

@OP:try this

awk 'BEGIN{FS="+";OFS="+"}$6~/^62018/{ $6=$6"0"}{ print $0 }' file

it doesn't work at my side:

# sed -e 's/\+62018\([0-9]+\)\+\+/+62018\10++/' file
UNB+UNOA:1+5030917029608:14+5000119000006:14+070509:0850+62018000100020++INVOIC

@OP:try this

awk 'BEGIN{FS="+";OFS="+"}$6~/^62018/{ $6=$6"0"}{ print $0 }' file

Of course. "+" is only a valid regex character in perl. Sed doesn't recognize it as the "1 or more" function. So do like this instead:

# sed -e 's/+62018\([0-9]*\)++/+62018\10++/' infile > outfile
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.