Insert Characters into a String in Bash

Insert characters Into a string in bash?

time=125959
echo ${time:0:2}:${time:2:2}:${time:4:2}

insert a character at x,y,z positions in a string

Using awk:

$ echo $mystring | awk 'BEGIN{FS=OFS=""}{$2=$2 "-";$5=$5 "-"}1'
22-333-4444

Explained:

awk 'BEGIN {
FS=OFS="" # field separators to null, ie. each char on an individual field
}{
$2=$2 "-" # set dashes to all the right places
$5=$5 "-"
}1' # output

Succesfully tested on GNU awk, mawk, Busybox awk and BWK awk version 20121220.

Update: what I want to do is to convert this string 135311046 to 13-531-1046 and then do grep on a file. All in one awk program:

$ awk '
BEGIN {
OFS=""
}
NR==FNR {
$2=$2 "-";$5=$5 "-"
a[$0]
next
}
$NF in a' FS="" input FS="," data

Output:

Line1.P2.ON28.C1.P1.FL1,12-522-2083
Line1.P2.ON26.C1.P1.FL1,12-531-2732

Succesfully tested on GNU awk, mawk and Busybox awk. Failed on BWK awk version 20121220.

Insert a character 4 chars before end of string in BASH

Using sed you can do:

s='0x2aab3f439000'
sed 's/.\{4\}$/:&/' <<< "$s"
0x2aab3f43:9000

SED - how to add ' ' and , to every character of a string

Corrected version: sed -r 's/(.{1})/'\''\1'\'',/g;s/,$//' - the backreference should be between the quotes. Not sure whether you need the space after comma or not, I deleted it to allow the second s command to match.

You can also use \x27 to represent single quote character:

$ echo 'STRING#!-@' | sed 's/./\x27&\x27,/g; s/,$//'
'S','T','R','I','N','G','#','!','-','@'

& will have entire text matched by the regexp, so no need to use capture groups for this case.

Other notes:

  • No need to use cat file | sed '..', you can use sed '..' file
  • -E is more portable these days compared to -r
  • .{1} is same as .

Insert a string at a specific position (fixed length file)

Solution as per OP's requirement for adding spaces as per length of 80:
In case you have control M characters in your Input_files and you want to remove them then use:

tr -d '\r' < Input_file > temp_file && mv temp_file Input_file

then run following code:

awk -v var="80" '{printf("%s%"var-length($0)+1"s%s\n",$0,OFS,"abcd")}' Input_file


2nd solution(more generic one): Could you please try following, this solution will look for maximum length of the line in whole Input_file and will add spaces according to it and newly entered last field will be in same alignment with all other lines.

awk '
FNR==NR{
len=length($0)>len?length($0):len
next
}
{
printf("%s%"len-length($0)+1"s%s\n",$0,OFS,"abcd")
}
' Input_file Input_file

insert character in particular position in shell

With sed with support for ERE ( -E or -r depending on your sed version):

sed -E 's/(([^|]*\|){24})/\1filename.txt|/'

How to auto insert a string in filename by bash?

If you're always inserting the string "x86" at character #18 in the string, you may use that command:

var="linux-202105200900-foo.direct.tar.gz"
var2=${var:0:18}"x86"${var:18}
echo $var2

The 2nd line means: "assign to variable var2 the first 18 characters of var, followed by x86 followed by the rest of the variable var"

If you want to insert "x86" just before the last hyphen in the string, you may write it like this:

var="linux-202105200900-foo.direct.tar.gz"
var2=${var%-*}"x86-"${var##*-}
echo $var2

The 2nd line means: "assign to variable var2:

  • the content of the variable var after removing the shortest matching pattern "-*" at the end
  • the string "x86-"
  • the content of the variable var after removing the longest matching pattern "*-" at the beginning


Related Topics



Leave a reply



Submit