awk split numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk split numbers
# 1  
Old 08-17-2012
awk split numbers

I would like to split a string of numbers "1-2,4-13,16,19-20,21-25,31-32" and output these with awk into

Code:
-dFirstPage=1 -dLastPage=2 file.pdf -dFirstPage=4 -dLastPage=13 file.pdf -dFirstPage=16 -dLastPage=16 file.pdf file.pdf -dFirstPage=19 -dLastPage=20 file.pdf -dFirstPage=21 -dLastPage=25 file.pdf -dFirstPage=31 -dLastPage=32 file.pdf

When i use the script below it unsorts it into
Code:
-dFirstPage=19 -dLastPage=20 file.pdf -dFirstPage=21 -dLastPage=25 file.pdf -dFirstPage=31 -dLastPage=32 file.pdf -dFirstPage=1 -dLastPage=2 file.pdf -dFirstPage=4 -dLastPage=13 file.pdf -dFirstPage=16 -dLastPage=16 file.pdf

Code:
awk -v p="1-2,4-13,16,19-20,21-25,31-32" -v dn="file.pdf" 'BEGIN{ORS=" "; split(p,t,",");for (i in t) if(t[i] ~ /-/) {split(t[i],t1,"-"); print  "-dFirstPage=" t1[1] ,"-dLastPage=" t1[2], dn } else {print "-dFirstPage=" t[i] ,"-dLastPage=" t[i], dn }}'

What ist wrong with the script?
# 2  
Old 08-17-2012
Hi

Code:
awk -v p="1-2,4-13,16,19-20,21-25,31-32" -v dn="file.pdf" 'BEGIN{ORS=" "; x=split(p,t,",");for (i=1;i<=x;i++) if(t[i] ~ /-/) {split(t[i],t1,"-"); print  "-dFirstPage=" t1[1] ,"-dLastPage=" t1[2], dn } else {print "-dFirstPage=" t[i] ,"-dLastPage=" t[i], dn }}'

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 08-17-2012
impeccable on my linux mawk 1.3.3!
# 4  
Old 08-17-2012
here is corrected one
Code:
awk -v p="1-2,4-13,16,19-20,21-25,31-32" -v dn="file.pdf" 'BEGIN{ORS=" ";n=split(p,t,",");for (i=1;i<=n;i++) if(t[i] ~ /-/) {split(t[i],t1,"-"); print  "-dFirstPage=" t1[1] ,"-dLastPage=" t1[2], dn } else {print "-dFirstPage=" t[i] ,"-dLastPage=" t[i], dn }}'

This User Gave Thanks to raj_saini20 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk split and awk calculation in the same command

I am trying to run the awk below. My question is when I split the input, then run anotherawk to perform a calculation using that splitas the input there are no issues. When I try to combine them the output is not correct, is the split not working or did I do it wrong? Thank you :). input ... (8 Replies)
Discussion started by: cmccabe
8 Replies

2. Shell Programming and Scripting

awk split help

Hello, I have the following input file: A=1;B=2;C=3;D=4 A=4;B=6;C=7;D=9 I wish to have the following output 1 2 3 4 4 6 7 9 Can awk split be used to do this? I have done this without using split, but the process is quite tedious. Any help is appreciated! (4 Replies)
Discussion started by: Rabu
4 Replies

3. Shell Programming and Scripting

Split files with formatted numbers

How to split the file and have suffix with formatted numbers Tried the following code awk '{filename="split."int((NR-1)/2)".txt"; print >> filename}' split.txt Current Result Expected Result (21 Replies)
Discussion started by: bobbygsk
21 Replies

4. Shell Programming and Scripting

Split a file into multiple files based on line numbers and first column value

Hi All I have one query,say i have a requirement like the below code should be move to diffent files whose maximum lines can be of 10 lines.Say in the below example,it consist of 14 lines. This should be moved logically using the data in the fisrt coloumn to file1 and file 2.The data of first... (2 Replies)
Discussion started by: sarav.shan
2 Replies

5. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

6. UNIX for Dummies Questions & Answers

awk split

Hi Folks, I have lines that look like this: >m110730_101608_00120_c100168052554400000315046108261127_s1_p0/7/29_426ACGTGCTATGCGG >m110730_101608_00120_c100168052554400000315046108261127_s1_p0/7/469_894ACGTGCTATGCGG I want to split all lines into: ... (4 Replies)
Discussion started by: heecha
4 Replies

7. UNIX for Dummies Questions & Answers

awk split

Can anybody tell me what is wrong with this ? It does not produce anyoutput. awk 'split( "this:that", arr,":")' (2 Replies)
Discussion started by: jville
2 Replies

8. UNIX for Dummies Questions & Answers

Split Function Prefix Numbers

Hello, Hello, I use the following command to split a file: split -Number_of_Lines Input_File MyPrefix_ output is MyPrefix_a MyPrefix_b MyPrefix_c ...... Instead, how can I get numerical values like: MyPrefix_1 MyPrefix_2 MyPrefix_3 ...... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

9. Shell Programming and Scripting

AWK split

Dear colleagues! I want to create a script which will take each file from the list and then parse it filename with awk/split. I do it this way: for file in `cat /$FileListFN`; do echo `awk ' {N=split(FILENAME,FNParts,"_")} {for (i=1; i<=N; i++) ... (10 Replies)
Discussion started by: slarionoff
10 Replies

10. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies
Login or Register to Ask a Question

Featured Tech Videos