Inserting a delimiter after certain charater position


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Inserting a delimiter after certain charater position
# 1  
Old 07-10-2007
Inserting a delimiter after certain charater position

Hi,
I have a string : -
ICFFHASMTAAMPFINCL22082006000002548789632
and i want to add delimiter after certain charater position through a script, eg. ICFFH,ASMTAAMPF,INCL,22082006,000002548789632.

I have tried and am able to achieve it through cut-paste. But i don't want to use cut paste as it is taking up a lot of space.

Can you please suggest any another way to do this?
# 2  
Old 07-11-2007
"taking up a lot of space"? I don't know what that is supposed to mean, but here is a very fast technique....
Code:
#! /usr/bin/ksh

string="ICFFHASMTAAMPFINCL22082006000002548789632"
echo "$string"

rest=${string#?????}
piece1=${string%$rest}
string=$rest

rest=${string#?????????}
piece2=${string%$rest}
string=$rest

rest=${string#????}
piece3=${string%$rest}
string=$rest

piece5=${string#????????}
piece4=${string%$piece5}

string2="${piece1},${piece2},${piece3},${piece4},${piece5}"

echo $string2
exit 0

# 3  
Old 07-11-2007
# 4  
Old 07-11-2007
Smilie Thanks a lot. It works!!!
# 5  
Old 07-11-2007
Hello:
so what are the hash and the question marks for in :

Code:
rest=${string#?????}
piece1=${string%$rest}

# 6  
Old 07-11-2007
Quote:
Originally Posted by aladdin
Hello:
so what are the hash and the question marks for in :

Code:
rest=${string#?????}
piece1=${string%$rest}

${string#substring} Strips shortest match of $substring from front of $string.
? Matches single character
rest=${string#?????} Removes the first five characters
# 7  
Old 07-11-2007
THX alot anbu23
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut command with dynamic passing of delimiter and position values

Hi All, We have a requirement of picking nth position value by using cut command. value would be delimited by any symbols. We have to pass delimited value and postition to get the value in a string. ex. echo "A,B,C,D,E" |cut -d "," -f3 echo "A|B|C|D|E"|cut -d "|" -f2 Kindly frame the... (5 Replies)
Discussion started by: KK230689
5 Replies

2. Shell Programming and Scripting

Inserting value at a particular position without changing the position of other characters

Hi All, I wanted a sed/awk command to add a value/character on a particular position without disturbing the position of other characters. I have file a.txt OL 10031 Day Black Midi Good Value P01 P07 OL 10031 Day Black Short Good Value P01 P07 I want to get the output as... (2 Replies)
Discussion started by: rahulsk
2 Replies

3. UNIX for Beginners Questions & Answers

Inserting Header at different position in a file

I would like to hear your directions on how to Insert theses tag </TITLE> and <TEXT> at a given position in 1000 of text files. My Files look like as samplefile1.txt <DOC> <DOCNO>3_September_2012</DOCNO> <TITLE> ... ... ... .... ... .. .. .. ... .. .. .... </TITLE> <TEXT> .... (1 Reply)
Discussion started by: imranrasheedamu
1 Replies

4. UNIX for Advanced & Expert Users

Inserting delimiter after a specific number of chars

Hello guys, I have a problem where I need to add a delimiter, that can be | for example, after each 28000 chars. The problem is that sometimes 1 row, which should contain 28000 chars is split in 2, so I want to put the delimiter after each 28000 so I will know the end of each row. Please... (2 Replies)
Discussion started by: Diogo R Jesus
2 Replies

5. Shell Programming and Scripting

Perl Code to change file delimiter (passed as argument) to bar delimiter

Hi, Extremely new to Perl scripting, but need a quick fix without using TEXT::CSV I need to read in a file, pass any delimiter as an argument, and convert it to bar delimited on the output. In addition, enclose fields within double quotes in case of any embedded delimiters. Any help would... (2 Replies)
Discussion started by: JPB1977
2 Replies

6. Shell Programming and Scripting

Shell script to put delimiter for a no delimiter variable length text file

Hi, I have a No Delimiter variable length text file with following schema - Column Name Data length Firstname 5 Lastname 5 age 3 phoneno1 10 phoneno2 10 phoneno3 10 sample data - ... (16 Replies)
Discussion started by: Gaurav Martha
16 Replies

7. Shell Programming and Scripting

Inserting double quotes after third delimiter

Hi, I'm trying to insert double quotes right after the third delimiter in a file. Delimiter is ^Z. For example: Input: Oct ^Z 1234 ^Z John ^Z Hello!" Desired Output: Oct ^Z 1234 ^Z John ^Z "Hello!" Any ideas? (1 Reply)
Discussion started by: kangaroo
1 Replies

8. Shell Programming and Scripting

Charater comparison

I have two files. Each file has one line with 2500 charaters in it and both lines should be the same, but thay are not. I need to compare the two lines and find where the differences are. So what I need to do is compare each character one at a time to find out whats different. (4 Replies)
Discussion started by: Tornado
4 Replies

9. Shell Programming and Scripting

running an Acii charater in a script.

Hi, I need to pass a space bar or an enter key in a script for it to return control to my script. How do I do this. For example the spacebar ascii value is 127, how do I tell the script to run this command. (4 Replies)
Discussion started by: quispiam
4 Replies

10. Shell Programming and Scripting

replacing charater

I need a help here. My administrator made some changes and we couldn't access some files any more. I am trying to replace a list of files from one format to another. Can anyone help me please? Here is an example of aaa_bbbb_cccccc.03172002_02:30:08 How can I replace the ':' with... (3 Replies)
Discussion started by: odogbolu98
3 Replies
Login or Register to Ask a Question