Add a string in the specified position of the file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Add a string in the specified position of the file
# 1  
Old 11-11-2008
Add a string in the specified position of the file

I have a file and it contains some text and the length is 145. I need to add a string which is of length 8 at the 120th position. Is it possible to add???? Need help on this....Smilie
# 2  
Old 11-11-2008
Supposing you talk about 1 line with more than 120 characters... maybe this is the kind you looking for. Just have to adapt the number 5 to your 120 and "sometext" to your string you want to paste in there.
Code:
cat infile
1234567890
sed 's/\(^.\{5\}\)\(.*\)$/\1sometext\2/' infile
12345sometext67890

# 3  
Old 11-11-2008
thank u zaxxon!!!

cat infile
1234567890
sed 's/\(^.\{5\}\)\(.*\)$/\1sometext\2/' infile
12345sometext67890

In case if i want to add one more text in the same line means..i.e
12345sometext67thisistheexample890

how can i get this output.........
# 4  
Old 11-11-2008
Code:
sed 's/\(^.\{5\}\)\(.\{2\}\)\(.*\)$/\1sometext\2thisistheexample\3/' infile
12345sometext67thisistheexample890

# 5  
Old 11-11-2008
thank u very zaxxon !!!!!!!!!! It works fine
The message will be like this....there will two spaces between T and CF
This is the example message 4561232364511111PT CF

Now I want to insert a character M between T and C and also a string say sometext..so the output will be

This is the example message 456123sometext2364511111PTM CF

When I use the command which you mentioned previously...after inserting the character M I am getting two spaces before CF....I need only one space after inserting M....Pls help on this..........Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How do I replace a string in file that is in a certain position with spaces?

I am trying to replace the string in position 26 through 35 of the data file with 10 spaces and I want the remaining file to stay as is, the record length is over 900 characters? I am trying to use the AWK and substr but I am not getting it formatted correctly. Before... (6 Replies)
Discussion started by: fnwine1500
6 Replies

2. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

3. Shell Programming and Scripting

Search string at a particular position in a file

Hi, i have a text file as : abc 0 1 Pass hjk 1 1 Pass bhk 0 0 Fail jjh 8 2 Pass nkji 0 1 Pass Now I want to check that if 1st column is jjh , then , store the value of 3rd string of that line in a variable. Hence, 2... (8 Replies)
Discussion started by: Anamika08
8 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. UNIX for Dummies Questions & Answers

Search a string in the file and then replace another string after that position

Hi I am looking for a particular string in a file.If the string exists, then I want to replace another string with some other text.Once replaced, search for the same text after that character position in the file. :wall: E.g: Actual File content: Hello Name: Nitin Raj Welcome to Unix... (4 Replies)
Discussion started by: dashing201
4 Replies

7. Shell Programming and Scripting

Position of the string in a complex file

I had a similar problem few days back and got this fixed with the below command when I have a file with this format GS*12345***** ST*1******** A* B* E* RMR*123455(This is the unique number to locate this row) F* SE*1*** GE*12345* GS*878787***** ST*2 H* J* RMR*567889(This is the... (9 Replies)
Discussion started by: Muthuraj K
9 Replies

8. Shell Programming and Scripting

AWK or SED to add string at specific position

Greetings. I don't have experience programing scripts. I need to insert a string in a specific position of another string on another file (last.cfg), for example: File last.cfg before using script: login_interval=1800 lcs.machinename=client04 File last.cfg after using script:... (4 Replies)
Discussion started by: vanesuke
4 Replies

9. Shell Programming and Scripting

how to find a position and print some string in the next and same position

I need a script for... how to find a position of column data and print some string in the next line and same position position should find based on *HEADER8* in text for ex: ord123 abs 123 987HEADER89 test234 ord124 abc 124 987HEADER88 test235 ... (1 Reply)
Discussion started by: naveenkcl
1 Replies

10. Shell Programming and Scripting

How to add character in specific position of a string?

Hi All, I would like to use sed to add "-" between the following string: Value: 20060830 Result: 2006-08-30 Pls advice. Thx a lot Victor (5 Replies)
Discussion started by: victorlung
5 Replies
Login or Register to Ask a Question