Array Length Reports as Having Length when it is Empty?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array Length Reports as Having Length when it is Empty?
# 8  
Old 09-13-2012
Yea I hear ya Corona...
It definatly makes sense what you were saying, but this script will only be ran manually
when myself or another person needs to restart/start/stop/status the daemon...

I definatly agree with you and I appreciate you taking the time to explain it!
I just need to get some more practice with awk's language before I can utilize it
with any confidence, and be able to know what exactly, for instance, the one line
You wrote does, and be able to debug later if need be.

Thanks Again,
Matt
# 9  
Old 09-13-2012
Here's an example of how awk works.

Code:
$ cat <<"EOF" >example1.awk

# Comments begin with # like in shell

# It takes a list of expressions and code blocks, and for every line
# it reads, runs the code blocks in order.
# next will cause it to skip to the next line without running statements below.
{ print "This runs first" }
{ print "This runs second" }
{ print "This runs third"; next }
{ print "This would run fourth except for that next" }
EOF

$ echo | awk -f example1.awk

This runs first
This runs second
This runs third

$ cat <<"EOF" >example2.awk

# Instead of code-blocks, you can put simple statements.
# Whenever the expression is true, the current line will be printed.
# /string/ is a statement.  Like in sed, that means to look for string.

/string/
EOF

$ echo "string" | awk -f example2.awk

string

$ echo "strong" | awk -f example2.awk

$ cat <<EOF >example3.awk
# You can put an expression in front of a code block on the same line.
# This will cause it to run the code block only when it is true.
/string/ {
        print "String matched"
}
EOF

$ echo "string" | awk -f example3.awk 

String matched

$ echo "strong" | awk -f example3.awk

$ cat <<"EOF" >example4.awk
# awk supports variables and columns.  $ does NOT mean variable, $ is
# an operator!  It means column.  So $5 is the 5th column.
# If you did N=5; print $N, that would print the 5th column.

# If the first column is 1, it will print column 1.
# If the first column is 2, it will print column 2.  etc.
{ print $($1) }
EOF

$ echo "1 a b c d" | awk -f example4.awk

1

$ echo "4 a b c d" | awk -f example4.awk

c

$ cat <<"EOF" >example5.awk
# BEGIN and END are special code-blocks which run before the program
# begins reading lines and once it finishes reading lines.
BEGIN { print "This runs before" }
END { print "This runs after" }
# This expression is always true, so always prints
1
EOF

$ echo "asdf" | awk -f example5.awk

This runs before
asdf
This runs after

$ cat <<"EOF" >example6.awk

# awk uses spaces as separators by default, but can split on any
# character.  GNU awk even lets you use a regex.
BEGIN { FS="." }
{ print $2 }
EOF

$ echo "A.b.c.d" | awk -f example6.awk

b

$ # You can assign columns, not just read them!
$ echo "1 2 3 4 5 6 7 8 9 10" | awk '{ $5="asdf" } 1'

1 2 3 4 asdf 6 7 8 9 10

$ # There are lots of special variables available.  NF is the number of
$ # columns, starting at 1 ( which makes $NF the last column ).
$ # NR is the total number of lines.
$ # FNR is the line-number in the current file being read.
$ # You can tell it to read stdin with -.  If no files are given, it also reads stdin.
$ # FILENAME is a special variable which tells you what file awk is currently reading.
$ printf "%s\n" a b c d e f g | awk '{ print "File", FILENAME, "Total", NR, "Current", FNR, $0 }' - example1.awk

File - Total 1 Current 1 a
File - Total 2 Current 2 b
File - Total 3 Current 3 c
File - Total 4 Current 4 d
File - Total 5 Current 5 e
File - Total 6 Current 6 f
File - Total 7 Current 7 g
File example1.awk Total 8 Current 1
File example1.awk Total 9 Current 2 # Comments begin with # like in shell
File example1.awk Total 10 Current 3
File example1.awk Total 11 Current 4 # It takes a list of expressions and code blocks, and for every line
File example1.awk Total 12 Current 5 # it reads, runs the code blocks in order.
File example1.awk Total 13 Current 6 { print "This runs first" }
File example1.awk Total 14 Current 7 { print "This runs second" }
File example1.awk Total 15 Current 8 { print "This runs third"; next }
File example1.awk Total 16 Current 9 { print "This would run fourth except for that next" }

$ # The OFS variable controls the output field separator.  It defaults to a space.
$ # You can set variables outside the awk program with -v VAR=VALUE.
$ # So let's show what OFS does.
$ printf "%s\n" a b c d e f g | awk -v OFS="," '{ print "File", FILENAME, "Total", NR, "Current", FNR, $0 }' - example1.awk

File,-,Total,1,Current,1,a
File,-,Total,2,Current,2,b
File,-,Total,3,Current,3,c
File,-,Total,4,Current,4,d
File,-,Total,5,Current,5,e
File,-,Total,6,Current,6,f
File,-,Total,7,Current,7,g
File,example1.awk,Total,8,Current,1,
File,example1.awk,Total,9,Current,2,# Comments begin with # like in shell
File,example1.awk,Total,10,Current,3,
File,example1.awk,Total,11,Current,4,# It takes a list of expressions and code blocks, and for every line
File,example1.awk,Total,12,Current,5,# it reads, runs the code blocks in order.
File,example1.awk,Total,13,Current,6,{ print "This runs first" }
File,example1.awk,Total,14,Current,7,{ print "This runs second" }
File,example1.awk,Total,15,Current,8,{ print "This runs third"; next }
File,example1.awk,Total,16,Current,9,{ print "This would run fourth except for that next" }

$ # ORS is like OFS, except it controls what's printed at the end of a line.  Defaults to newline.
$ printf "%s\n" a b c d e f g | awk -v ORS="," '{ print "File", FILENAME, "Total", NR, "Current", FNR, $0 }' - example1.awk
File - Total 1 Current 1 a,File - Total 2 Current 2 b,File - Total 3 Current 3 c,File - Total 4 Current 4 d,File - Total 5 Current 5 e,File - Total 6 Current 6 f,File - Total 7 Current 7 g,File example1.awk Total 8 Current 1 ,File example1.awk Total 9 Current 2 # Comments begin with # like in shell,File example1.awk Total 10 Current 3 ,File example1.awk Total 11 Current 4 # It takes a list of expressions and code blocks, and for every line,File example1.awk Total 12 Current 5 # it reads, runs the code blocks in order.,File example1.awk Total 13 Current 6 { print "This runs first" },File example1.awk Total 14 Current 7 { print "This runs second" },File example1.awk Total 15 Current 8 { print "This runs third"; next },File example1.awk Total 16 Current 9 { print "This would run fourth except for that next" },

$

# 10  
Old 09-14-2012
Cool, thanks for the examples!


Thanks,
Matt

---------- Post updated at 05:08 PM ---------- Previous update was at 09:38 AM ----------

Hey Corona, I'm playing with awk a bit and was trying some examples and just general testing of awk.

If say you have a 'string' that is basically just a list of numbers separated by a space, how do you print from
'{print $1}' to say the last element/column in the string..?

I thought it had something to do with "END" but I couldn't figure it out...

For Example: I had been doing stuff with Unix Timestamps previously and thought to return to it with a different approach using awk for some practice...
Code:
DATE_VARS=$(date "+%S %M %k %d %m %Y")
# OUTPUT -->     DATE_VARS == "09 02 17 14 09 2012"

### Use 'awk' to set the variable PERL_VARS...
PERL_VARS=$(echo "$DATE_VARS" | awk -F ' ' -v ORS=',' '{print $1} END')

# OUTPUT of $PERL_VARS should be:
# "09,02,17,14,09,2012"

Not sure what I'm missing in the "awk" command..?
I'm sure there's a million different ways to do this but I was curious how exactly I would do it like the above example?

Any suggestions??


Thanks in Advance,
Matt
# 11  
Old 09-14-2012
you can do like this..

