Sponsored Content
Top Forums Shell Programming and Scripting String manipulation using awk Post 302880340 by Don Cragun on Thursday 19th of December 2013 02:19:11 AM
Old 12-19-2013
You could also try something like:
Code:
printf "%s\n" 512m512m 1024g1024g 2048G2048G 512M1024G 123a456b789c 987Kb654Kb321Kb | awk '
{       printf("\tInput string %d: %s\n", NR, $0)
        nnum = split($0, num, /[^[:digit:]]*/)
        for(i = 1; i < nnum; i++)
                printf("a[%d]=%s\n", i - 1, num[i])
        nfs = split($0, fs, /[[:digit:]]*/)
        printf("fieldsep=%s", fs[2])
        pa = 0
        for(i = 3; i <= nfs; i++)
                if(fs[i] != fs[2]) pa = 1
        if(pa)
                for(i = 3; i <= nfs; i++)
                        printf(",%s", fs[i])
        printf("\n")
}'

which produces:
Code:
	Input string 1: 512m512m
a[0]=512
a[1]=512
fieldsep=m
	Input string 2: 1024g1024g
a[0]=1024
a[1]=1024
fieldsep=g
	Input string 3: 2048G2048G
a[0]=2048
a[1]=2048
fieldsep=G
	Input string 4: 512M1024G
a[0]=512
a[1]=1024
fieldsep=M,G
	Input string 5: 123a456b789c
a[0]=123
a[1]=456
a[2]=789
fieldsep=a,b,c
	Input string 6: 987Kb654Kb321Kb
a[0]=987
a[1]=654
a[2]=321
fieldsep=Kb

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk string manipulation

Here is my awk code in a shell script: localRecDir=/somedirectory/ # awk -v LRD="$localRecDir" '{out = sprintf ("%s0000000%s"),LRD,substr($3,4) ; print > out; close(out)}' *.log Here is the contents of my *.log file I am trying to parse with my script: 000007 0110 07-0001583 000007 ... (2 Replies)
Discussion started by: zoo591
2 Replies

2. UNIX for Dummies Questions & Answers

String manipulation

Hi all, Please help me decide this problem. I have a converter application getting its parameters by specified format. Ie: -date2007-02-14 Also I have a files named yyyyMMdd.rar format. So how do I convert the file name to it. Input: 20070214.rar Output: -date2007-02-14 I wrote the... (2 Replies)
Discussion started by: mr_bold
2 Replies

3. Shell Programming and Scripting

String manipulation

Hi There! I have the following block of text in my input file and in order to parse it correctly, i need to have i.e. If a line starts with a number, ignore it else replace it with the number from the previous line until the first ',' So, for example, 15,9:5/12345 ,10:1 ... (5 Replies)
Discussion started by: orno
5 Replies

4. Shell Programming and Scripting

I need help with string manipulation

First of all I am VERY new to this so bare with me and try and explain everything even if it seems simple. Basically I want to read a line of text from a html file. See if the line of text has a certain string in it. copy an unknown number of characters (the last 4 characters wiil be ".jpg" the... (1 Reply)
Discussion started by: c3lica
1 Replies

5. Shell Programming and Scripting

String Manipulation

How u convert string "hi pravin how are you?" to "Hi Pravin How Are You?" (4 Replies)
Discussion started by: proactiveaditya
4 Replies

6. Shell Programming and Scripting

String manipulation

Hi, I have the string like this ". Start : 06:53:11 - MON JUL 05, 2010" I need to print the part "06:53:11 - MON JUL 05, 2010" How i can do this? Thanks, Lenova (2 Replies)
Discussion started by: lenova2010
2 Replies

7. Shell Programming and Scripting

Deleting part of a string : string manipulation

i have something like this... echo "teCertificateId" | awk -F'Id' '{ print $1 }' | awk -F'te' '{ print $2 }' Certifica the awk should remove 'te' only if it is present at the start of the string.. anywhere else it should ignore it. expected output is Certificate (7 Replies)
Discussion started by: vivek d r
7 Replies

8. Shell Programming and Scripting

Awk to convert a text file to CSV file with some string manipulation

Hi , I have a simple text file with contents as below: 12345678900 971,76 4234560890 22345678900 5971,72 5234560990 32345678900 71,12 6234560190 the new csv-file should be like: Column1;Column2;Column3;Column4;Column5 123456;78900;971,76;423456;0890... (9 Replies)
Discussion started by: FreddyDaKing
9 Replies

9. Shell Programming and Scripting

String manipulation

Hi , I am getting a string like aaa,bbb,sdsdad,sdfsdf,sdfsdfdsf,rtyrtyr,45654654,ddfdfdfgdfg,dfgdfgdg........... Now what I need is to format it. So after each nth comma I need one newline. So the above will look like when n=3 aaa,bbb,sdsdad, sdfsdf,sdfsdfdsf,rtyrtyr,... (4 Replies)
Discussion started by: Anupam_Halder
4 Replies

10. Shell Programming and Scripting

String Manipulation

I'm making a little game in Perl, and I am trying to remove the first instance of a character in an arbitrary string. For example, if the string is "cupcakes"and the user enters another string that contains letters from "cupcake" e.g: "sake"the original string will now look like this (below)... (3 Replies)
Discussion started by: whyte_rhyno
3 Replies
FMTCHECK(3)						   BSD Library Functions Manual 					       FMTCHECK(3)

NAME
fmtcheck -- sanitizes user-supplied printf(3)-style format string LIBRARY
Utility functions from BSD systems (libbsd, -lbsd) SYNOPSIS
#include <bsd/stdio.h> const char * fmtcheck(const char *fmt_suspect, const char *fmt_default); DESCRIPTION
The fmtcheck() scans fmt_suspect and fmt_default to determine if fmt_suspect will consume the same argument types as fmt_default and to ensure that fmt_suspect is a valid format string. The printf(3) family of functions cannot verify the types of arguments that they are passed at run-time. In some cases, like catgets(3), it is useful or necessary to use a user-supplied format string with no guarantee that the format string matches the specified arguments. The fmtcheck() was designed to be used in these cases, as in: printf(fmtcheck(user_format, standard_format), arg1, arg2); In the check, field widths, fillers, precisions, etc. are ignored (unless the field width or precision is an asterisk '*' instead of a digit string). Also, any text other than the format specifiers is completely ignored. RETURN VALUES
If fmt_suspect is a valid format and consumes the same argument types as fmt_default, then the fmtcheck() will return fmt_suspect. Other- wise, it will return fmt_default. SECURITY CONSIDERATIONS
Note that the formats may be quite different as long as they accept the same arguments. For example, "%p %o %30s %#llx %-10.*e %n" is com- patible with "This number %lu %d%% and string %s has %qd numbers and %.*g floats (%n)". However, "%o" is not equivalent to "%lx" because the first requires an integer and the second requires a long. SEE ALSO
printf(3) BUGS
The fmtcheck() function does not understand all of the conversions that printf(3) does. BSD
October 16, 2002 BSD
All times are GMT -4. The time now is 08:02 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy