Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #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

  2. #2
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris

    Welcome to the forum.

    Here is one solution for specific position character replacement. The surrounding lines are to display the computing environment:
    #!/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
    pl " Input data file $FILE:"
    cat $FILE
    pl " Results:"
    sed 's/^\(.\{53\}\) /\1Z/' $FILE
    exit 0
    % ./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:
    12345678901234567890123456789012345678901234567890123 567890123456789012345678
    See man sed for details.

    Advice 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.
    Best wishes ... cheers, drl
    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 )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts