How to put delimiters in text files after fix characters?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to put delimiters in text files after fix characters?
# 1  
Old 03-20-2013
[Solved] How to put delimiters in text files after fix characters?

Hi ,

i have a text file in which i want to put delimiters after certain characters ( fix),.
like put a delimiter (any like ,) after 1-3 character than 4 than 5 than 6-17 .....

files looks like this (original)
Code:
22503003673916972040504700538884720121008175657358045042677480191000000919046068826

and i want file like this
Code:
225,0,3,003673916972,0405047005388847,20121008175657,3580450426774801,91,000000919046068826,

please suggest any command or script that will do this work?

Thanks in advance

Last edited by Franklin52; 03-21-2013 at 03:58 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 03-20-2013
I assume you have a list of delimiter spacings

you can read a string or file one character at a time:

Code:
i=0
spacing="3 4 5 17"
while IFS= read -r -n1 char
do
    i=i+1
    echo  -n "$char"
    if [[ "$spacing" =~ "$i" ]]; then 
        echo -n ","
    fi
done < "$file"
echo

Mike

Last edited by Michael Stora; 03-20-2013 at 04:38 PM..
# 3  
Old 03-20-2013
Would something like this work?
Code:
$ cat temp.sh
string=2250300367391697204050470053888472012100817
for len in 17 5 4 3; do
  string=`echo $string | sed "s/^\(.\{$len\}\)\(.*\)/\1,\2/"`
done
echo $string

$ ./temp.sh
225,0,3,003673916972,04050470053888472012100817

# 4  
Old 03-20-2013
You can also do this with awk. Note that clist must be initialized to a list of the character positions after which a comma is added (in increasing numeric order):
Code:
awk -v clist='3 4 5 17' '
BEGIN { n = split(clist, cl, / /)
}
{       for(i = n; i; i--)
                $0 = substr($0, 1, cl[i]) "," substr($0, cl[i] + 1)
        print
}' input

With the sample input provided, it produces:
Code:
225,0,3,003673916972,040504700538884720121008175657358045042677480191000000919046068826

# 5  
Old 03-21-2013
Hi Cragun ,

your code works fine for me..Thanks :-)....( others codes are also working )
I have one more prob...actually this string was from a file , which contains n number of string (around 10K string )
this code works fine for 1 string ...but i need to work that in all my string ..(sample file given below)
Code:
cat input.txt
01903003673916970040504600904021820121008175705910040281413617891000000918439013338
01903003673916971040503000655201920121008173606353489041923240491000000919033006189
22503003673916973040503701720296920121008175603354131053633661491000000918983673026
22503003673916972040504700538884720121008175657358045042677480191000000919046068826

can u please help.

Thanks...

Last edited by Franklin52; 03-21-2013 at 07:10 AM.. Reason: Please use code tags for data and code samples
# 6  
Old 03-21-2013
Code:
while read string; do
  for len in 17 5 4 3; do
    string=`echo $string | sed "s/^\(.\{$len\}\)\(.*\)/\1,\2/"`
  done
  echo $string
  # or the awk code
done < input.txt

# 7  
Old 03-21-2013
To keep your code flexible, i.e. not restricted to a constant number of commas, and running sed just once on the entire file, you may want to try this:
Code:
$ POS="3 4 5 17 37 66" 
$ for i in $POS
    do sedcmd="s|.|&,|$i;"$sedcmd
    done
$ echo $sedcmd
s|.|&,|66;s|.|&,|37;s|.|&,|17;s|.|&,|5;s|.|&,|4;s|.|&,|3;

$ sed $sedcmd file
019,0,3,003673916970,04050460090402182012,10081757059100402814136178910,00000918439013338
019,0,3,003673916971,04050300065520192012,10081736063534890419232404910,00000919033006189
225,0,3,003673916973,04050370172029692012,10081756033541310536336614910,00000918983673026
225,0,3,003673916972,04050470053888472012,10081756573580450426774801910,00000919046068826

You may even want to consider to use a variable token:
Code:
$ TOK=","
$ for i in $POS; do sedcmd="s|.|&$TOK|$i;"$sedcmd; done


Last edited by RudiC; 03-21-2013 at 07:52 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extended ASCII Characters keep on getting reintroduced to text files

I am working with a log file that I am trying to clean up by removing non-English ASCII characters. I am using Bash via Cygwin on Windows. Before I start I set: export LC_ALL=C I clean it up by removing all non-English ASCII characters with the following command; grep -v $''... (4 Replies)
Discussion started by: lewk
4 Replies

2. UNIX for Dummies Questions & Answers

How to change a set of characters between delimiters?

Hi all, I am trying to write a script for renaming some files and I can't understand how to replace a set of characters between delimiters with sed. The current filename is: <ID1>_<ID2>_<DATE>_<ID3>.PDF And I need to get the following: <ID1>_<ID2>_<DATE>_X01<ID2>.PDF I have tried the... (5 Replies)
Discussion started by: bartleby
5 Replies

3. Shell Programming and Scripting

Put words to fix position in a file

Hi all, There are several lines in my file as a=123,b=dene,c=2312,d=234234,g=vxcvxcv,h=44 a=3,b=dene,c=22,d=23422342334,g=vxcvxcv,h=4 a=123,b=dene,c=2312,d=234234,g=vxcvxcv,h=678 I take values with this command awk -F '' '{print $1,$2,$3}' a.txt I want to put values to a fix position... (6 Replies)
Discussion started by: bahadiraktan
6 Replies

4. Shell Programming and Scripting

How to fix line breaks format text for huge files?

Hi, I need to correct line breaks for huge files (more than 1MM records in a file) and then format it properly. Except the header and trailer, each record starts with 'D'. Requirement:Scan the whole file except the header and trailer records and see if any of the records start with... (19 Replies)
Discussion started by: kikionline
19 Replies

5. Shell Programming and Scripting

How to fix file with more delimiters

Hi, gunius I have a post as following I use one of posted code resolved. code following. awk -F\, 'NF==5{$3=$3""$4;$4="";sub(/,,/,",")}1' OFS="," infile I got new problem, my file contains some null column, target should be 126,,shepperd street 203#, 23459 using current code, I got ... (4 Replies)
Discussion started by: ken002
4 Replies

6. Shell Programming and Scripting

Order text by delimiters

I try order the content from file by delimiters. This is the text: interface Loopback0 description !!!RID RR_SLT ip address 172.31.128.19 255.255.255.255 interface GigabitEthernet0 description !!!P_SLT GI0/0/9 ip address 172.31.130.246 255.255.255.252 and the result that I need... (11 Replies)
Discussion started by: bobbasystem
11 Replies

7. AIX

Print text between two delimiters

Hi, Can somebody help me with the below situation, Input File, ======== 2007_08_07_IA-0100-014_(MONTHLY).PDF 2007_08_07_IA-0100-031_(QUARTERLY)(RERUN).PDF 2008-02-28_KR-1022-003_(MONTH)(RERUN)(REC1).CSV Required output, ============ MONTHLY QUARTERLY MONTH ... (15 Replies)
Discussion started by: sravicha
15 Replies

8. Programming

c program to extract text between two delimiters from some text file

needa c program to extract text between two delimiters from some text file. and then storing them in to diffrent variables ? text file like 0: abc.txt ========= aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass... (7 Replies)
Discussion started by: kukretiabhi13
7 Replies

9. UNIX for Advanced & Expert Users

need help to put pipe(|) in between characters

Hi, I need to put | bet ween every 3 characters in a string. InString = abcd12342 Out = abc|d12|342 Thanks (3 Replies)
Discussion started by: ukatru
3 Replies

10. AIX

Randomly appearing control characters in text files

Hi, From some time, we have noticed that our ascii files have started corrupting due to the presence of some random control characters (^@, ^M, ^H, ^D). The characters appear randomly on any file after the process that creates the file finishes. If we rerun the process, the files re creates... (0 Replies)
Discussion started by: aakashahuja
0 Replies
Login or Register to Ask a Question