Insert and shifting data at column

Hi all,
i have data like this

joe  :          1          :a
bob :          2          :b
sue            : 3         :c
foo            : 4          :d

at column 2 i want to insert TOP to the top column and at column 3 i want to insert BOTTOM to the bottom column. and the result will like this..

joe          :TOP            :b
bob:            1                      :c
sue              : 2                       :d
foo              :3              :BOTTOM

how i do that with awk..
thanks for your help

$ nawk -F\: 'NR==1{f=$0;s=$2}{a[i++]=$0;l=$0;ls=$2;next}END{sub(s," TOP ",f);print f;for(j=1;j<i-1;j++){print a[j]};sub(ls," BOTTOM ",l);print l}' input.txt
joe : TOP :a
bob : 2 :b
sue : 3 :c
foo : BOTTOM :d
 
user@linux-aah2:~/user/learn> cat data.txt
joe : 1 :a
bob : 2 :b
sue : 3 :c
foo : 4 :d

Script:

 
#!/bin/ksh
awk -F: -v len=$(wc -l < data.txt) '{
if ( NR==1 )
        print $1":TOP:"$3
else if ( NR==len )
        print $1":"$2":BOTTOM"
else
print
}' data.txt

Output:

 
joe :TOP:a
bob : 2 :b
sue : 3 :c
foo : 4 :BOTTOM

 
awk -F\: 'NR==1{f=$0;s=$2}{a[i++]=$0;l=$0;ls=$3;next}END{sub(s," TOP ",f);print f;for(j=1;j<i-1;j++){print a[j]};sub(ls," BOTTOM ",l);print l}' input.txt

thanks itkamaraj by the result still like asterix script..i want in third column shifting like this

b
c
d
BOTTOM

No need to make a pass over the file to count lines, nor to cache the data in an array:

awk -F ":" '
    function p( c2 )
    {
        printf( "%s : %s :", $1, c2 );
    }
    BEGIN {
        getline;
        p( "TOP" );
    }
    {
        printf( "%s\n", $3 );
        p( $2 );
    }
    END {
        printf( "BOTTOM\n" );
    }
' input-file
1 Like

thanks agama it works but something miss, the result i want should be like this

 
joe :TOP:b
bob : 1 :c
sue : 2 :d
foo : 3 :BOTTOM

your script result is like this

 
joe :TOP:b
bob : 2 :c
sue : 3 :d
foo : 4 :BOTTOM

BTW sorry to ask you all a lot question..i am a newbie in scripting , i am a geologist. I really need this script to edit my data and saving my time.
Thanks For all your help guys

Try:

sed 's/[ \t][ \t]*/ /g; 1s/:[^:]*/:TOP/; $s/:[^:]*/:BOTTOM/2' infile

hi scrutnizer, your script output like this

joe:TOP: a
bob: 2: b
sue: 3: c
foo: 4:BOTTOM

all i need is...shfting down column 2 and add TOP at the top column and shfting up column 3 and add BOTTOM at the bottom column...and th result will be like this

joe:TOP:b 
bob: 1: c
sue: 2: d
foo: 3:BOTTOM

Funny how we all seem to be misreading your requirements :slight_smile: Try this then:

awk -F'[ \t:]*' 'n{print n,$3} {n=$1 OFS (NR==1?"TOP":p); p=$2} END{print n,"BOTTOM"}' OFS=: infile

Output:

joe:TOP:b
bob:1:c
sue:2:d
foo:3:BOTTOM
1 Like

yup..maybe because my english is bad...:)..

your script working perfectly...THANK YOU VERY MUCH Scrutinizer :b: :b: :b:

Yep, I tweeked it a bit and didn't notice that the output was bad. Changed code below if you are still looking for something:

awk -F ":" '
    function p( c2 )
    {
        printf( "%s : %s :", $1, c2 );
    }
    BEGIN {
        getline;
        p( "TOP" );
        c2 = $2
    }
    {
        printf( "%s\n", $3 );
        p( c2 );
        c2 = $2;
    }
    END {
        printf( "BOTTOM\n" );
    }
'
1 Like