How to split a string with no delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to split a string with no delimiter
# 1  
Old 03-01-2011
How to split a string with no delimiter

Hi;

I want to write a shell script that will split a string with no delimiter.
Basically the script will read a line from a file.

For example the line it read from the file contains:
99234523

These values are never the same but the length will always be 8.

How do i split this string into 4 sections of 2?

Thanks
# 2  
Old 03-01-2011
Try:
Code:
perl -pe 's/(..)/\1 /g' file

# 3  
Old 03-01-2011
Code:
% fold -2 infile
99
23
45
23

# 4  
Old 03-01-2011
Bash, ksh93, ...
Code:
while read line
do
      a=${line:0:2}
      b=${line:2:2}
      c=${line:4:2}
      d=${line:6:2}
done

# 5  
Old 03-01-2011
Sorry forgot to say this will be a shell script

Thanks
# 6  
Old 03-01-2011
But what shell?
# 7  
Old 03-01-2011
Code:
echo "99234523" |awk 'BEGIN{FIELDWIDTHS="2 2 2 2"}{print $1 RS $2 RS $3 RS $4}'
99
23
45
23

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to split string on single quote as delimiter

I have a variable that contains the following string: FPATH=-rw-rw-r-- 1 user1 dba 0 Aug 7 13:14 /app/F11.3/app/cust/exe/filename1.exe' -rw-rw-r-- 1 user1 dba 0 Aug 19 10:09 /app/app/F11.3/app/cust/sql/33211.sql' -rw-r--r-- 1 user1 dba 0 Aug 6 17:20 /app/F11.2/app/01/mrt/file1.mrt' I... (7 Replies)
Discussion started by: mohtashims
7 Replies

2. UNIX for Advanced & Expert Users

How to split large file with different record delimiter?

Hi, I have received a file which is 20 GB. We would like to split the file into 4 equal parts and process it to avoid memory issues. If the record delimiter is unix new line, I could use split command either with option l or b. The problem is that the line terminator is |##| How to use... (5 Replies)
Discussion started by: Ravi.K
5 Replies

3. Shell Programming and Scripting

How to target certain delimiter to split text file?

Hi, all. I have an input file. I would like to generate 3 types of output files. Input: LG10_PM_map_19_LEnd_1000560 LG10_PM_map_6-1_27101856 LG10_PM_map_71_REnd_20597718 LG12_PM_map_5_chr_118419232 LG13_PM_map_121_24341052 LG14_PM_1a_456799 LG1_MM_scf_5a_opt_abc_9029993 ... (5 Replies)
Discussion started by: huiyee1
5 Replies

4. Shell Programming and Scripting

Split file into multiple files using delimiter

Hi, I have a file which has many URLs delimited by space. Now i want them to move to separate files each one holding 10 URLs per file. http://3276.e-printphoto.co.uk/guardian http://abdera.apache.org/ http://abdera.apache.org/docs/api/index.html I have used the below code to arrange... (6 Replies)
Discussion started by: vel4ever
6 Replies

5. Shell Programming and Scripting

split file by delimiter with csplit

Hello, I want to split a big file into smaller ones with certain "counts". I am aware this type of job has been asked quite often, but I posted again when I came to csplit, which may be simpler to solve the problem. Input file (fasta format): >seq1 agtcagtc agtcagtc ag >seq2 agtcagtcagtc... (8 Replies)
Discussion started by: yifangt
8 Replies

6. Shell Programming and Scripting

split record based on delimiter

Hi, My inputfile contains field separaer is ^. 12^inms^ 13^fakdks^ssk^s3 23^avsd^ 13^fakdks^ssk^a4 I wanted to print only 2 delimiter occurence i.e 12^inms^ 23^avsd^ (4 Replies)
Discussion started by: Jairaj
4 Replies

7. Shell Programming and Scripting

Split into columns based on the parameter and use & as delimiter

Here is my source, i have million lines like this on a file. disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=& drc=&mo=&sid=&lang=EN&loc=JPN I want to split this into columns in order to load in database, anything starts with"&mjv=6" as first... (13 Replies)
Discussion started by: elamurugu
13 Replies

8. Shell Programming and Scripting

renaming files using split with a delimiter

I have a directory of files that I need to rename by splitting the first and second halves of the filenames using the delimiter "-O" and then renaming with the second half first, followed by two underscores and then the first half. For example, natfinal1995annvol1_14.pdf -O filenum-20639 will be... (2 Replies)
Discussion started by: swimulator
2 Replies

9. Shell Programming and Scripting

Awk split doesn't work for empty delimiter

Does anyone know how will I make awk's split work with empty or null separator/delimiter? echo ABCD | awk '{ split($0,arr,""); print arr; }' I need output like: A B C D I am under HP-UX (5 Replies)
Discussion started by: Orbix
5 Replies

10. Shell Programming and Scripting

split string with multibyte delimiter

Hi, I need to split a string, either using awk or cut or basic unix commands (no programming) , with a multibyte charectar as a delimeter. Ex: abcd-efgh-ijkl split by -efgh- to get two segments abcd & ijkl Is it possible? Thanks A.H.S (1 Reply)
Discussion started by: azmathshaikh
1 Replies
Login or Register to Ask a Question