Code:
 $echo "$DATE_VARS" | tr " " ","
50,16,17,14,09,2012

and for your command..
Code:
PERL_VARS=$(echo "$DATE_VARS" | awk -F ' ' -v ORS=',' '{print $1} END')

1) Space is by default field separator so no need to give..
2) $1 prints only first field
3) END command needs action part...

try this..
Code:
echo "$DATE_VARS" | awk  -v ORS=',' '{for(i=1;i<=NF;i++) {print $i}}'

This User Gave Thanks to pamu For This Post:
# 12  
Old 09-15-2012
Code:
date "+%S %M %k %d %m %Y"|awk '$1=$1' OFS=,

This User Gave Thanks to RudiC For This Post:
# 13  
Old 09-15-2012
Quote:
Originally Posted by mrm5102
If say you have a 'string' that is basically just a list of numbers separated by a space, how do you print from
'{print $1}' to say the last element/column in the string..?
Use $0 to get the entire string.

Quote:
I thought it had something to do with "END" but I couldn't figure it out...
It does not. END is the end of the program, not the string.
This User Gave Thanks to Corona688 For This Post:
# 14  
Old 09-15-2012
You do not need awk to solve your problem. Simply make the date the way you want it in the first place, and you will not have to translate it.

Code:
DATE_VARS=$(date "+%S,%M,%k,%d,%m,%Y")

See RudiC's example, though. Also, the one I wrote you which uses OFS too.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert variable length record to fixed length

Hi Team, I have an issue to split the file which is having special chracter(German Char) using awk command. I have a different length records in a file. I am separating the files based on the length using awk command. The command is working fine if the record is not having any... (7 Replies)
Discussion started by: Anthuvan
7 Replies

2. Shell Programming and Scripting

Flat file-make field length equal to header length

Hello Everyone, I am stuck with one issue while working on abstract flat file which i have to use as input and load data to table. Input Data- ------ ------------------------ ---- ----------------- WFI001 Xxxxxx Control Work Item A Number of Records ------ ------------------------... (5 Replies)
Discussion started by: sonali.s.more
5 Replies

3. Shell Programming and Scripting

Generate 100 Character Fixed Length Empty File

Hello Everyone, I'm running AIX 5.3 and need to generate a 100 character fixed length empty file from within a bash script that I am developing. I searched and was able to find: dd if=/dev/zero of=/test/path/file count=100 however my understanding is that this will generate a file of a... (10 Replies)
Discussion started by: jvt
10 Replies

4. Shell Programming and Scripting

changing a variable length text to a fixed length

Hi, Can anyone help with a effective solution ? I need to change a variable length text field (between 1 - 18 characters) to a fixed length text of 18 characters with the unused portion, at the end, filled with spaces. The text field is actually field 10 of a .csv file however I could cut... (7 Replies)
Discussion started by: dc18
7 Replies

5. Shell Programming and Scripting

Make variable length record a fixed length

Very, very new to unix scripting and have a unique situation. I have a file of records that contain 3 records types: (H)eader Records (D)etail Records (T)railer Records The Detail records are 82 bytes in length which is perfect. The Header and Trailer records sometimes are 82 bytes in... (3 Replies)
Discussion started by: jclanc8
3 Replies

6. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

7. UNIX for Dummies Questions & Answers

Length of a Unix filepath max length

Hi Guys, Could anyone shed some light on the length of a Unix filepath max length pls ? thanks ! Wilson (3 Replies)
Discussion started by: wilsontan
3 Replies

8. UNIX for Dummies Questions & Answers

Sed working on lines of small length and not large length

Hi , I have a peculiar case, where my sed command is working on a file which contains lines of small length. sed "s/XYZ:1/XYZ:3/g" abc.txt > xyz.txt when abc.txt contains lines of small length(currently around 80 chars) , this sed command is working fine. when abc.txt contains lines of... (3 Replies)
Discussion started by: thanuman
3 Replies

9. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies
Login or Register to Ask a Question