splitting long string into several lines?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting splitting long string into several lines?
# 1  
Old 06-26-2010
splitting long string into several lines?

I'm using a barcode scanner to grab ISBNs. Unfortunately, short of hitting "enter" each time (not easy while on a ladder), there's no good way to split it up. So I scanned it into a series of long lines in notepad.

Now, I need to split each line into 12-number lines.
instead of:
Code:
111111111111222222222222333333333333444444444444
555555555555
666666666666777777777777

I want:
Code:
111111111111
222222222222
333333333333
444444444444
555555555555
666666666666
777777777777

Anybody have an easy way? I guess I could use CUT several times, but given the length of each line (several hundred characters, maybe more) I figure there's a better way to do it, probably SED or the like.

Tried looking on usenet, but using google groups to find the appropriate group is a pain.

Thanks in advance.

Last edited by radoulov; 06-26-2010 at 02:21 PM.. Reason: Please use code tags!
# 2  
Old 06-26-2010
Code:
fold -w12 infile

# 3  
Old 06-28-2010
Awesome! I'll give it a shot. Many thanks!
# 4  
Old 07-02-2010
Friend came up with this. More complex version: fed a series of numbers, after a pause it will force a new line. For the scanner I used, works like a charm.
Code:
#!/bin/bash

READFLAG=0;
char="";
EXIT="";

while [ 1 :]; do
read -t1 -n1 char
if [ "$?" == "0" :]; then
echo -n $char;
READFLAG=1;
# echo "readflag is $READFLAG";

elif [ "$READFLAG" -eq "1" :]; then
echo "" # endline to out
echo -e '\a' # bell to notify we are ready
READFLAG=0;
# echo "readflag is $READFLAG";
fi
done


Last edited by radoulov; 07-02-2010 at 04:10 AM.. Reason: Please use code tags!
# 5  
Old 07-02-2010
And for a few more cases -

Code:
$
$
$ cat barcodes
111111111111222222222222333333333333444444444444
555555555555
666666666666777777777777
888
8
88
88
8
888
99
99
99
 
 
99
99
99
$
$ perl -ne 'chomp; $x .= $_; if (length($x) >= 12){ do{$x=~/^(.{12})(.*)$/ and print $1,"\n" and $x=$2} until (length($x)<12) }' barcodes
111111111111
222222222222
333333333333
444444444444
555555555555
666666666666
777777777777
888888888888
999999999999
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Break one long string into multiple fixed length lines

This is actually a KSH under Unix System Services (Z/OS), but hoping I can get a standard AIX/KSH solution to work... I have a very large, single line file in Windows, that we download via FTP, with the "SITE WRAP" option, into a Z/OS file with an LRECL of 200. This essentially breaks the single... (4 Replies)
Discussion started by: bubbawuzhere
4 Replies

2. Shell Programming and Scripting

Splitting one line into multiple lines

Hi, I have one problem scenorio as below. my source file is : cat input_file. "hi","there","how","are","you?","It","was","great","working","with","you.","hope","to","work","y ou." my output should be like. "hi","there","how","are","you?", "It","was","great","working","with",... (7 Replies)
Discussion started by: abhilash_nakka
7 Replies

3. Shell Programming and Scripting

Parsing a long string string problem for procmail

Hi everyone, I am working on fetchmail + procmail to filter mails and I am having problem with parsing a long line in the body of the email. Could anyone help me construct a reg exp for this string below. It needs to match exactly as this string. GetRyt... (4 Replies)
Discussion started by: cwiggler
4 Replies

4. Shell Programming and Scripting

Splitting string with awk

Input: Debris Linux is a minimalist, desktop-oriented distribution and live CD based on Ubuntu. It includes the GNOME desktop and a small set of popular desktop applications, such as GNOME Office, Firefox web browser, Pidgin instant messenger, and ufw firewall manager. Debris Linux ships... (5 Replies)
Discussion started by: cola
5 Replies

5. UNIX for Dummies Questions & Answers

Help with splitting a string..

Hello I need help with the following. I have strings like #if defined(__def1__) #if defined(__def1__) || defined(__def2__) #if defined(__def1__) && defined(__def2__) && defined(__def3__). #if defined(__def1__) || defined(__def2__) || defined(__def3__). I need to print what is there in... (4 Replies)
Discussion started by: srk
4 Replies

6. UNIX for Dummies Questions & Answers

Splitting a string and putting another string in the middle?

Hello all! I'm trying to put together a small script that will take in a file name and attach a datestamp to the end of it (but before the file type extension). To illustrate... Before: filename.txt anotherfilename.txt After: filename_20090724.txt anotherfilename_20090724.txt ... (7 Replies)
Discussion started by: jisoo411
7 Replies

7. Shell Programming and Scripting

Splitting the line in multiple lines

Hi Guys, I need a help. I am pretty new to the shell level programing. I was trying to split a long lines to a multiple lines with few conditions. I goggle for the code and found some snippets and tried to modified it but I got few strange problems too. I have to split the lines if line is ... (3 Replies)
Discussion started by: dd_sh
3 Replies

8. Shell Programming and Scripting

Splitting a string

Hi, I am new to shell scripting, Can any body suggest me how I can split a string with a delimiter as whitespace into words and store into a array. I have read a line from file, now I want to split the line into words and store in a array for further use. eg : the sky is blue want... (3 Replies)
Discussion started by: smk
3 Replies

9. Shell Programming and Scripting

string splitting

Hi, I have a string like 'xyz' and i want to to split the above string into letters for comparision purpose. any idea pls. cheers RRK. (3 Replies)
Discussion started by: ravi raj kumar
3 Replies

10. UNIX for Advanced & Expert Users

Help with splitting lines in a file using awk

I have a file which is one big long line of text about 10Kb long. Can someone provide a way using awk to introduce carriage returns every 40 chars in this file. Any other solutions would also be welcome. Thank you in advance. (5 Replies)
Discussion started by: martinbarretto
5 Replies
Login or Register to Ask a Question