Sponsored Content
Top Forums Shell Programming and Scripting How to get front and back parameter of each characters? Post 302974305 by Don Cragun on Friday 27th of May 2016 01:04:39 PM
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:
 

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:30 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy