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?
# 15  
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:
# 16  
Old 05-27-2016
Hello paranrat,
We obviously have a language barrier that has caused some suggestions being made that do not seem to do what you are trying to do. Your use of the terms character, parameter, front, back, and assign with do not seem to mean the same things that we usually associate with those terms in this forum.

But, except for the example shown in post #14 in this thread (with multiple words on a single input line), it looks like RavinderSingh13's has suggested code that does what you want depending on what version of awk you have available on your system. RavinderSingh13's code depends on the ability to use:
Code:
num=split($0, A,"")

to put each character from an input line into a separate element of the array A[]. This extension works on some versions of awk, but the standards state that the behavior of split() using an empty string as the extended regular expression to be used as the field separator when splitting fields produces unspecified behavior.

The following script allows you to specify the number of leading and trailing characters to be printed along with each character from each of the blank separated words given in the file named file as a command-line operand (defaulting to 2 if no operand is supplied):
Code:
#!/bin/ksh
# Set default number of "#" characters to add to the beginning and end of given
# word to 2 (or if one operand is supplied on the command-line when this script
# is invoked, treat that operand as a number to override this default).
Before_and_After_count=${1:-2}

# Invoke awk to process words read from a file named file with the awk variable
# count set to the value assigned to the shell variable Before_and_After_count.
awk -v count="$Before_and_After_count" '

# The following secion of code is executed once before reading lines from the
# sespecified input file(s).
BEGIN {	# Create a string of "#" characters to be prepended and appended to the
	# words to be processed:
	for(i = 1; i <= count; i++)
		string = string "#"

	# Compute the number of characters to print on each output line (not
	# including the spaces printed between charactes from the extended word.
	print_length = count * 2 + 1
}

# Define a function to loop through the characters in a word given as the input
# parameter to print one output line for each character with count characters
# of context before and after that character.
function print_word(word,	len, spot, text) {
	# Set text to the expanded word with leading and trailing "#"
	# characters.
	text = string word string

	# Set len to the number of characters in the word given as input.
	len = length(word)

	# Create one line of output for each character in the given word with
	# count characters of context before and after the character at the
	# center of this output line.
	for(spot = 1; spot <= len; spot++)
		space_print(substr(text, spot, print_length))
}

# Define a function to print a string from an expanded word with a <space>
# character added to the between between each input character.
function space_print(string,	offset, out) {
	# Set out to the string to be output (with an unwanted leading space).
	for(offset = 1; offset <= print_length; offset++)
		out = out " " substr(string, offset, 1)

	# Print the output skipping the unwanted leading space.
	print substr(out, 2)
}

# The following section of code is processed for each line read from the input
# file(s).
{	# For each blank(s) separated word found in the input, process that word.
	for(i = 1; i <= NF; i++)
		print_word($i)
}
# The following line terminates the awk script and lists the file(s) to be
# processed.
' file

If you save this script in a file named tester and make it executable and the file named file contains the text:
Code:
hello
wonderful
r e a d e r {([0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ])}

then the command ./tester (providing the default 2 characters of context) produces the output:
Code:
# # h e l
# h e l l
h e l l o
e l l o #
l l o # #
# # w o n
# w o n d
w o n d e
o n d e r
n d e r f
d e r f u
e r f u l
r f u l #
f u l # #
# # r # #
# # e # #
# # a # #
# # d # #
# # e # #
# # r # #
# # { ( [
# { ( [ 0
{ ( [ 0 1
( [ 0 1 2
[ 0 1 2 3
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 A
7 8 9 A B
8 9 A B C
9 A B C D
A B C D E
B C D E F
C D E F G
D E F G H
E F G H I
F G H I J
G H I J K
H I J K L
I J K L M
J K L M N
K L M N O
L M N O P
M N O P Q
N O P Q R
O P Q R S
P Q R S T
Q R S T U
R S T U V
S T U V W
T U V W X
U V W X Y
V W X Y Z
W X Y Z ]
X Y Z ] )
Y Z ] ) }
Z ] ) } #
] ) } # #

and the command ./tester 19 (specifying 19 characters of context) produces the output:
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 r f u l # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # w o n d e r f u l # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # w o n d e r f u l # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # w o n d e r f u l # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # w o n d e r f u l # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # w o n d e r f u l # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # w o n d e r f u l # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # w o n d e r f u l # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # w o n d e r f u l # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # r # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # e # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # a # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # d # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # e # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # r # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # { ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G
# # # # # # # # # # # # # # # # # # { ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H
# # # # # # # # # # # # # # # # # { ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I
# # # # # # # # # # # # # # # # { ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J
# # # # # # # # # # # # # # # { ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K
# # # # # # # # # # # # # # { ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L
# # # # # # # # # # # # # { ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M
# # # # # # # # # # # # { ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N
# # # # # # # # # # # { ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O
# # # # # # # # # # { ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P
# # # # # # # # # { ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q
# # # # # # # # { ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R
# # # # # # # { ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S
# # # # # # { ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T
# # # # # { ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U
# # # # { ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V
# # # { ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W
# # { ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X
# { ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y
{ ( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
( [ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ]
[ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ] )
0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ] ) }
1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ] ) } #
2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ] ) } # #
3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ] ) } # # #
4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ] ) } # # # #
5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ] ) } # # # # #
6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ] ) } # # # # # #
7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ] ) } # # # # # # #
8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ] ) } # # # # # # # #
9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ] ) } # # # # # # # # #
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ] ) } # # # # # # # # # #
B C D E F G H I J K L M N O P Q R S T U V W X Y Z ] ) } # # # # # # # # # # #
C D E F G H I J K L M N O P Q R S T U V W X Y Z ] ) } # # # # # # # # # # # #
D E F G H I J K L M N O P Q R S T U V W X Y Z ] ) } # # # # # # # # # # # # #
E F G H I J K L M N O P Q R S T U V W X Y Z ] ) } # # # # # # # # # # # # # #
F G H I J K L M N O P Q R S T U V W X Y Z ] ) } # # # # # # # # # # # # # # #
G H I J K L M N O P Q R S T U V W X Y Z ] ) } # # # # # # # # # # # # # # # #
H I J K L M N O P Q R S T U V W X Y Z ] ) } # # # # # # # # # # # # # # # # #
I J K L M N O P Q R S T U V W X Y Z ] ) } # # # # # # # # # # # # # # # # # #
J K L M N O P Q R S T U V W X Y Z ] ) } # # # # # # # # # # # # # # # # # # #

Although written and tested using a Korn shell on OS X, this will also work with any other shell that processes POSIX-required parameter expansions. However, if you want to run this on a Solaris/SunOS system, you'll need to change awk in this script to /usr/xpg4/bin/awk or nawk.

Does this meet your needs?
This User Gave Thanks to Don Cragun 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