Results 1 to 2 of 2
I would like make a sed
In fact, I would replace it in my input file, position 54 by Z, where in position 54 is blank. So there is a ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 02-07-2012 #1Just Joined!
- Join Date
- Feb 2012
- Posts
- 1
Replace position to a file
I would like make a sed
In fact, I would replace it in my input file, position 54 by Z, where in position 54 is blank. So there is a requirement to add.
could someone help me please
- 02-16-2012 #2Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,199
Hi.
Welcome to the forum.
Here is one solution for specific position character replacement. The surrounding lines are to display the computing environment:
producing:Code:#!/usr/bin/env bash # @(#) s1 Demonstrate character replacement by position, sed. # Utility functions: print-as-echo, print-line-with-visual-space, debug. # export PATH="/usr/local/bin:/usr/bin:/bin" pe() { for _i;do printf "%s" "$_i";done; printf "\n"; } pl() { pe;pe "-----" ;pe "$*"; } edges() { local _f _n _l;: ${1?"edges: need file"}; _f=$1;_l=$(wc -l $_f); head -${_n:=3} $_f ; pe "--- ( $_l: lines total )" ; tail -$_n $_f ; } db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; } db() { : ; } C=$HOME/bin/context && [ -f $C ] && $C sed FILE=${1-data1} pl " Input data file $FILE:" cat $FILE pl " Results:" sed 's/^\(.\{53\}\) /\1Z/' $FILE exit 0
See man sed for details.Code:% ./s1 Environment: LC_ALL = C, LANG = C (Versions displayed with local utility "version") OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64 Distribution : Debian GNU/Linux 5.0.8 (lenny) bash GNU bash 3.2.39 sed GNU sed version 4.1.5 ----- Input data file data1: xxxxxxxxx1xxxxxxxxx2xxxxxxxxx3xxxxxxxxx4xxxxxxxxx5xxxxxxxxx6xxxxxxxxx7xxxxxxxx 12345678901234567890123456789012345678901234567890123 567890123456789012345678 xxxxxxxxx1xxxxxxxxx2xxxxxxxxx3xxxxxxxxx4xxxxxxxxx5xxxxxxxxx6xxxxxxxxx7xxxxxxxx 123456789012345678901234567890123456789012345678901234567890123456789012345678 ----- Results: xxxxxxxxx1xxxxxxxxx2xxxxxxxxx3xxxxxxxxx4xxxxxxxxx5xxxxxxxxx6xxxxxxxxx7xxxxxxxx 12345678901234567890123456789012345678901234567890123Z567890123456789012345678 xxxxxxxxx1xxxxxxxxx2xxxxxxxxx3xxxxxxxxx4xxxxxxxxx5xxxxxxxxx6xxxxxxxxx7xxxxxxxx 123456789012345678901234567890123456789012345678901234567890123456789012345678
Note:
Best wishes ... cheers, drlAdvice for forum posts, general:
To obtain the best answers quickly for processing datasets --
extracting, transforming, filtering, you should:
1. Post representative samples of your data (i.e. data that
should "succeed" and data that should "fail")
2. Post what you expect the results to be, in addition to
describing them. Be clear about how the results are to be
obtained, e.g. "add field 2 in file1 to field 3 from file2"
3. Post what you have attempted to do so far.
4. Place the data and expected output within CODE tags, so that
they are more easily readable.
Special cases, exceptions, etc., are very important to include
in the samples.Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote
