Sponsored Content
Full Discussion: awk decrement loop
Top Forums Shell Programming and Scripting awk decrement loop Post 302735567 by Don Cragun on Sunday 25th of November 2012 03:46:13 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
IGAWK(1)							 Utility Commands							  IGAWK(1)

NAME
igawk - gawk with include files SYNOPSIS
igawk [ all gawk options ] -f program-file [ -- ] file ... igawk [ all gawk options ] [ -- ] program-text file ... DESCRIPTION
Igawk is a simple shell script that adds the ability to have ``include files'' to gawk(1). AWK programs for igawk are the same as for gawk, except that, in addition, you may have lines like @include getopt.awk in your program to include the file getopt.awk from either the current directory or one of the other directories in the search path. OPTIONS
See gawk(1) for a full description of the AWK language and the options that gawk supports. EXAMPLES
cat << EOF > test.awk @include getopt.awk BEGIN { while (getopt(ARGC, ARGV, "am:q") != -1) ... } EOF igawk -f test.awk SEE ALSO
gawk(1) Effective AWK Programming, Edition 1.0, published by the Free Software Foundation, 1995. AUTHOR
Arnold Robbins (arnold@skeeve.com). ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +--------------------+-----------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +--------------------+-----------------+ |Availability | SUNWgawk | +--------------------+-----------------+ |Interface Stability | Volatile | +--------------------+-----------------+ NOTES
Source for gawk is available on http://opensolaris.org. Free Software Foundation Nov 3 1999 IGAWK(1)
All times are GMT -4. The time now is 10:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy