awk decrement loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk decrement loop
# 1  
Old 11-25-2012
Hammer & Screwdriver awk decrement loop

Hi all,

could you please help me - a complete newbie - with the following task. I have such data:

12345678

and i need to split it into all possible sequences of 4 chars, like this:

1234_5678

and then again:

1_2345_678
12_3456_78
123_4567_8

I can do this in GAWK

Code:
#c is the sequence length

BEGIN{FS=OFS="";o=" ";c=4;n="_";}

{ gsub( " ", ""); gsub( /./, "& ");

{  for (i = 0; ++i <=NF;) 
            if ($i == o)
            ++D % c || $i = n
} 1

Can i have another loop for 'c' inside this loop - to decrement it: --d till d=1?

Do you think AWK is OK for that task? I really want to have something native without the need to incorporate the code inside a shell script.

Thank you!
# 2  
Old 11-25-2012
Quote:
Originally Posted by shivacoder
Can i have another loop for 'c' inside this loop - to decrement it: --d till d=1?
Of course. Probably there are some limits to nesting loops, but i have to encounter them yet.

Quote:
Do you think AWK is OK for that task?
Absolutely. You could do the same in other languages too (perl, shell, python, ruby, ...), but it probably would gain you nothing. If you feel comfortable with awk (and it seems that you do) then, by all means, go with it!

I hope this helps.

bakunin
# 3  
Old 11-25-2012
Note that according to the standards, setting FS to an empty string produces undefined results. (Doing so treats each character as a field in gawk and some other version of awk, but generates various errors or unexpected results on other versions of awk.) If I understand what shivacoder is trying to do, I think the following is a portable way to do what was requested:
Code:
awk -v c=4 '{
        for(i = c; i >= 1; i--) {
                # Print a header preceded by a <newline> if this is not the 1st
                # output line.
                printf("%sProcessing %d character strings from \"%s\".\n",
                        i != c || NR > 1 ? "\n" : "", i, $0)
                if(i > length($0)) {
                        printf("There aren'\''t %d characters in \"%s\".\n",
                                i, $0) 
                        continue
                }
                # Print the 1st line.
                printf("%s%s%s\n", substr($0, 1, i),
                        i == length($0) ? "" : "_", substr($0, i + 1))
                if(length($0) >= i)
                        for(j = 1; j + i < length($0); j++)
                                printf("%s_%s_%s\n", substr($0, 1, j),
                                        substr($0, j + 1, i),
                                        substr($0, j + i + 1))
                # Do not print the last line if it would duplicate the 1st line.
                if(i != length($0) / 2 && length($0) > i)
                        printf("%s_%s\n", substr($0, 1, length($0) - i),
                                substr($0, length($0) - i + 1))
        }
}' input


Last edited by Don Cragun; 11-26-2012 at 01:18 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk for loop help

Hello, I have an input file that looks like so: 1 2 3 4 5 6 7 8 9 and I just want to print the first and third column (note: my actual file contains many many more fields so I don't want to use '{ print $NF }' for each field I want. I tried using: awk 'BEGIN {FS=" "} { for (i=1;... (13 Replies)
Discussion started by: Rabu
13 Replies

2. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

3. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

4. Shell Programming and Scripting

awk loop and using shell in awk

Hi, everyone! I have a file, when I print its $1 out it show several strings like this: AABBCC AEFJKLFG FALEF FAIWEHF What I want to do is that, after output of each record, search the string in all files in the same folder, print out the record and file name. This is what I want... (4 Replies)
Discussion started by: xshang
4 Replies

5. Shell Programming and Scripting

Decrement one from 3rd Column

Hi, I need a script that will subtract 1 from the third column of the line beginning with %, leaving all other values the same. So 158 should be 157, 308 should be 307, 458 should be 457. Before: # 30109 xyz abc Data % 30109 158 5 8 2 000023f 01f4145 # 30109 ... (3 Replies)
Discussion started by: morrbie
3 Replies

6. Shell Programming and Scripting

Decrement using bash!!

Hi all, Thanks in Advance! I want a simple script to print today and yesterdays date. using this command date +%d%m%Y i can able get today's date but i want yesterday's date with the same format. so i tried using simple decrement operator but... (2 Replies)
Discussion started by: anishkumarv
2 Replies

7. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

8. Shell Programming and Scripting

decrement a four part number in shell script

I have a four part number eg: 1.21.1.3 I need to find a way in shell script to decrement this by one and put in a loop so the values printed will be 1.21.1.2 1.21.1.1 1.21.1.0 Which is the best way to do this in shell script?? (7 Replies)
Discussion started by: codeman007
7 Replies

9. Shell Programming and Scripting

Can we increment or decrement a date value?

export a=`date` a=`expr $a + 1` Is it possible? if not how can i increment or decrement a date variable? (2 Replies)
Discussion started by: arghya_owen
2 Replies

10. Shell Programming and Scripting

While-loop with awk

Hi, I have recently posted in another thread started by me :D. But in an effort to make my script more beautiful I've been thinking abbout while loops. I run my script with the command: sh script 4 numbers.txt And my script is like this: data=`cat $2 | xargs -n $1` #echo $data ... (13 Replies)
Discussion started by: baghera
13 Replies
Login or Register to Ask a Question