Find the answer to your Linux question:
Results 1 to 5 of 5
Hey guys, I've been searching and searching and hell I can't find the solution to my problem. Maybe someone here can shed some light on the problem. Here goes... Basically, ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    7

    Bash Script - Using sed or similar? HELP!

    Hey guys,

    I've been searching and searching and hell I can't find the solution to my problem. Maybe someone here can shed some light on the problem.

    Here goes...

    Basically, here is my 'sample code' ...

    sn=30
    var1=`echo "Johnny" |wc -L`
    res=`echo "${sn}-${var1}" |bc`

    First line is just a variable definition.
    Second line is printing out the length of characters, in this case "6".
    Third line is calculating "30-6" to echo me "24". Which it does.

    At this point, I want to use (perhaps 'sed'?) to write a file with "Johnny" and 24 white spaces after.

    So in my file, I would have: Johnny[24 spaces]

    How can I achieve this?

    Someone please help me

    Thanks!

    Scorp.

  2. #2
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Well, a few ways you could do this. An easy one is

    Code:
    echo -n "Johnny" > outfile
    COUNTER=0
    while [ $COUNTER -le $res ]; do
       echo -n ' ' >> outfile
       let COUNTER+=1
    done

  3. #3
    Just Joined!
    Join Date
    Oct 2007
    Posts
    7

    Doesn't work

    Hey,

    Is there another way to do this?

    For some reason this won't work in my code.

    Let me know if there's any other method that I can use.

    Thanks!

    P.

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    echo "Johnny" | awk '{l=30-length($0);j="%s%"l"s"
     printf j , l," " > "outfile"
    }'

  5. #5
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    ...
    Code:
    sn=30
    var1="Johnny"
    printf "%-"$sn"s\n" "$var1">yourfile

Posting Permissions

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