String manipulation using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String manipulation using awk
# 1  
Old 12-18-2013
String manipulation using awk

I have the following string

512m512m

I'm trying to split the string using awk

Code:
awk '{ split(512m512m,a,"m") print $a[0]; $a[1] }'

Sometimes the string could be

1024g1024g or 2048G2048G or 512M1024G

how can i change the fieldsep to be a alphabet irrespective of case, and also display my fieldseperator


If my input string is
1024g1024g
My output should be
a[0]=1024
a[1]=1024
fieldsep=g

How to get this using awk.
# 2  
Old 12-18-2013
Hi,
example:
Code:
$ echo '512m513m' | awk  -F'm' '{printf "$1=%s\n$2=%s\n",$1,$2}'
$1=512
$2=513

Code:
$ echo '512m513m' | awk  'BEGIN{FS="m"};{printf "$1=%s\n$2=%s\n%s\n",$1,$2,FS}'
$1=512
$2=513
m

Regards.
# 3  
Old 12-18-2013
awk is not ideal for this task.
I hope your awk script will do other things, too.
Your sample has several errors. Correction:
Code:
awk '{ split("512m512m",a,"[kmgt]"); print a[1],a[2] }'

I have only ugly complex solutions to also display the field separator...

---------- Post updated at 05:41 PM ---------- Previous update was at 05:12 PM ----------

An elegant solution with perl:
Code:
perl -le '"512m512m"=~/(\d+)([kmgt])(\d+)/; print $1; print $3; print $2;'

---------- Post updated at 06:07 PM ---------- Previous update was at 05:41 PM ----------

Finally a halfway elegant solution with awk:
Code:
awk '{ x="512m512m"; n=substr(x,match(x,/[kmgt]/),1); split(x,A,n); print A[1]; print A[2]; print n }'

# 4  
Old 12-18-2013
How about this unit (separator) follows value in array :

Code:
$ awk 'BEGIN { x="512M1024G"; gsub(/[kKmMgGtT]/,";&;",x); split(x,A,";");print A[1],A[2]; print A[3],A[4]}'
512 M
1024 G

# 5  
Old 12-18-2013
Or
Code:
$ echo "512M1024G" | awk '{gsub(/([^0-9])/," & "); while(NF>i) print $(++i),$(++i)}'
512 M
1024 G

Regards.
# 6  
Old 12-18-2013
some more example


Code:
# Single char 
$ echo "1024m1025g" | awk -F'[[:alpha:]]' '{print $1,$2}'
1024 1025

$ echo "1024g1025g" | awk -F'[[:alpha:]]' '{split($0,C);print C[1],C[2]}'
1024 1025

# Multiple char
$ echo "1024gaas1025g" | awk -F'[[:alpha:]]*' '{print $1,$2}'
1024 1025

$ echo "1024gaAas1025g" | awk -F'[[:alpha:]]*' '{split($0,C);print C[1],C[2]}'
1024 1025

# 7  
Old 12-19-2013
Code:
 echo "512m512m" |  awk '{ split($0,a,/[[:alpha:]]/);print a[1],a[2]}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question