Sponsored Content
Top Forums Shell Programming and Scripting How to get front and back parameter of each characters? Post 302974251 by RavinderSingh13 on Friday 27th of May 2016 01:55:38 AM
Old 05-27-2016
Quote:
Originally Posted by paranrat
Hello R.Singh
Sorry for disturbing you again. Could you describe a bit the code above? If I would like to define 3 string before and after of each character. Which part that I need to make adjustment?
Thanks for your help
Hello paranrat,

Following is the explanation for above code as requested by you.
Code:
awk '{num=split($0, A,"");                                                                           ##### I am invoking the split function, which works on split(line/variable, array_name(which we want to create for this variable/line),delimiter of the line or variable). So here I am creating an array named A with respect to $0(complete line) whose delimiter is NULL. as per your Input_file shown eg--> hello, doesn't have any space or any other thing which segregates it, so NULL delimiter I am taking here to take all elements of this into an array named A. Also I am storing the total number of elements or you could say array named A's length into variable num.
      for(i=1;i<=num;i++){                                                                           ##### Starting a for loop from variable i=1 to till the value of variable named num.
                                if(i==1){                                                            ##### Here I am checking for condition if i has value 1, which is for very first character of line where before it NO character will be there so putting "# #" before it in following print statement, as per your requirement mentioned.
                                                print "# #" OFS A[i] OFS A[i+1] OFS A[i+2]        
                                        }
                                else if(i==num){                                                     ##### Now mentioning 2nd condition where variable i's value is equal to num, means last element of the complete line where no more further 2 characters will be there so printing " # #" there too in following print statement.
                                                print A[i-2] OFS A[i-1] OFS A[i] OFS "# #"
                                               }
                                else    {                                                            ##### This condition invokes when above both conditions are FALSE means when i's value is NOT equal to first and last character of the line.
                                                Q=A[i-2] OFS A[i-1] OFS A[i] OFS A[i+1] OFS A[i+2];  ##### Here I am storing values of array A[i-2], A[i-1], A[i], A[i+1] and A[i+2], which is because current array's value will be A[i], so as per your requirement we need 2 elements before it and 2 elements after current value, as mentioned before this condition will be only TRUE when it is NOT very first or very last character so it will put these values into variable named Q.
                                                sub(/^[[:space:]]+/,"# ",Q);                         ##### I am substituting any starting space in variable named Q with a "# " in case any element is not having 2 elements before it.
                                                sub(/[[:space:]]+$/," #",Q);                         ##### Substituting any space at last of variable Q with " #" to complete the pattern as second last element doesn't have 2 elements after it.
                                                print Q                                              ##### printing the variable named Q's value now.
                                        }
                         }
     }
    '   Input_file                                                                                   ##### mentioning the Input_file here.

Also I have created a generic code as follows, which may help you. It should run for any numbers eg--> you want 3 or 4 etc digits before a character in a line, as follows is the one.
Code:
awk -vs1="###" 'BEGIN{v=length(s1)}{$0=s1 $0 s1;num=split($0, A,"");for(i=v+1;i<=num-v;i++){q=i-v;p=i+v;while(q<=p){Q=Q?Q OFS A[q]:A[q];q++};print Q;Q=""}}'  Input_file

Output will be as follows.
Code:
# # # h e l l
# # h e l l o
# h e l l o #
h e l l o # #
e l l o # # #
# # # w o n d
# # w o n d e
# w o n d e r
w o n d e r f
o n d e r f u
n d e r f u l
d e r f u l #
e r f u l # #
r f u l # # #

In case you want to print 4 values before and after each character then you should only change variable in above code -vs1="####"
as follows.
Code:
awk -vs1="####" 'BEGIN{v=length(s1)}{$0=s1 $0 s1;num=split($0, A,"");for(i=v+1;i<=num-v;i++){q=i-v;p=i+v;while(q<=p){Q=Q?Q OFS A[q]:A[q];q++};print Q;Q=""}}' Input_file

Output will be as follows.
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 # # # #
# # # # w o n d e
# # # w o n d e r
# # w o n d e r f
# w o n d e r f u
w o n d e r f u l
o n d e r f u l #
n d e r f u l # #
d e r f u l # # #
e r f u l # # # #

I hope this helps you, please get back to me in case you have any queries. Also please confirm if this is the output you need as I am confuse still about your explanation.

Thanks,
R. Singh

Last edited by RavinderSingh13; 05-27-2016 at 03:06 AM.. Reason: Added a BEGIN statement in code now.
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 05:37 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy