How to get front and back parameter of each characters?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get front and back parameter of each characters?
# 1  
Old 05-26-2016
How to get front and back parameter of each characters?

I have list of words file. I trying to get a front and back parameter of each characters of words.

Code:
hello
....
....

Here is what I have done:

Code:
awk '{  word=$1; len=length(word); tlen=2*len-1;
        for (i=1; i<len; i++) a[i]="#";
        for (j=1; j<=len; j++) a[i+j-1]=substr(word,j,1);
        for (r=1; r<=len; r++)
            { for(i=1; i<=tlen; i++) printf "%s ", a[(i+r-2)%tlen+1];
              print " "
            }
        print " "
}' $1

Code:
# # # # h e l l o
# # # h e l l o #
# # h e l l o # #
# h e l l o # # #
h e l l o # # # #

But what I have expected to is

Code:
# # h e l  // null value replace by "#"
# h e l l
h e l l o
e l l o #
l l o # #

How to get 2 parameter front and back of each characters?
# 2  
Old 05-26-2016
Not clear. What is "2 parameter front and back"?
# 3  
Old 05-26-2016
Question is somewhat not clear.
just let us know..
what is expected and what you are getting. may be then we can help you.
# 4  
Old 05-26-2016
In this case the word is "hello"

Code:
                        h
                        e
h e l l o   - - ->      l 
                        l
                        o

focus on "h" ; before character h is null and assign with '#". After character h are "e" and "l".
focus on "e" ; before character e is "h". After character e are "l" and "l".
focus on "l" ; before character l are "h" and "e". After character l are "l" and "o".
focus on "l" ; before character l are "e" and "l". After character l is "o".
focus on "o" ; before character o are "l" and "l". After character o is null and assign with '#".

I would like to get 2 characters before and after of that focused on. I expect to get like below:

Code:
# # h e l  // 2 characters before h are "#" and "#". 2 characters after h are "e" and "l"
# h e l l  // 2 characters before e are "#" and "h". 2 characters after e are "l" and "l"
h e l l o  // 2 characters before l are "h" and "e". 2 characters after l are "l" and "o" 
e l l o #  // 2 characters before l are "e" and "l". 2 characters after l are "o" and "#"
l l o # #  // 2 characters before o are "l" and "l". 2 characters after o are "#" and "#"

# 5  
Old 05-26-2016
What if you got an even character count?
# 6  
Old 05-26-2016
Quote:
Originally Posted by paranrat
In this case the word is "hello"

Code:
                        h
                        e
h e l l o   - - ->      l 
                        l
                        o

focus on "h" ; before character h is null and assign with '#". After character h are "e" and "l".
focus on "e" ; before character e is "h". After character e are "l" and "l".
focus on "l" ; before character l are "h" and "e". After character l are "l" and "o".
focus on "l" ; before character l are "e" and "l". After character l is "o".
focus on "o" ; before character o are "l" and "l". After character o is null and assign with '#".

I would like to get 2 characters before and after of that focused on. I expect to get like below:

Code:
# # h e l  // 2 characters before h are "#" and "#". 2 characters after h are "e" and "l"
# h e l l  // 2 characters before e are "#" and "h". 2 characters after e are "l" and "l"
h e l l o  // 2 characters before l are "h" and "e". 2 characters after l are "l" and "o" 
e l l o #  // 2 characters before l are "e" and "l". 2 characters after l are "o" and "#"
l l o # #  // 2 characters before o are "l" and "l". 2 characters after o are "#" and "#"

Hello paranrat,

Could you please try following and let me know if this helps.
Code:
echo "h e l l o" | awk '{num=split($0, A," ");for(i=1;i<=num;i++){if(i==1){print "# #" OFS A[i] OFS A[i+1] OFS A[i+2]} else if(i==num){print A[i-2] OFS A[i-1] OFS A[i] OFS "# #"} else {Q=A[i-2] OFS A[i-1] OFS A[i] OFS A[i+1] OFS A[i+2];sub(/^[[:space:]]+/,"# ",Q);sub(/[[:space:]]+$/," #",Q);print Q}}}'

Output will be as follows.
Code:
# # h e l
# h e l l
h e l l o
e l l o #
l l o # #

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 05-26-2016
How about

Code:
awk '
        {L = length * 2
         M = int (L / 4)
         X = sprintf ("%*sY%*s", M, "", M, "")
         gsub (/ /, "#", X)
         sub  (/Y/, $1, X)
         gsub (/./, "& ", X)
         for (i=1; i<=L; i+=2) print substr (X, i, L-1)
        }
' file

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

A test command parameter is not valid, when special characters are tried to match

Hi I have a scenario where hyphen(-) from file should be ignored I used the following code if && ; then if ; then pow=$LINE echo $pow > history.txt flag=1 fi fi I get the following output ./valid.sh: -: 0403-012 A test... (7 Replies)
Discussion started by: Priya Amaresh
7 Replies

2. Shell Programming and Scripting

Special characters in parameter

Hello, I'm trying to write a simple (korn) shell script which is called from the command line with some parameters. But one of the parameter contains a "!" sign. For example: myscript.ksh foo bar foo!bar When I call the script like above I always get an error. So I tried to wrap the... (1 Reply)
Discussion started by: merlinhst123
1 Replies

3. Shell Programming and Scripting

Passing parameter to script, and split the parameter

i am passing input parameter 'one_two' to the script , the script output should display the result as below one_1two one_2two one_3two if then echo " Usage : <$0> <DATABASE> " exit 0 else for DB in 1 2 3 do DBname=`$DATABASE | awk -F "_" '{print $1_${DB}_$2}` done fi (5 Replies)
Discussion started by: only4satish
5 Replies

4. AIX

special characters in front of xml declaration

Hi I read xml files through mq and placed them on unix by using datastage as tool. I can see some special characters infront of declaration part for every xml file i have produced. below is the sample snippet when i opened the file by suing vi editor ^Z^E|^A^Z^Z<?xml version="1.0"... (1 Reply)
Discussion started by: dsdev_123
1 Replies

5. Shell Programming and Scripting

Command that takes one parameter and then searches for the passed in parameter

Hi I am looking for a unix command or a small shell script which can takes one parameter and then searches for the passed in the parameter in any or all files under say /home/dev/ Can anyone please help me on this? (3 Replies)
Discussion started by: pankaj80
3 Replies

6. IP Networking

Back-to-Back Connection using HBAs

Hi every body, Is it possible to connect two servers Back-to-Back (Point-to-Point) using HBA adapters & using Fiber. Note it is direct connection & there is no switches between the servers. I'm concern about using HBA adapters, it is possible or not. Thanks in advance. :) (3 Replies)
Discussion started by: aldowsary
3 Replies

7. AIX

back to back printing in UNIX

Hi , Can you suggest me how to back to back printing in UNIX? Is there any way? Kindly advise. Regards Vijaya Amirtha Raj (3 Replies)
Discussion started by: amirthraj_12
3 Replies

8. Shell Programming and Scripting

how do I make dynamic parameter names? Or get the value of a parameter evaluated twi

Say I write something like the following: var1=1 var2=2 for int in 1 2 do echo "\$var$int" done I want the output to be: 1 2 Instead I get something like: $var1 $var2 (2 Replies)
Discussion started by: Awanka
2 Replies

9. Shell Programming and Scripting

Converting %## back to special characters from an HTML form

I have an HTML form that sends email to a large list of users one at a time by matching an email address in peoplesoft to their username. It works great, except that special characters are converted to %## format. Is there a library of these I can use to sed them back (yes this is a crappy UNIX... (1 Reply)
Discussion started by: 98_1LE
1 Replies

10. Shell Programming and Scripting

pass parameter back to calling program

Hi all, I am running AIX version 4. I have a shell script that is calling another script. I want the called script to obtain a value and pass it back to the calling script. So far, I know that to pass a parameter to a called script is as such: sh proc2.sh $1 $2 etc. What I don't know how... (11 Replies)
Discussion started by: jthomas
11 Replies
Login or Register to Ask a Question