amend first line of file : sed..awk?

amend first line of file : sed..awk?

Post by John Lesl » Sat, 05 Jul 2003 22:39:25



I need a shell script that will change characters 5-10 in the first
line of file to a specified string, i.e "GORDON". I will not know what
the original value is.

Can I do this with sed, if so, how, or do I need to use awk?

JL

 
 
 

amend first line of file : sed..awk?

Post by Tim Hayne » Sat, 05 Jul 2003 22:45:50



> I need a shell script that will change characters 5-10 in the first
> line of file to a specified string, i.e "GORDON". I will not know what
> the original value is.

> Can I do this with sed, if so, how, or do I need to use awk?

Sure, sed'll suffice. Consider:

 | zsh, purple  1:44PM tim/ % echo 'knickers foo wibble
 | pants bar bash' | sed '1,1s/^...../GORDON/'
 | GORDONers foo wibble
 | pants bar bash

HTH,

~Tim
--

be criminalised. (Bill Unruh)               |http://spodzone.org.uk/

 
 
 

amend first line of file : sed..awk?

Post by Stephane CHAZELA » Sat, 05 Jul 2003 22:56:24



> I need a shell script that will change characters 5-10 in the first
> line of file to a specified string, i.e "GORDON". I will not know what
> the original value is.

{
  dd bs=4 count=1 2> /dev/null
  printf %s GORDON
  dd bs=6 count=1 > /dev/null 2>&1
  cat

Quote:} < file | dd conv=notrunc of=file 2> /dev/null

or

sed '1s/\(....\)....../\1GORDON/' < file 1<> file

--
Stphane

 
 
 

amend first line of file : sed..awk?

Post by Tim Hayne » Sat, 05 Jul 2003 22:55:54




>> I need a shell script that will change characters 5-10 in the first
>> line of file to a specified string, i.e "GORDON". I will not know what
>> the original value is.

>> Can I do this with sed, if so, how, or do I need to use awk?

> Sure, sed'll suffice. Consider:

[snip]

As a more accurate example, also consider:

 | zsh, purple  1:54PM tim/ % echo 'knickers foo wibble
 | pants bar bash' | sed '1,1s/^\(.....\)...../\1GORDON/'
 | knickGORDONoo wibble
 | pants bar bash

HTH,

~Tim
--
   14:55:30 up 28 days,  5:28, 11 users,  load average: 0.04, 0.04, 0.10

http://piglet.is.dreaming.org     |Another apt-get dist-upgrade

 
 
 

amend first line of file : sed..awk?

Post by Stephane CHAZELA » Sat, 05 Jul 2003 23:09:20


[...]

Quote:> {
>   dd bs=4 count=1 2> /dev/null
>   printf %s GORDON
>   dd bs=6 count=1 > /dev/null 2>&1
>   cat
> } < file | dd conv=notrunc of=file 2> /dev/null

> or

> sed '1s/\(....\)....../\1GORDON/' < file 1<> file

Well... stupid I am. The point in using notrunc is that you
don't need to process the whole file:

{
  dd bs=4 count=1 < file 2> /dev/null
  printf %s GORDON

Quote:} 1<> file

or

sed '1s/\(....\)....../\1GORDON/;q' < file 1<> file

replace "1<>" with "| dd conv=notrunc of=file 2> /dev/null"
if your shell doesn't have "<>".

Beware that the replacement string must be exactly as long as
the replaced one.

--
Stphane

 
 
 

amend first line of file : sed..awk?

Post by Chris F.A. Johnso » Sun, 06 Jul 2003 02:37:27



> I need a shell script that will change characters 5-10 in the first
> line of file to a specified string, i.e "GORDON". I will not know what
> the original value is.

> Can I do this with sed, if so, how, or do I need to use awk?

     There's no need for sed or awk; a simple shell script will
     suffice; the only external program needed is tail.

     This assumes a POSIX shell:

FILE=/path/to/file
NEWSTRING=GORDON
TEMPFILE=$FILE.tmp

IFS= read -r line < $FILE
line2=${line#?????}
line1=${line%$line2}
{
 echo "$line1$NEWSTRING${line2#??????}"
 tail +2 $FILE

Quote:} > $TEMPFILE

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
 
 
 

amend first line of file : sed..awk?

Post by Stephane CHAZELA » Sun, 06 Jul 2003 05:46:51


[...]

Quote:>      There's no need for sed or awk; a simple shell script will
>      suffice;

??? Don't you use sed or awk in shell scripts?

[...]

Quote:> FILE=/path/to/file
> NEWSTRING=GORDON
> TEMPFILE=$FILE.tmp

> IFS= read -r line < $FILE
> line2=${line#?????}

should be:

line2=${line#????}

Quote:> line1=${line%$line2}

Should be:

line1=${line%"$line2"}

Quote:> {
>  echo "$line1$NEWSTRING${line2#??????}"

should be:

printf '%s\n' "$line1$NEWSTRING${line2#??????}"

Quote:>  tail +2 $FILE
> } > $TEMPFILE

You'd better take the habit to quote variable references (or
always put ? IFS=; set -f ? at the beginning of your scripts or
use zsh).

Note that the dd solution I gave can cope with input with NUL
chars.

-rwxr-xr-x    1 root  root    30016 2002-10-31 22:00 /bin/dd*
-rwxr-xr-x    1 root  root    83132 2003-06-05 08:36 /bin/tail*

--
Stphane

 
 
 

amend first line of file : sed..awk?

Post by John Lesl » Thu, 10 Jul 2003 16:52:23


Thanks

sed '1,1s/^\(.....\)...../\1GORDON/'

works fine. Thanks

However, I now have a similar requirement where the characters to be
replaced are in characters 945 to 950 in the record...anyway to do
this without typing in all those ........ 's ????????

John




> >> I need a shell script that will change characters 5-10 in the first
> >> line of file to a specified string, i.e "GORDON". I will not know what
> >> the original value is.

> >> Can I do this with sed, if so, how, or do I need to use awk?

> > Sure, sed'll suffice. Consider:
> [snip]

> As a more accurate example, also consider:

>  | zsh, purple  1:54PM tim/ % echo 'knickers foo wibble
>  | pants bar bash' | sed '1,1s/^\(.....\)...../\1GORDON/'
>  | knickGORDONoo wibble
>  | pants bar bash

> HTH,

> ~Tim