Re: using AWK
On Mon, 17 Dec 2012 08:16:26 -0800 Devin Teske wrote: On Dec 17, 2012, at 3:39 AM, Jack Mc Lauren wrote: Hi guys How can I read a file which contains a number and assign that number to a variable via awk programming? By the way, I want to use this awk program in a shell script. Thanks in advance Try this: awk -v file=/etc/ttys 'BEGIN { getline line file; printf First line from %s: %s\n, file, line }' Semms a bit complicated when you could set the awk variable directly e.g. $ echo 42 /tmp/f $ awk -v x=`cat /tmp/f` 'BEGIN{ print x+1 }' 43 ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
using AWK
Hi guys How can I read a file which contains a number and assign that number to a variable via awk programming? By the way, I want to use this awk program in a shell script. Thanks in advance ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: using AWK
Hi Jack, On Dec 17, 2012, at 03:39, Jack Mc Lauren jack.mclau...@yahoo.com wrote: How can I read a file which contains a number and assign that number to a variable via awk programming? By the way, I want to use this awk program in a shell script. I'm actually not sure what you're asking, exactly -- you want the number to go into an awk variable? Or a shell variable? Assuming you want it to go into an awk variable, I would try something like this: getline my_number filename; close filename; That assumes the filename is stored in the variable named filename. It puts the number in the awk variable named my_number. To put that in context, let's say you're getting the filename from $0, and you want to multiply the number by 2 and print it. You might do: filename = $0; getline my_number filename; close filename; print my_number * 2; Or if I completely misinterpreted your question, let me know :-) ~Ben ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: using AWK
On 12/17/2012 12:39 PM, Jack Mc Lauren wrote: Hi guys How can I read a file which contains a number and assign that number to a variable via awk programming? By the way, I want to use this awk program in a shell script. Thanks in advance ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org A good awk start : http://www.grymoire.com/Unix/Awk.html ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: using AWK
Hi Jack, HI How can I read a file which contains a number and assign that number to a variable via awk programming? By the way, I want to use this awk program in a shell script. I'm actually not sure what you're asking, exactly -- you want the number to go into an awk variable? Or a shell variable? Yes, I want the number to go into an awk variable. Assuming you want it to go into an awk variable, I would try something like this: getline my_number filename; close filename; That assumes the filename is stored in the variable named filename. It puts the number in the awk variable named my_number. To put that in context, let's say you're getting the filename from $0, and you want to multiply the number by 2 and print it. You might do: filename = $0; getline my_number filename; close filename; print my_number * 2; Or if I completely misinterpreted your question, let me know :-) ~Ben This is what i wrote: #! /bin/sh filename=$0 awk 'getline no filename; print no' But when I run this script sh /awk_no.sh /var/no.txt I have this error : awk: syntax error at source line 1 context is getline no filename; print no awk: bailing out at source line 1 Thank you :) ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: using AWK
On Mon, 17 Dec 2012 04:22:21 -0800 (PST), Jack Mc Lauren wrote: Yes, I want the number to go into an awk variable. [...] This is what i wrote: #! /bin/sh filename=$0 awk 'getline no filename; print no' But when I run this script sh /awk_no.sh /var/no.txt I have this error : awk: syntax error at source line 1 context is getline no filename; print no awk: bailing out at source line 1 Thank you :) The error is obvious: You need to transition $filename from the sh level into the awk script, i. e. the file name string (in your example, /var/no.txt) must be visible inside the awk script. You're using a variable called filename which is uninitialized (empty). You can either define the file name statically in the awk script, or use ${filename} in the script (use double quotes to allow resolution). Also note that $0 is the name by which the script has been called. $1 is the 1st parameter. -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ... ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: using AWK
On Dec 17, 2012, at 04:22, Jack Mc Lauren jack.mclau...@yahoo.com wrote: This is what i wrote: OK -- I'm adjusting my assumptions about what you're trying to do. :-) Bear with me: #! /bin/sh filename=$0 So (a) there's only one input file, not multiple... and (b) it should come from the command line of the shell script wrapper. Right? awk 'getline no filename; print no' If there's only one input file, then this is super easy and you don't even need any of the getline or close stuff. Try: filename=$1 awk '{no = $0; print no;}' $filename In the shell script context (outside the awk), $1 refers to the first command line parameter of the script. You don't want $0 there. On the other hand, *inside* the awk part, dollar-sign variables have a completely different meaning. $0 in *awk* (not sh) means the entire contents of each line of the input file. So if your file had multiple lines, that block would run multiple times. But since I'm guessing your file only has one line (that being the number in question), the awk block will only run once. ~Ben ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: using AWK
On Dec 17, 2012, at 3:39 AM, Jack Mc Lauren wrote: Hi guys How can I read a file which contains a number and assign that number to a variable via awk programming? By the way, I want to use this awk program in a shell script. Thanks in advance Try this: awk -v file=/etc/ttys 'BEGIN { getline line file; printf First line from %s: %s\n, file, line }' -- Devin _ The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: using AWK
On Mon, 17 Dec 2012 08:16:26 -0800, Devin Teske wrote: On Dec 17, 2012, at 3:39 AM, Jack Mc Lauren wrote: Hi guys How can I read a file which contains a number and assign that number to a variable via awk programming? By the way, I want to use this awk program in a shell script. Thanks in advance Try this: awk -v file=/etc/ttys 'BEGIN { getline line file; printf First line from %s: %s\n, file, line }' Or more verbose: #!/bin/sh filename=$1 echo file is ${filename} with content: cat ${filename} echo calling awk... awk BEGIN { getline no \${filename}\ close \${filename}\ print no print no * 2 } # EXAMPLE: # # # % ./awkvar.sh /tmp/no.txt # file is /tmp/no.txt with content: # 12345 # calling awk... # 12345 # 24690 The example shows how to use the variable inside awk. You could get rid of the getline function in case the file contains only the number you're interested in. If you need further processing of the file, you can do that inside awk (e. g. omitting comment lines, obtain data from a given line number of specific pattern). -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ... ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: using AWK
On Dec 17, 2012, at 8:23 AM, Polytropon wrote: On Mon, 17 Dec 2012 08:16:26 -0800, Devin Teske wrote: On Dec 17, 2012, at 3:39 AM, Jack Mc Lauren wrote: Hi guys How can I read a file which contains a number and assign that number to a variable via awk programming? By the way, I want to use this awk program in a shell script. Thanks in advance Try this: awk -v file=/etc/ttys 'BEGIN { getline line file; printf First line from %s: %s\n, file, line }' Or more verbose: #!/bin/sh filename=$1 echo file is ${filename} with content: cat ${filename} echo calling awk... awk -v filename=$filename BEGIN { getline no filename close filename print no print no * 2 } # EXAMPLE: # # # % ./awkvar.sh /tmp/no.txt # file is /tmp/no.txt with content: # 12345 # calling awk... # 12345 # 24690 The example shows how to use the variable inside awk. You could get rid of the getline function in case the file contains only the number you're interested in. If you need further processing of the file, you can do that inside awk (e. g. omitting comment lines, obtain data from a given line number of specific pattern). -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ... _ The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
using AWK - Thanks :)
Hi all Thank you so much my friends, Ben Frank Polytropon Devin you helped me so much :) ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: OT: Shell Script using Awk
On Sunday 02 November 2008 03:21:55 David Allen wrote: My apologies for asking on this list, but I'm stuck without Perl and need to use awk to generate a report. I'm working with a large data set spread across multiple files, but to keep things simple, say I have A Very Long String that containing records, each delimited by a single space. I need to print those records in columnar format, but with only 7 columns per line: record1 record2 record3 record4 record5 record6 record7 record08 record09 record10 record11 record12 record13 record14 Are you dead set on using awk(1)? Because my first thought would be rs(1). cat inputfile | rs 0 7 To turn your space-separated entries into 7 columns. You may need some fiddling about (to avoid running out of memory, space on the line, etc). This is one of my top three sadly-neglected BSD commands everyone should know more about, along with lam(1) and jot(1). Jonathan ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: OT: Shell Script using Awk
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 David Allen wrote: On 11/1/08, Sahil Tandon [EMAIL PROTECTED] wrote: David Allen [EMAIL PROTECTED] wrote: My apologies for asking on this list, but I'm stuck without Perl and need to use awk to generate a report. I'm working with a large data set spread across multiple files, but to keep things simple, say I have A Very Long String that containing records, each delimited by a single space. I need to print those records in columnar format, but with only 7 columns per line: record1 record2 record3 record4 record5 record6 record7 record08 record09 record10 record11 record12 record13 record14 ... A small sh script: #!/bin/sh awk ' { for (i=1; i=NF; i++) { printf(%s , $i) if (i % 7 == 0) { printf(\n) } } if (NF % 7 != 0) { printf(\n) } } ' input An elegant solution if ever I read one. The mod operator should have been the first thing that came to mind. I'm not sure whether I need a class in remedial math, or remedial awk, but either way, my thanks for the solution. Just in case you've never discovered column, piping the output of this to column -t will get you nice formatting for free. - -- Thanks, Josh Paetzel PGP: 8A48 EF36 5E9F 4EDA 5ABC 11B4 26F9 01F1 27AF AECB -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.8 (Darwin) iEYEARECAAYFAkkNlBEACgkQJvkB8Sevrsv6lwCdHk5llGh4ZG+0CnQLARJDqGD9 0AEAniRtmjDNfKXHdsGAudA3uiwYFB9f =IImT -END PGP SIGNATURE- ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
OT: Shell Script using Awk
My apologies for asking on this list, but I'm stuck without Perl and need to use awk to generate a report. I'm working with a large data set spread across multiple files, but to keep things simple, say I have A Very Long String that containing records, each delimited by a single space. I need to print those records in columnar format, but with only 7 columns per line: record1 record2 record3 record4 record5 record6 record7 record08 record09 record10 record11 record12 record13 record14 ... Should be simple, but I'm getting nowhere. Thanks! -- David promising never to do this again Allen ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: OT: Shell Script using Awk
On Sat, Nov 01, 2008 at 06:21:55PM -0700, David Allen wrote: My apologies for asking on this list, but I'm stuck without Perl and need to use awk to generate a report. I'm working with a large data set spread across multiple files, but to keep things simple, say I have A Very Long String that containing records, each delimited by a single space. I need to print those records in columnar format, but with only 7 columns per line: record1 record2 record3 record4 record5 record6 record7 record08 record09 record10 record11 record12 record13 record14 ... Should be simple, but I'm getting nowhere. $ cat input col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 $ cat output.awk { print $1 $2$3$4$5$6$7 print $8 $9 $10 $11 $12 $13 $14 } $ cat intput | awk -f output.awk col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 -- | Jeremy Chadwickjdc at parodius.com | | Parodius Networking http://www.parodius.com/ | | UNIX Systems Administrator Mountain View, CA, USA | | Making life hard for others since 1977. PGP: 4BD6C0CB | ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: OT: Shell Script using Awk
On 11/1/08, Jeremy Chadwick [EMAIL PROTECTED] wrote: On Sat, Nov 01, 2008 at 06:21:55PM -0700, David Allen wrote: My apologies for asking on this list, but I'm stuck without Perl and need to use awk to generate a report. I'm working with a large data set spread across multiple files, but to keep things simple, say I have A Very Long String that containing records, each delimited by a single space. I need to print those records in columnar format, but with only 7 columns per line: record1 record2 record3 record4 record5 record6 record7 record08 record09 record10 record11 record12 record13 record14 ... Should be simple, but I'm getting nowhere. $ cat input col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 $ cat output.awk { print $1 $2$3$4$5$6$7 print $8 $9 $10 $11 $12 $13 $14 } $ cat intput | awk -f output.awk col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 Thanks for the reply, Jeremy, but that approach would require an entirely manual approach, which isn't suitable for what I'm working with. Writing a script that's the same size as the data I'm working with isn't an option. ;-) ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: OT: Shell Script using Awk
On Sat, Nov 01, 2008 at 08:17:54PM -0800, David Allen wrote: On 11/1/08, Jeremy Chadwick [EMAIL PROTECTED] wrote: On Sat, Nov 01, 2008 at 06:21:55PM -0700, David Allen wrote: My apologies for asking on this list, but I'm stuck without Perl and need to use awk to generate a report. I'm working with a large data set spread across multiple files, but to keep things simple, say I have A Very Long String that containing records, each delimited by a single space. I need to print those records in columnar format, but with only 7 columns per line: record1 record2 record3 record4 record5 record6 record7 record08 record09 record10 record11 record12 record13 record14 ... Should be simple, but I'm getting nowhere. $ cat input col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 $ cat output.awk { print $1 $2$3$4$5$6$7 print $8 $9 $10 $11 $12 $13 $14 } $ cat intput | awk -f output.awk col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 Thanks for the reply, Jeremy, but that approach would require an entirely manual approach, which isn't suitable for what I'm working with. Writing a script that's the same size as the data I'm working with isn't an option. ;-) I'm confused -- what's the problem? -- | Jeremy Chadwickjdc at parodius.com | | Parodius Networking http://www.parodius.com/ | | UNIX Systems Administrator Mountain View, CA, USA | | Making life hard for others since 1977. PGP: 4BD6C0CB | ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: OT: Shell Script using Awk
On Sat, 1 Nov 2008 18:21:55 -0700, David Allen [EMAIL PROTECTED] wrote: My apologies for asking on this list, but I'm stuck without Perl and need to use awk to generate a report. I'm working with a large data set spread across multiple files, but to keep things simple, say I have A Very Long String that containing records, each delimited by a single space. I need to print those records in columnar format, but with only 7 columns per line: record1 record2 record3 record4 record5 record6 record7 record08 record09 record10 record11 record12 record13 record14 ... Should be simple, but I'm getting nowhere. Is this what you're after? $ cat input col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 col15 col16 cat input | awk -F\ '{for (i=1;iNF;i+=7) print $i,$(i+1),$(i+2),$(i+3),$(i+4),$(i+5),$(i+6) }' Thanks! -- David promising never to do this again Allen ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED] ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: OT: Shell Script using Awk
On Sat, 1 Nov 2008 20:17:54 -0800, David Allen [EMAIL PROTECTED] wrote: On 11/1/08, Jeremy Chadwick [EMAIL PROTECTED] wrote: On Sat, Nov 01, 2008 at 06:21:55PM -0700, David Allen wrote: My apologies for asking on this list, but I'm stuck without Perl and need to use awk to generate a report. I'm working with a large data set spread across multiple files, but to keep things simple, say I have A Very Long String that containing records, each delimited by a single space. I need to print those records in columnar format, but with only 7 columns per line: record1 record2 record3 record4 record5 record6 record7 record08 record09 record10 record11 record12 record13 record14 ... Should be simple, but I'm getting nowhere. $ cat input col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 $ cat output.awk { print $1 $2$3$4$5$6$7 print $8 $9 $10 $11 $12 $13 $14 } $ cat intput | awk -f output.awk col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 Maybe you want them to line up too. Would using tabs be appropriate? Maybe something like this? awk -F\ '{for (i=1;iNF;i+=7) print $i \t $(i+1) \t $(i+2) \t $(i+3) \t $(i+4) \t $(i+5) \t $(i+6) }' input Thanks for the reply, Jeremy, but that approach would require an entirely manual approach, which isn't suitable for what I'm working with. Writing a script that's the same size as the data I'm working with isn't an option. ;-) ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED] ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: OT: Shell Script using Awk
David Allen [EMAIL PROTECTED] wrote: My apologies for asking on this list, but I'm stuck without Perl and need to use awk to generate a report. I'm working with a large data set spread across multiple files, but to keep things simple, say I have A Very Long String that containing records, each delimited by a single space. I need to print those records in columnar format, but with only 7 columns per line: record1 record2 record3 record4 record5 record6 record7 record08 record09 record10 record11 record12 record13 record14 ... A small sh script: #!/bin/sh awk ' { for (i=1; i=NF; i++) { printf(%s , $i) if (i % 7 == 0) { printf(\n) } } if (NF % 7 != 0) { printf(\n) } } ' input -- Sahil Tandon [EMAIL PROTECTED] ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: OT: Shell Script using Awk
On 11/1/08, Gary Newcombe [EMAIL PROTECTED] wrote: On Sat, 1 Nov 2008 18:21:55 -0700, David Allen [EMAIL PROTECTED] wrote: My apologies for asking on this list, but I'm stuck without Perl and need to use awk to generate a report. I'm working with a large data set spread across multiple files, but to keep things simple, say I have A Very Long String that containing records, each delimited by a single space. I need to print those records in columnar format, but with only 7 columns per line: record1 record2 record3 record4 record5 record6 record7 record08 record09 record10 record11 record12 record13 record14 ... Should be simple, but I'm getting nowhere. Is this what you're after? $ cat input col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 col15 col16 cat input | awk -F\ '{for (i=1;iNF;i+=7) print $i,$(i+1),$(i+2),$(i+3),$(i+4),$(i+5),$(i+6) }' Bingo! That was what similar to what I was starting with before going off on a tangent. Seems I screwed up the syntax and gave up too early. Thanks, Gary. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: OT: Shell Script using Awk
On 11/1/08, Sahil Tandon [EMAIL PROTECTED] wrote: David Allen [EMAIL PROTECTED] wrote: My apologies for asking on this list, but I'm stuck without Perl and need to use awk to generate a report. I'm working with a large data set spread across multiple files, but to keep things simple, say I have A Very Long String that containing records, each delimited by a single space. I need to print those records in columnar format, but with only 7 columns per line: record1 record2 record3 record4 record5 record6 record7 record08 record09 record10 record11 record12 record13 record14 ... A small sh script: #!/bin/sh awk ' { for (i=1; i=NF; i++) { printf(%s , $i) if (i % 7 == 0) { printf(\n) } } if (NF % 7 != 0) { printf(\n) } } ' input An elegant solution if ever I read one. The mod operator should have been the first thing that came to mind. I'm not sure whether I need a class in remedial math, or remedial awk, but either way, my thanks for the solution. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]