Sponsored Content
Top Forums Shell Programming and Scripting How to get front and back parameter of each characters? Post 302974159 by RavinderSingh13 on Thursday 26th of May 2016 03:00:19 AM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 02:58 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy