Insert | in specific position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert | in specific position
# 1  
Old 01-29-2013
Insert | in specific position

Hi ,

I have a file which has line similar to below
Code:
13123324234234234234234234234234234
3454546456dfhgfhgh454645654asdasfsdsddfgdgdfg
345345345mnmnbmnb346mnb4565464564564645645


Not for each line for specific position I need to insert some '|'

Positions are fixed. Like 3,5,9,11
So the above file will be after the parsing
Code:
131|23|3242|34|234234234234234234234234
345|45|4645|6d|fhgfhgh454645654asdasfsdsddfgdgdfg
345|34|5345|mn|mnbmnb346mnb4565464564564645645

Please help on how I can do this.

Last edited by Franklin52; 01-30-2013 at 04:34 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 01-30-2013
use below code

Code:
sed "s/^\(...\)\(...\)\(...\)\(.*\)/\1|\2|\3|\4/g" $file

# 3  
Old 01-30-2013
Quote:
Originally Posted by srinivas matta
use below code

Code:
sed "s/^\(...\)\(...\)\(...\)\(.*\)/\1|\2|\3|\4/g" $file

This doesn't give the desired output. Some changes make it work:

Code:
# sed "s/^\(.\{3\}\)\(.\{2\}\)\(.\{4\}\)\(.\{2\}\)\(.*\)/\1|\2|\3|\4|\5/g" anup.txt 
131|23|3242|34|234234234234234234234234
345|45|4645|6d|fhgfhgh454645654asdasfsdsddfgdgdfg
345|34|5345|mn|mnbmnb346mnb4565464564564645645

This User Gave Thanks to zazzybob For This Post:
# 4  
Old 01-30-2013
Bob's code is correct. I didnt realize that there are 2 and 4 characters. I thought that all the 3 character separations.

Thanks Bob
# 5  
Old 01-30-2013
Code:
awk '{ print substr($0,1,3),substr($0,4,2),substr($0,6,4),substr($0,10,2),substr($0,12); }' OFS=\| file

This User Gave Thanks to Yoda For This Post:
# 6  
Old 01-30-2013
thank you all..

I already was trying and found my code is almost similar to what bipinajith posted.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert charactera in 1st position of specific lines using vi editor or sed command

Dear all, i am having text file like below surya rama ranga laxman rajesh reddy i want add string (OK) before a text from line 3 to 5 the result will be surya rama OK ranga OK laxman OK rajesh reddy (1 Reply)
Discussion started by: suryanarayana
1 Replies

2. Shell Programming and Scripting

How to insert a '#' in the first position of all the files?

Hi All, how to insert a '#' in the first position of all the files based on a certain condition. I tried this: cat /bin/user/input_file.txt | while read a do b=`sed 's/.*song=good.*/\#&/g' $a ` echo $b > /bin/user/new/output_file.txt done input_file.txt has list of names of 10... (5 Replies)
Discussion started by: anand787
5 Replies

3. Shell Programming and Scripting

substitute a string on a specific position for specific lines

I woud like to substitue a string on a specific position for specific lines I've got a file and I would like to change a specific string from "TOCHANGE" to "ABCABCAB" For every line (except 1,2, 3 and the last one) , I need to check between the 9th and the 16th digits. For the 3rd line, I... (7 Replies)
Discussion started by: BSF
7 Replies

4. Shell Programming and Scripting

insert character in particular position.

I want to insert space in 7th position of all the lines usign vi editor or sed command Input file 12345689010 abcdefghijk . . Output file 123456 89010 abcdef ghijk . . (7 Replies)
Discussion started by: Jairaj
7 Replies

5. Shell Programming and Scripting

Using sed to replace specific character and specific position

I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done? Example: File: A0199999123 A0199999124 A0199999125 Need to replace 99999 in positions 3-7 with 88888. Any help is appreciated. (5 Replies)
Discussion started by: programmer22
5 Replies

6. Shell Programming and Scripting

Insert character in a specific position of a file

Hi, I need to add Pipe (|) at 5th and 18th position of all records a file. How can I do this? I tried to add it at 5th position using the below code. It didnt work. Please help!!! awk '{substr($0,5,1) ~ /|/}{print}' $input_file > $temp_file (1 Reply)
Discussion started by: gpaulose
1 Replies

7. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies

8. Shell Programming and Scripting

search a line and insert string into specific at position

Hi, guys. I have one question: How can I search for a line with certain string in it and then insert a string into this line? For example: There is a file called shadow, the contents of it are below: ************************** ... yuanz:VIRADxMsadfDF/Q:0:0:50:7:::... (9 Replies)
Discussion started by: daikeyang
9 Replies

9. Shell Programming and Scripting

insert server name in position 1 on each line

Hello, It has been a long time since I have written unix code and I need to insert a variable into the first position of each line in a file. Below is an example of the script and the desired output file Here is my short script server="$(hostname)" df -kg | awk '{print $1, $2, $3, $4,... (3 Replies)
Discussion started by: rlc198842
3 Replies

10. Shell Programming and Scripting

How to insert strings at certain position

Hi, I need to insert strings "0000 00" at the each line within the file. The postion is 37 to 42. ex. name1 name2 0000 00 nam name 0000 00 The "0000 00" in two lines should be lined up. I don't know why it's not lined up when I posted it. Can anyone help? (14 Replies)
Discussion started by: whatisthis
14 Replies
Login or Register to Ask a Question