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
Can I do this with sed, if so, how, or do I need to use awk?
JL
> Can I do this with sed, if so, how, or do I need to use awk?
| 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/
orQuote:} < file | dd conv=notrunc of=file 2> /dev/null
sed '1s/\(....\)....../\1GORDON/' < file 1<> file
--
Stphane
>> 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:
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
Well... stupid I am. The point in using notrunc is that youQuote:> {
> 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
{
dd bs=4 count=1 < file 2> /dev/null
printf %s GORDON
orQuote:} 1<> file
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
> Can I do this with sed, if so, how, or do I need to use awk?
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
??? Don't you use sed or awk in shell scripts?Quote:> There's no need for sed or awk; a simple shell script will
> suffice;
[...]
should be:Quote:> FILE=/path/to/file
> NEWSTRING=GORDON
> TEMPFILE=$FILE.tmp
> IFS= read -r line < $FILE
> line2=${line#?????}
line2=${line#????}
Should be:Quote:> line1=${line%$line2}
line1=${line%"$line2"}
should be:Quote:> {
> echo "$line1$NEWSTRING${line2#??????}"
printf '%s\n' "$line1$NEWSTRING${line2#??????}"
You'd better take the habit to quote variable references (orQuote:> tail +2 $FILE
> } > $TEMPFILE
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
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
1. sed/awk question : delete first blank in every line
dear people,
can you help me with this :
I have a file with datasets.
1 23 44
2 34 23
3 33 32
How do I remove the first blank in every line ?
thanks !
christian
3. Grep/awk/sed for multiple/groups of lines in a file???
4. Can't umount /usr - device is busy
5. libform for Solaris, where to find ?
6. DVD player software can't play sound
7. In Ksh/awk/sed how can i read specific line (like line 45 in tmp.txt)
8. Grep/awk/sed for multiple/groups of lines in a file???
9. sed/awk : need just the first column in a multi-column file
10. what sed command to print the first line and the last line
11. sed - delete first line of text file