Computing.Net > Forums > Unix > Replace based on position

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Replace based on position

Reply to Message Icon

Name: anilcgowda
Date: November 23, 2005 at 13:58:33 Pacific
OS: UNIX
CPU/Ram: 2GB
Comment:

Hi,

I want to replace few characters based on position.

For example, I have the following string:
202204051121010000033333301000086963000000000000000000000000000000000000000000
in which the characters in 7th position to 12th are date. I want to replace the characters in 7th position to 12th position with today's date. That is replace 051121 with 051123.

I will be having today's date in a variable. I need to assign that to 7th - 13th position.

Can you please let me know how to change this.

Thanks,
Anil




Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: November 23, 2005 at 16:05:23 Pacific
Reply:

I think the easiest way is to cut the string at columns 1 to 6 and postion 13 to the end of the line. Then glue them back together with today's date:

#!/bin/ksh

line="202204051121010000033333301000086963000000000000000000000000000000000000000000"

l1=$(echo "$line"|cut -c1-6)
length=${#line}

# build today's date
l2=$(date "+%m%d%y")

l3=$(echo $line|cut -c13-${length})

newline="${l1}${l2}${l3}"
echo $newline


0

Response Number 2
Name: FishMonger
Date: November 26, 2005 at 08:10:54 Pacific
Reply:

Wouldn't a single regex be easier?

#!/usr/bin/perl

$str = '202204051121010000033333301000086963000000000000000000000000000000000000000000';
$date = `date "+%m%d%y"`;

$str =~ s/^(.{6})(.{6})(.*)$/$1$date$3/;
print $str;


0

Response Number 3
Name: nails
Date: November 26, 2005 at 10:23:19 Pacific
Reply:

Fishmonger:

Yes, it is easier if I knew perl. -)

I tried your perl script on Red Hat Linux and Solaris, and each case I had to chop the newline to get the output right:

.
$date = `date "+%m%d%y"`;
chop($date);
.

Would you mind explaining the regex you used.

Thanks

Nails



0

Response Number 4
Name: FishMonger
Date: November 26, 2005 at 11:09:26 Pacific
Reply:

It's a substitution regex; here's its break down.

^
anchors it to the beginning of the string.

(.{6})
captures/puts the first 6 characters onto $1 like your first echo/cut statement

(.{6})
captures the next 6 chars (the date) into $2 like your second echo/cut. Actually, since it's not being used, we don't need to capture the date into a var; we just need to do the matching.

(.*)
captures the remaining chars into $3

$
anchors to the end of the string

That was the matching part, the rest just “glues” together the parts $1 $date and $3 and assigns it back to $str.

The removing of the \n from $date can be done in its assignment.

chomp($date = `date "+%m%d%y"`);

chomp only removes the newline char
chop removes any char

I don't do any shell scripting, but you can use the same regex with sed, or possibly with awk.



0

Response Number 5
Name: FishMonger
Date: November 26, 2005 at 11:11:22 Pacific
Reply:

Correction:

(.*) is the same as your second echo/cut statement


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

Any DirectoryListener in ... file copy between 2 termi...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Replace based on position

renaming files based on date arguments www.computing.net/answers/unix/renaming-files-based-on-date-arguments/2499.html

ftp using mget based on time stamp www.computing.net/answers/unix/ftp-using-mget-based-on-time-stamp-/6625.html

renaming a file based on systm date www.computing.net/answers/unix/renaming-a-file-based-on-systm-date/4875.html