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?
# 8  
Old 05-26-2016
Quote:
Originally Posted by RudiC
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

Hi RudiC,

I already tested but only first line of word is working.

File tested:
Code:
hello
wonderful

Output:
Code:
# # h e l
# h e l l
h e l l o
e l l o #
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
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 # # # # #

Thanks RudiC Smilie
# 9  
Old 05-26-2016
Hello paranrat,

For my suggestion, if you are reading an Input_file then you could try following where I have changed only delimiter in split as follows. Let's say following is Input_file:
Code:
hello
wonderful

Then following is the code for same.
Code:
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}}}'  Input_file

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

EDIT: Adding a non-one liner form of solution now.
Code:
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
                                        }
                         }
     }
    '   Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 05-26-2016 at 04:46 AM.. Reason: Added a non-one liner form of solution now.
This User Gave Thanks to RavinderSingh13 For This Post:
# 10  
Old 05-26-2016
I would follow a completely different approach:

First, pad your word to the left and right with two '#' characters. Then loop through the word, starting from the first position, and display the respective substring. Here an example implementation in Zsh:

Code:
word=hello # Original word
xword="##$word##" # Word padded 
for (( i = 1; i < ${#xword}-3; i++)) 
do
   echo $xword[$i,$((i + ${#word} - 1))]
done

This User Gave Thanks to rovf For This Post:
# 11  
Old 05-26-2016
Quote:
Originally Posted by paranrat
.
.
.
I already tested but only first line of word is working.
.
.
.
According to your (extremely difficult to understand, if at all) specification, it IS working, at least for my setup:
Code:
# # h e l
# h e l l
h e l l o
e l l o #
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 # # # #

Please check your input! I'm utmost inclined to bet it's NOT what you posted!
# 12  
Old 05-26-2016
Hello paranrat,

Also apart from what RudiC has asked you to check about your Input_file. I would like to request you to please elaborate the conditions more as how you will decide the number of # to be printed in case Input_file could be following.
Code:
cat Input_file
hello
wonderful
xyz
a
beautifully
whoami
IamRavinderSingh

Then what should be the output for above Input_file, if you could put some more light here please.

Thanks,
R. Singh
# 13  
Old 05-27-2016
Quote:
Originally Posted by RavinderSingh13
Hello paranrat,

Also apart from what RudiC has asked you to check about your Input_file. I would like to request you to please elaborate the conditions more as how you will decide the number of # to be printed in case Input_file could be following.
Code:
cat Input_file
hello
wonderful
xyz
a
beautifully
whoami
IamRavinderSingh

Then what should be the output for above Input_file, if you could put some more light here please.

Thanks,
R. Singh
Hello, R.Singh

Basically what I would like to do is getting 2 strings before and after of each character. Starting from first character until final character.
"#" assign is for replacing null string.
Code:
For example "r e a d e r". 
first character is "r"; string before "r" is null thus null string will be replace by "#" and after "r" are e and a.
second character is "e"; string before "e" is r and after "e" are a and d.
third character is "a"; string before "a" are r and e, after "a" are d and e.
forth character is "d"; string before "d" are e and d, after "d" are e and r.
fifth character is "e"; string before "e" are a and d, after "e" is r
final character is "r"; string before "r" are d and e, after "r" is null thus assign with #

Code:
Simply explanation:
x x r x x
x x e x x
x x a x x
x x d x x
x x e x x
x x r x x

Thanks Smilie

---------- Post updated at 09:21 PM ---------- Previous update was at 09:16 PM ----------

Quote:
Originally Posted by RudiC
According to your (extremely difficult to understand, if at all) specification, it IS working, at least for my setup:
Code:
# # h e l
# h e l l
h e l l o
e l l o #
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 # # # #

Please check your input! I'm utmost inclined to bet it's NOT what you posted!
Hello RubiC

Thanks for your advising and sorry if my post make you disappointed.


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

Thanks Smilie and sorry again Smilie

---------- Post updated at 10:53 PM ---------- Previous update was at 09:21 PM ----------

Quote:
Originally Posted by RavinderSingh13
Code:
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
                                        }
                         }
     }
    '   Input_file

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
# 14  
Old 05-27-2016
How about this?

Code:
awk '
function fill(c,n,r) { for(i=1;i<n;i++) r=r c; return r }
{
 X = fill("#", length/2) $0 fill("#", 1+length/2);
 for(i=1; i<=length; i++)
    print substr(X, i, length);
}' infile

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