How to insert numbers to a in between statement

Hi Guys,

I want to create a shell script that will give me the output below. I want to insert the numbers from the input file to my url addresses below. And from the numbers below, I want to separate the last digit with a period (i.e. from 222222222222 to 22222222222.2). Appreciate any help.

Input file:

111111111111
222222222222
333333333333



Output file:

website 11111111111.1.numbers.com
website 22222222222.2.numbers.com
website 33333333333.2.numbers.com

Thanks in advance.

Br,
Pinpe

Hi

$ cat a
111111111111
222222222222
333333333333


$ sed 's/\(.*\)\(.\)/website \1\.\2\.numbers.com/' a
website 11111111111.1.numbers.com
website 22222222222.2.numbers.com
website 33333333333.3.numbers.com

Guru.

1 Like

One more method

awk '{_l=length;_1=substr($1,_l-2,1);_2=substr($1,0,_l-1);print"website "_2"."_1".numbers.com"}' infile

regards,
Ahamed

Here, an extra digit is getting added! I guess the user want the number to be intact and only that the last digit should have a period before it.

regards,
Ahamed

yes ahamad,

You are right. I am sorry.

This is PERFECT! Thanks dude! :b: :slight_smile:

Br,
Pinpe

---------- Post updated at 03:57 PM ---------- Previous update was at 03:10 PM ----------

Hi Guru,

Thank you very much for this elegant script. I just want to add more strings on the output file. I want to insert #!/bin/bash on top of the output file.

Desired Output File:

#!/bin/bash

website 11111111111.1.numbers.com
website 22222222222.2.numbers.com
website 33333333333.3.numbers.com

Br,
Pinpe

Hi
You have already posted the same question under a different thread. It is not right to do double posting. Anyways, here is the answer:

$ cat a
1111111111
2222222222
3333333333


$ sed -e 's/\(.*\)\(.\)/website \1\.\2\.numbers.com/' -e '1i #!/bin/bash \n' a
#!/bin/bash

website 111111111.1.numbers.com
website 222222222.2.numbers.com
website 333333333.3.numbers.com

Guru.

Hi Guru,

Yes. I should remove that other thread and maintain this one. Anyways, below is the response I'm getting...

$ sed -e 's/\(.*\)\(.\)/website \1\.\2\.numbers.com/' -e '1i #!/bin/bash \n' a
/bin/bash: Event not found.

Thanks again.

Br,
Pinpe

Hi
I think you are using csh or tcsh, and hence the exclamation(!) is creating this issue. Escape it with a backspace:

sed -e 's/\(.*\)\(.\)/website \1\.\2\.numbers.com/' -e '1i #\!/bin/bash \n' a

Guru.

Hi Guru,

This is the error now as shown below.

sed -e 's/\(.*\)\(.\)/website \1\.\2\.numbers.com/' -e '1i #\!/bin/bash \n' a
sed: command garbled: 1i #!/bin/bash \n

Br,
Pinpe

Hi

Not sure why you are getting the error. Not able to reproduce the error in which ever shell I tried. An alternate:

sed -e 's/\(.*\)\(.\)/website \1\.\2\.numbers.com/' a | awk 'BEGIN{print "#!/bin/bash\n"}1'

Guru.

1 Like

Hi Guru,

This works for me provided that I need to used nawk instead of awk and escape the (!) because I'm on tcsh. Thanks so much dude! :b: :slight_smile:

sed -e 's/\(.*\)\(.\)/website \1\.\2\.numbers.com/' a | nawk 'BEGIN{print "#\!/bin/bash\n"}1'

Br,
Pinpe

A few candidates to consider:

  1. The dot in replacement text is not special. You do not need to backslash-escape it in that context. Backslash-escaping non-special characters yields undefined sequences which can cause problems with some implementations (and opens the door to the remote possibility that a future extension to the standard will break current sed scripts).

  2. The insert (i) command should be followed by backslash and newline, before the text to insert. (GNU sed supports text immediately after the command name but this is not portable.)

  3. In a strictly compliant implementation, the \n escape sequence is only allowed in a regular expression evaluated against the pattern space. It is not allowed in replacement text nor in addresses. As with the \. sequence mentioned above, \n in in those contexts yields an undefined sequence (GNU sed will probably emit a newline). However, within the text argument to the insert (i) and append (a) commands, backslashes are stripped and the following character treated literally. \! becomes ! and \n becomes n (GNU sed instead chooses to emit a newline).

GNU sed developers may call these extensions, but they're more like incompatible deviations. Just a little one-liner and look at all the potential gotchas. What a headache!

Regards,
Alister

for solaris and csh

# sed -e 's/\(.*\)\(.\)/website \1\.\2\.numbers.com/' -e '1i\\
#\!/bin/bash\\
' file

regards
ygemici