doing rot13 on a field with a random number with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting doing rot13 on a field with a random number with awk
# 1  
Old 09-23-2008
doing rot13 on a field with a random number with awk

i have a several million line file like this:

M:charity[cross]general:water:fairbanks:charity[cross]general

field 2 and field 5 are the same

i want to read the file and rot13 or any caesar cipher field 2 and replace the [ ] with a random number 1 - 9

anyone know how to do this?

something slightly more sophisticated than a caesar cipher/rotate that is reversible and could be done with awk would be great too
# 2  
Old 09-23-2008
do you really have to do it in awk? If you have Python, here's an alternative
Code:
import random
randnum1 = random.sample(range(10),1)[0] #generate random num 1 to replace at field2
randnum2 = random.sample(range(10),1)[0] #generate random num 1 to replace at field5
for line in open("file"):
    line=line.strip().split(":") #split line into field using ":" as delimiter
    line[1]=line[1].encode('rot13') #rot13 field 2
    line[1] = line[1].replace("[",str(randnum1)).replace("]",str(randnum1))
    line[4] = line[4].replace("[",str(randnum2)).replace("]",str(randnum2))
    print ":".join(line)

# 3  
Old 09-23-2008
awk doesn't have the equivalent of a tr() function so it's messy to do rot13. http://www.miranda.org/~jkominek/rot13/awk/rot13.awk is the top google hit, and looks like a reasonable implementation.
# 4  
Old 09-23-2008
ghostdog i dont know any python whatsoever so my first response is that im not comfortable with that but after looking at what you just did im sincerely tempted to learn python now.. i just spent the last 15 minutes researching books on it

thanks era but i found that already i just dont know how to tie that rot13 implementation into the 2nd field of the file in a way that will allow me to also do other manipulations on the other fields
# 5  
Old 09-23-2008
Quote:
Originally Posted by bathtub
i just spent the last 15 minutes researching books on it
go to the Python doc site and take the tutorial
# 6  
Old 09-23-2008
Quote:
Originally Posted by ghostdog74
go to the Python doc site and take the tutorial
way ahead of ya Smilie

wonder how long it would take someone who only knows bash scripting and unix to learn it
# 7  
Old 09-23-2008
Quote:
Originally Posted by bathtub
way ahead of ya Smilie

wonder how long it would take someone who only knows bash scripting and unix to learn it
you can learn the basics in a day
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command to return only field with a number in it

What is an awk command to print only fields with a number in it?? Input file....... S,S,S,S,S,S,S,S,S 001S,S,S,S,S,S,S,S,S 00219S,23S,24S,43S,47S,S,S,S,S 00319S,10S,23S,41S,43S,47S,S,S,S 00423S,41S,43S,46S,47S,S,S,S,S 00510S,23S,24S,43S,46S,S,S,S,S 00610S,23S,43S,46S,47S,S,S,S,S... (2 Replies)
Discussion started by: garethsays
2 Replies

2. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

3. Programming

random number

How can I choose randomly the row numbers of my file in awk? (4 Replies)
Discussion started by: Homa
4 Replies

4. Shell Programming and Scripting

awk assign output of array to specific field-number

With this script i want to print the output to a specific field-number . Can anybody help? awk 'NR=FNR{split(FILENAME,fn,"_");nr=$2;f = $1} END{for (i=1;i<=f;i++) print i,$fn=nr}' input_5.csv input_6.csvinput_5.csv 4 135 5 185 6 85 11 30input_6.csv 1 90 3 58 4 135 7 60 8 55 10... (1 Reply)
Discussion started by: sdf
1 Replies

5. Shell Programming and Scripting

awk conditional expression to compare field number and variable value

Hi, I'm trying to compare the value in a field to the value in a variable using awk. This works: awk '$7 == "101"'but this is what I want (and it doesn't work): value=101 awk '$7 == "$value"' Any help or insight on this would be great. Thanks in advance. (1 Reply)
Discussion started by: goodbenito
1 Replies

6. Shell Programming and Scripting

Sorting on two fields time field and number field

Hi, I have a file that has data in it that says 00:01:48.233 1212 00:01:56.233 345 00:09:01.221 5678 00:12:23.321 93444 The file has more line than this but i just wanted to put in a snippet to ask how I would get the highest number with time stamp into another file. So from the above... (2 Replies)
Discussion started by: pat4519
2 Replies

7. Shell Programming and Scripting

awk to print exact field number

Hello there. I want some help with awk I have this script that reads a file from awk and you can insert from keyboard any list from the fields that exist so to be printed on monitor echo "give a choice" read -a ans array=${ans} awk -F: -v k="$array" '{ ... (4 Replies)
Discussion started by: covis
4 Replies

8. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

9. Shell Programming and Scripting

use awk to aggregrate the field number

Hi, I have a various files;each filled with hundreds of line with similar number of fields. I would like to extract out field $5 from each of this file and aggregate them before printing out to a file. I tried to :- #!/usr/bin/awk -f file="file1.txt file2.txt file3.txt file4.txt" ... (1 Reply)
Discussion started by: ahjiefreak
1 Replies

10. Shell Programming and Scripting

[awk]: Row begins by random number and field 10 is greater than 10.00%

Hello! I wish to extract the pid where CPU is above 10% last pid: 22621; load averages: 4.71, 5.04, 5.13 15:08:34 221 processes: 212 sleeping, 2 running, 1 stopped, 6 on cpu CPU states: %... (3 Replies)
Discussion started by: Lomic
3 Replies
Login or Register to Ask a Question