String Processing


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers String Processing
# 1  
Old 02-19-2008
String Processing

I had a file with 150k records in it and I ran a tr on it to remove all new lines/CR which created one large record(whoops). Is there a way to add a \n after every 39th character without using 'dd' to turn it back into the original format?

dd is way to slow.

shell is ksh.
# 2  
Old 02-19-2008
Hi.

Here is one way with perl:
Code:
#!/usr/bin/perl

# @(#) p1       Demonstrate read fixed-length records.
#           See Recipe 8.15 perl Cookbook, Ed 1.

use warnings;
use strict;

my ($debug);
$debug = 0;
$debug = 1;

my ($lines) = 0;
my ( $f, $file );
my ($record);
my ($RECORDSIZE) = 10;

$f = shift || die " Need a file name.\n";
open( $file, "<", $f ) || die " Cannot open $f\n";
until ( eof($file) ) {
  $lines++;
  read( $file, $record, $RECORDSIZE ) == $RECORDSIZE
    or die " Short read on $f at line $lines\n";
  print $record, "\n";
}

print STDERR " ( Lines written: $lines )\n";

exit(0);

which can be called from a ksh script:
Code:
#!/bin/ksh -

# @(#) s1       Demonstrate perl reading fixed-length records.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) tr perl

FILE=data1
tr -d '\n' <data0 >$FILE

echo
echo " Input file $FILE:"
cat $FILE

echo
echo
echo " Output from perl script:"
./p1 data1

exit 0

Producing:
Code:
% ./s1
(Versions displayed with local utility "version")
Linux 2.6.11-x1
pdksh 5.2.14 99/07/13.2
tr (coreutils) 5.2.1
perl 5.8.4

 Input file data1:
a234567890b234567890c234567890d234567890e234567890f234567890g234567890h234567890i234567890j234567890

 Output from perl script:
a234567890
b234567890
c234567890
d234567890
e234567890
f234567890
g234567890
h234567890
i234567890
j234567890
 ( Lines written: 10 )

Change the record length to fit your situation ... cheers, drl
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Processing arguments in a string

Hi The following code works when reading the arguments from the command line but fails when I try to read from a string. So this works while ; do case $1 in -dbversion) if '`" ]; then { echo "ERROR: missing value for '$1' (seen '$2')"; usage; exit 1; } else { shift;... (6 Replies)
Discussion started by: user052009
6 Replies

2. Programming

awk processing / Shell Script Processing to remove columns text file

Hello, I extracted a list of files in a directory with the command ls . However this is not my computer, so the ls functionality has been revamped so that it gives the filesizes in front like this : This is the output of ls command : I stored the output in a file filelist 1.1M... (5 Replies)
Discussion started by: ajayram
5 Replies

3. Shell Programming and Scripting

Processing 1 match string from grep at a time

hi all, i have a few log files with dates that are incorrrect (please don't ask me why). i need to add 2852 days, 16 hours, and 21 minutes (246471660 seconds) to each of these dates to correct them. i need to write to new "test2.txt" file that corrects the dates found by grep and yet have it... (4 Replies)
Discussion started by: cilantrocado
4 Replies

4. Shell Programming and Scripting

[Python]StringVar() Class String processing

I have a requirement where I collect inputs from entry widget in gui(via variables a,b,c) and then output a string like this: -a a -b b -c c. Hence if I have given a=1 b=2 c=3, The output string is --> -a 1 -b 2 -c 3 or if I have given a=1 b=2 (c left blank) then the output is --> -a 1... (1 Reply)
Discussion started by: animesharma
1 Replies

5. Shell Programming and Scripting

[string processing]Adding new line in file

I have a report file which somewhere has a lines starting with word ".sub" I have a new variable named $new. What i am trying to do is insert the content of $new just before the occurrence of ".sub" in the report file. How can I do that? (11 Replies)
Discussion started by: animesharma
11 Replies

6. Shell Programming and Scripting

String processing in CSH

Please delete it (0 Replies)
Discussion started by: animesharma
0 Replies

7. Shell Programming and Scripting

String processing to compare dates

Hi all, I have been scripting a shell script to allow me to easily create htaccess users and add them to existing htaccess groups etc. However, the main part of the script that I am trying to accomplish is to store additional metadata in comments in the htpasswd file. I am trying to store an... (8 Replies)
Discussion started by: joshuaspence
8 Replies

8. Shell Programming and Scripting

Appending string to match pattern (data processing)

Hello i have go the following result from performing 2 testing using the same file. I have used unix script to extract the result because the files are many as shown below. 01_gravity.f.tcov 7 3 42.86 02_gravity.f.tcov 9 4 80.86... (4 Replies)
Discussion started by: ganiel24
4 Replies

9. Shell Programming and Scripting

string processing

hi, I have a string "satabeltodounixscriptingpleasecheckfortheerros" in the above line if it contains "unix" , i need to take 5 characters after that word. please help thanks in advance Satya (2 Replies)
Discussion started by: Satyak
2 Replies
Login or Register to Ask a Question