Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This is the conversion table from numbers to letters.
I am only working on blocks of 2 numbers.
If a block of 2 numbers is not in the table then it returns "X". The same when we start at the last position and thus only one digit makes a block.
After inserting the convert function and a for loop to start at different position, it seems to work. Thanks konsolebox !
* However, printing only converted string > 2 characters in length was just an example.
For real, I have numerous steps to go through once I converted the numbers to letters.
Would it mean that all these numerous steps have to be written inside the function "print_set" ?
My original query was to be able, once I converted the numbers to letters, to start again from the converted "intermediate.txt" as an input and to process it the same way as I would have process it with a command line.
This way I would have be able to define RS=ORS="\n", FS=OFS="|", and to use the variable $1 and $2 instead of "x" and "$0" in your code. Which would have been easier after I think.
* In the function "truncate" I don't really get the expression of the system function:
Would it mean that all these numerous steps have to be written inside the function "print_set" ?
Not necessary. Originally it was two sets of steps inside for loops in the main block. I just placed it there to make it more readable.
Quote:
My original query was to be able, once I converted the numbers to letters, to start again from the converted "intermediate.txt" as an input and to process it the same way as I would have process it with a command line.
This way I would have be able to define RS=ORS="\n", FS=OFS="|", and to use the variable $1 and $2 instead of "x" and "$0" in your code. Which would have been easier after I think.
I'm really confused about that method (using RS, FS, etc.). Perhaps I just don't want to understand it since I see a better way to do it. Is there something you want to do more about the results? Please describe it in a pseudo-like manner, not by how you plan to do it.
Quote:
* In the function "truncate" I don't really get the expression of the system function:
Code:
": > '" file "' >/dev/null 2>&1"
I assume he "0" exit status means success.
It's just a shell command emulated within awk using the system function. Or perhaps awk calls a subshell for that. Yes it means success.
Is there something you want to do more about the results?
Yes there are about 100 other operations (some are a bit complex) to do after printing the line containing $2 > 2 characters long.
That's why "keeping $2 > 2 letters" was just an example to know how to make the transition between the first part of the script that uses the original input file to convert numbers to letters (redirected to "intermediate.txt"), and the use of "getline < file" to use "intermediate.txt" as a new input file (with RS="\n", FS="|") ready to be processed through numerous steps.
If that's the case you could just use convert $0 for every function.
Code:
function do_something_1() {
<do something with $0>
$0 = result
return 1
}
function do_something_2() {
<do something with $0>
$0 = result
return 1
}
{
if (!do_something_1()) {
next
}
if (!do_something_2()) {
next
}
print $0
}
Last edited by konsolebox; 09-26-2012 at 12:32 AM.
Thanks konsolebox but I'm just a bit lost with your 2 last posts.
I don't manage to apply them on my code. If you could show me with one function (not everything, let me some...)
Code:
BEGIN{
RS="@"; FS=OFS="|"; conv["12"]="a"; conv["23"]="b"; conv["34"]="c"; conv["45"]="d"; conv["56"]="e"; conv["67"]="f"; conv["78"]="g";
conv["87"]="h"; conv["76"]="i"; conv["65"]="j"; conv["54"]="k"; conv["43"]="l"; conv["32"]="m"; conv["21"}="n"
}
function convert(field, start){
letter = ""
block = substr (field, start, 2)
while (block != ""){
letter = letter (block in conv ? conv[block] : "x")
start = start + 2
block = substr (field, start, 2)
}
return letter
}
function rev(field){
rever = ""
l = length(field)
for (i=l; 0<i; i--){
rever = rever substr (field, i, 1)
}
return rever
}
NR==1{next}
NR>1{
sub("\n", "|") # write second line next to the preceding one
gsub("\n", "")
}
{
for(i=1; i<=(length($3); i++){
print $1 FS convert($3, i) > "intermediate.txt" # step 1) and output in a file (we removed $2)
}
for(i=1; i<=(lentgh($3); i++){
print $1 FS convert(rev($3), i) >> "intermediate.txt" # step 2) (we removed $2) and 3) concatenate in the same file
}
}
##### BLOCK BELOW DOESN'T WORK ######
{
close("intermediate.txt");
RS=ORS="\n"; FS=OFS="|"; # re-define RS, FS to be able to use "intermediate.txt" as if it was the input of a second command-line
while((getline < "intermediate.txt") > 0){
if(length($2) > 2) {print $0} # note that previous $3 in original input becomes $2 in "intermediate.txt"
else{next}
... <keep processing "intermediate.txt">
}
konsolebox: Even I am not sure I understood your previous post, I tried to modify the functions and to simplify a bit more, to fuse the 2 functions "convert" and "reverse" together and to incorporate the increment to start at different positions, into a single function.
I tried:
Code:
function total(n, start){
letter = ""
for (first=1; first<= length(n); first++){
ss = substr (n, first)
block = substr (ss, start, 2)
while (block != ""){
letter = letter (block in conv ? conv[block] : "X")
start = start + 2
block = substr (ss, start, 2)
}
$2 = letter
}
letter_reverse = ""
for (i = length(n); i=1; i--){
n_reverse = n_reverse substr (n, i, 1)
for (first=1; first<= length(n); first++){
ss_reverse = substr (n_reverse, first)
block_reverse = substr (ss_reverse, start, 2)
while (block_reverse != ""){
letter_reverse = letter_reverse (block_reverse in conv ? conv[block_reverse] : "X")
start = start + 2
block_reverse = substr (ss_reverse, start, 2)
}
$2 = letter_reverse
}
}
return 1
}
but it doesn't work at all...
Is it possible in a same function to use one line (here "n") to produce 2 different results (here "letter" and "letter_reverse")?
How can I use the same variable (here "$2") to define these 2 distinct values????
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.