LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Closed Thread
  Search this Thread
Old 05-12-2010, 08:11 PM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Scramble characters in AWK


I see I'm finally posting an AWK question rather than an answer for a change

I wanted to make an AWK script that would scramble all the characters in each field, but leave the first and last characters where they were.

How would I do this?
 
Old 05-12-2010, 09:09 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
...
How would I do this?
By reading the manual.
 
Old 05-12-2010, 09:36 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
And, by the way, it conceptually is very similar to the ways it can be done in Perl - the same entities are involved. So I'm surprised you are asking it - you have already read Perl documentation on regular expressions, haven't you ?
 
Old 05-12-2010, 10:15 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
My only hint would be, think of options for what FS can be
 
Old 05-12-2010, 10:24 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by grail View Post
My only hint would be, think of options for what FS can be
It is definitely a possibility; even in 'awk' "there is more than one way to do it".
 
Old 05-12-2010, 10:25 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Oh, and by the way, starting from the way one would do it with his/her eyes and fingers still applies.
 
Old 05-13-2010, 01:22 AM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
MTK358, if I remember correctly, the thing you want to do with 'awk' is very close to what you once wanted to do with Perl - I mean that OO "C" attempt of yours. I think in that case you meant to replace string middle leaving the beginning and the end intact. This time the beginning and the end are just single characters, but other than that it's the same task, isn't it ?
 
Old 05-13-2010, 08:03 AM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Thanks for emotionally spoiling another project for me.

For some reason this makes me so emotional.

Is there a way to not have the "View Post" button on ignored posts?

And is there a way to have other members write as if no one replied?

The mods claim that it will make no difference here:

http://www.linuxquestions.org/questi...hreads-799110/

but they are WRONG WRONG WRONG!!!

 
Old 05-13-2010, 08:08 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Sergei Steshenko View Post
By reading the manual.
I did read some if it, and I found a nice list of built-in functions.

Even so, it's not like the manual says how do do this.

Oh, I get it.

You just want me to look STUPID.
 
Old 05-13-2010, 08:20 AM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
MTK;
Please count to ten before continuing in this thread. What I see is a terse comment from Sergei, followed by several helpful comments. It would have been much better to respond to the latter.

Regardless, do not post anything else here except a discussion of the substance of your problem and the suggestions so far.

To discuss anything else, send me a PM or e-mail.

thank you
 
Old 05-13-2010, 08:22 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Ok ... well he seems to be offline now, so do you have any questions I can help with?
Did you understand my hint?
 
Old 05-13-2010, 08:23 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
+1 pixellany to just continuing on the thread topic
 
Old 05-13-2010, 08:51 AM   #13
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
For example:

Quote:
Originally Posted by Sergei Steshenko View Post
And, by the way, it conceptually is very similar to the ways it can be done in Perl - the same entities are involved. So I'm surprised you are asking it - you have already read Perl documentation on regular expressions, haven't you ?
I didn't even think that this has anything to do with regular expressions.

I just asked for an algorithm, or some hints on how to make one.

I noticed many times before that he thinks I don't read anything. There is a difference between not reading something and reading something that you have no clue about. It's as if he doesn't understand the concept of not understanding.

It also seems to me that he looks down upon me for being stupid for doing something before I fully understand it. But how else do you learn?

For example this little comment that really hurt me:

Quote:
Originally Posted by Sergei Steshenko View Post
Just marvelous. You start a server and do not know where its log files are ? And how are you going to debug anything at all ?
Let me tell you something, Sergei! I was just doing that for fun.

I am not working in a big corporation setting up a huge corporate server without knowing where log files are.

I am sitting relaxed at home at my desktop, setting up a server just for the sake of learning how servers work, without any real intention of serving a public website, and to have a platform to learn PHP on.

Also, I find your terse language difficult to understand, and to a certain degree I find it condescending.

And one last thing: I just can't stand those cryptic puzzles and weird ways of doing things that so many members disagree on.

You are the polar opposite of LQ's fun, helpful atmosphere.

So if you want to help me, please understand these concepts first:
  • Not understanding
  • Doing stuff for fun
  • Not being so serious
  • Google searches that bring no relavent results
  • Not forcing me to do stuff "your way", which is different for everyone else's way.

If you disagree even slightly, I kindly ask you not to ever reply to my threads again. I would rather not get any replies at all than get a reply from you.

Last edited by MTK358; 05-13-2010 at 08:55 AM.
 
Old 05-13-2010, 08:58 AM   #14
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
MTK;
I asked you to keep this to the substance of your question. Because you ignored my request, you have been given a 1-day "time out".

Please contact me or any moderator privately to discuss further.
 
Old 05-13-2010, 12:18 PM   #15
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Not a direct answer to your question, but an alternative:

While it's quite easy in gawk, I wanted to see if I could do it using bash. Here's how I did it:
Code:
#!/bin/bash
# Scramble all characters between the first an last character in each input token
#
scramble ()
{
  declare -a inside
  local first last i
  if [ ${#1} -le 3 ]
  then
#   If here, nothing to do
    scramble="${1}"
  else
#   We have something to do . . .
    first="${1:0:1}"
    last="${1:$((-1)):1}"
    i=1
    while [[ "${i}" -lt $((${#1}-1)) ]]
    do
      inside[${i}]=${1:$((i)):1}
      i=$((${i}+1))
    done
    scramble="${first}"
    for i in $(echo -n $(shuf -e "${inside[@]}"))
    do
      scramble="${scramble}"${i}
    done
    scramble="${scramble}""${last}"
  fi
}
for ok in "${@}"
do
  scramble "${ok}"
  echo -n "${scramble}"" "
done
echo
and here's how the output looks:
Code:
$ ./scramble this is a test
tihs is a test 
$ ./scramble this is a test
this is a test 
$ ./scramble this is a test
this is a test
$ ./scramble this is a test
tihs is a tset
Note the use of the shuf function to do the actual permutation. While a permutation function is fairly easy to write, I see no need to "reinvent the wheel."
 
  


Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting word scramble assistant need help with one aspect xskycamefalling Programming 3 05-10-2009 11:09 AM
dvd playback has scramble video sharky Ubuntu 3 12-29-2008 08:42 AM
MySQL head scramble jabfinger Programming 4 07-28-2005 12:29 PM
can any one give a hand on CSS problem(Content Scramble System) ypx Linux - Software 0 12-04-2004 04:10 AM
can any one give a hand on CSS problem(Content Scramble System) ypx Linux - Software 0 12-03-2004 08:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration