Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting text at a certain location

Status
Not open for further replies.

lbzh

Programmer
Aug 7, 2004
25
US
I have a Unix scripting question,I have a file such as below:
1234ABCDE KLDS
2334KLVDE ABdd
9832BDKLE TMDA


I would like to insert the string "HERE" in position 5 in every line in the file and the rest of the characters in each line move over
resulting in the following:
1234HEREABCDE KLDS
2334HEREKLVDE ABdd
9832HEREBDKLE TMDA
......
Note: Each line could move 2 or more successive blanks somewhere on the line which I would like to maintain.
any ideas on what command to use?

 
Try this:
Code:
awk '{print substr($0,1,4) "HERE" substr($0,5);}' File1.txt >NewFile1.txt

Don't know what you mean by:
Note: Each line could move 2 or more successive blanks somewhere on the line which I would like to maintain.

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
This works correctly, thanks a lot.
One more question though:

Instead of inserting the text "here", I would like to insert a variable which holds the value of a 4 character string.

what is the syntax for doing that.

I tried with a variable called here as below:

awk '{print substr($0,1,4) "$here" substr($0,5);}' old_file >NewFile1.txt

and

awk '{print substr($0,1,4) $here substr($0,5);}' old_file >NewFile1.txt

but neither worked.

Thanks

 
Code:
awk -v var="${myShellVariable} '{print substr($0,1,4) var substr($0,5);}' File1.txt >NewFile1.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Something like this ?
sed "s!^\(....\)!\1$here!" old_file >NewFile1.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top