Operating System - HP-UX
1832667 Members
3137 Online
110043 Solutions
New Discussion

how to insert character/digit into txt file through sed/awk scripting

 
MANISH PATEL_2
Frequent Advisor

how to insert character/digit into txt file through sed/awk scripting

Hi Guys,

I need a help that how can i insert character/digit in line by line.

I want to add 3 numeric before 34th character, 3 character after 52, 1 character after 59th and 1 character after 75th.

How can i write a script by using awk/sed.

Thanks
Manish
10 REPLIES 10
A. Clay Stephenson
Acclaimed Contributor

Re: how to insert character/digit into txt file through sed/awk scripting

In awk, one approach would be to split the line into 4 separate chunks using the substr function and then splicing these pieces back together along with your insertions. Notice how I intentionally not giving you explicit instructions. If you are interested in learning then the hint that you have received is more than sufficient. Hint 2: It's a little easier to get your awk just right if you will build an awk file rather than trying to do it all directly from the shell. So create a file, e.g. "my.awk" and then
awk -f my.awk infile > outfile
If it ain't broke, I can fix that.
Hein van den Heuvel
Honored Contributor

Re: how to insert character/digit into txt file through sed/awk scripting

I would recommend to also check out perl.
You can parse the input
- with substr as with awk as suggested earlier
- with a regular expression like:
/(.{33})(.{18}) ... /
- my favourite for this task: pack/unpack
($first,$second,...) = unpack "a33a18...

Good luck!
Hein van den Heuvel
James R. Ferguson
Acclaimed Contributor

Re: how to insert character/digit into txt file through sed/awk scripting

Hi:

...and in addition Clay and Hein's suggestions to use 'substr' with either 'awk' or Perl or Perl's 'unpack()', you can write ugly 'sed' regular expression substitions like:

# echo "aaaaaa"|sed -ne 's/\(a\{3\}\)\(a\{3\}\)/X\1Y\2/p'

...which would produce:

XaaaYaaa

from: aaaaaa

Regards!

...JRF...
Sandman!
Honored Contributor

Re: how to insert character/digit into txt file through sed/awk scripting

imho try the sed construct below:

# sed 's/^\(.\{33\}\)\(.\{19\}\)\(.\{7\}\)\(.\{16\}\)/\1987\2str\3c\4c/g' file

where 987 is an example of a 3-char length numeric to be added before the 34th character...so on and so forth.
MANISH PATEL_2
Frequent Advisor

Re: how to insert character/digit into txt file through sed/awk scripting


Thanks for the prompt reply...

But i am not familier with awk/sed scripting, if you can provide me a script or sentence i would really appreciate.

Thanks
Manish
Hein van den Heuvel
Honored Contributor

Re: how to insert character/digit into txt file through sed/awk scripting

>> I want to add 3 numeric

Any numeric? Random pick?
Same numeric on all lines?
What would the value depend on?

For further help you may want to show us how far you got trying to solve this, and append a .txt file with a few (5?) sample lines which clearly show the data before and after the edit.

Regards,
Hein.
James R. Ferguson
Acclaimed Contributor

Re: how to insert character/digit into txt file through sed/awk scripting

Hi (again) Manish:

> But i am not familier with awk/sed scripting, if you can provide me a script or sentence i would really appreciate.

Well, the example both Sandman and I provided show one way to perform what you seek. You will need to modify the example to fit your character offsets and that which you want to inject.

Regular expressions syntax denotes any character with a dot ("."). The notation of:

.{33}

or escaped for 'sed':

.\{33\}

specifies any-character repeated 33-times.

The encapsulation of that into parenthesis:

\(.\{33\}\)

allows us to back-refrenece this as \1 . Each successive group of parenthesized elements is referenced as \2, \3, etc.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: how to insert character/digit into txt file through sed/awk scripting

I don't normally do this but when someone asks for baby food rather than learning how to make baby food, I looked at your profile. 0 of 17 points assignments indicates that you are not only unwilling to learn but ungrateful as well.

I will give you one final hint:
{
s1=substr($0,1,33)
print s1,"999"
}

run that file through awk and see if a couple of ideas don't collide inside your head.
If it ain't broke, I can fix that.
MANISH PATEL_2
Frequent Advisor

Re: how to insert character/digit into txt file through sed/awk scripting

Hi Guys,

As per guide by Stephenson, I am able to insert character/digit in file by awk command with substr function.

Thanks once again.


Manish
MANISH PATEL_2
Frequent Advisor

Re: how to insert character/digit into txt file through sed/awk scripting

.