Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
How to insert a character after the first byte in a line in Linux

I have a setting file states 1,2,3 meaning to place a , after the 1st, 2nd, and 3rd byte.

For example my input is

stackoverflow

then I want to make it

s,t,a,ckoverflow

How should I do it with Linux tools? I feel sed should be used, but I don't know how to do it.

Edit:

I have more than 10 commas to be placed. And the line also includes multi-byte characters.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

In sed:

echo stackoverflow | sed 's/./&,/1;s/./&,/3;s/./&,/5'

s/.../.../n replaces the nth match. After the first replacement, the second byte would now be the third match, and after that, the third byte would now be the fifth.

Or, with regex groups:

echo stackoverflow | sed 's/\(.\)\(.\)\(.\)/\1,\2,\3,/'

In Basic Regular Expressions (BRE), \(…\) is used to group expressions. You can refer to the text that matched the nth group using \n, to re-use matched text in the replacement, for example. So, in this case, I have three groups, each containing just ..

With GNU sed, you can avoid the \ by using Extended Regular Expressions (ERE):

echo stackoverflow | sed -r 's/(.)(.)(.)/\1,\2,\3,/'

With multibyte charsets, try with the C locale:

$ echo 'æ  ' | LC_ALL=C sed 's/\(.\)\(.\)\(.\)/\1,\2,\3,/'
�,�, , 
$ echo → | LC_ALL=C sed 's/\(.\)\(.\)\(.\)/\1,\2,\3,/'
�,�,�,
over 4 years ago · Santiago Trujillo Report

0

IFS=, read -ra arr <<< "$1"
rules=()
for i in "${arr[@]}"; do
    rules[$i]=1
done

s=stackoverflow
for (( i=0; i<${#s}; i++ )); do
    if (( ${rules[i+1]} )); then
        printf '%s,' "${s:i:1}"
    else
        printf '%s' "${s:i:1}"
    fi
done

Usage example:

$ ./sof 1,2,3
$ s,t,a,ckoverflow
$ ./sof 1,2,3,7
$ s,t,a,ckov,erflow
over 4 years ago · Santiago Trujillo Report

0

This might work for you (GNU sed):

sed 's/\B/\n/g;s/\n//4g;s/\n/,/g' file

or:

sed 's/\B/,/;s/\B/,/;s/\B/,/' file
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!