awk is Printing folders with only numbers as expected. But can't explain 'total' statement.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk is Printing folders with only numbers as expected. But can't explain 'total' statement.
# 1  
Old 09-02-2010
awk is Printing folders with only numbers as expected. But can't explain 'total' statement.

I am trying to get folder names that contain only numbers.

Can someone explain why following command is printing 'total 450' as part of output..

Code:
 
$>  ls -lt | awk '$9 ~ /^[1-9]*$/' | more
total 450
drwxr-x--x   3 user1  group1     512 Mar  9  2008 329227163
drwxr-x--x   3 user1  group1     512 Mar  9  2008 1285642344
drwxr-x--x   3 user1  group1     512 Mar  9  2008 94825883

Rest of the output is expected.
# 2  
Old 09-02-2010
Code:
ls -lt | awk '$9 ~ /^[1-9]+$/' | more

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 09-02-2010
Quote:
Originally Posted by kchinnam
I am trying to get folder names that contain only numbers.

Can someone explain why following command is printing 'total 450' as part of output..

Code:
 
$>  ls -lt | awk '$9 ~ /^[1-9]*$/' | more
total 450
drwxr-x--x   3 user1  group1     512 Mar  9  2008 329227163
[...]

[1-9]* is 0 or more, therefore you're searching for empty or only numeric 9th field.
You need 1 or more - [1-9][1-9]* or [1-9]+ (if supported).
# 4  
Old 09-02-2010
'cause "/[^1-9]*$" evaluates to 0 or more digits.
As there's no field 9 in 'total 450', the condition is true.
Try - you need one or more occurrences of digits:
Code:
ls -lt | awk '$9 ~ /^[1-9][1-9]*$/'

# 5  
Old 09-02-2010
wow, so quick so many working solutions. Thaks to everyone. All solutions are working.

Yes,, ^[1-9]*+ is equivalent to 1 or more occurances of names that contain only numbers. Thanks to bartus11.

Code:
ls -lt | awk '$9 ~ /^[1-9]+$/'


Last edited by kchinnam; 09-02-2010 at 10:54 AM.. Reason: grammer
# 6  
Old 09-02-2010
Just to add that recent shells support extended globbing and if you're using one of them, you don't need awk for this task.

With ksh:

Code:
ls -lt !(*[!1-9]*)

With bash:

Code:
shopt -s extglob
ls -lt !(*[!1-9]*)

Or 0-9, depending on your needs.

With zsh:

Code:
ls -lt <->

# 7  
Old 09-02-2010
It appears on solaris 8 I have extended set here --> /usr/xpg4/bin/ls
But I am not sure where in your reg.exp you are telling it to act only on folder names(column 9).

Also why can't we tell extended globber to understand something like
this -->
Quote:
(^[1-9])+$ or +(^[1-9])$
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash read input in case statement not working as expected

I'm having an issue with bash read input when using a case statement. The script halts and doesn't read the input on the first loop. if I hit enter then the scripts starts to respond as expected. Need some help here. defaultans=8hrs read -e -i $defaultans -p "${bldwht}How long would you like... (5 Replies)
Discussion started by: woodson2
5 Replies

2. Shell Programming and Scripting

If statement fails with integer expression expected

Below is what i have in my script. htcount=$(curl -s --user tomcatstatus:tomcatstatus http://`hostname`.mypc.com:887/manager/jmxproxy?qry=Catalina:type=ThreadPool,name=\"http-nio-887\" |grep sBusy | cut -d ' ' -f2) echo $htcount if ; then echo "more than 10" else echo "Less than 10" fi... (6 Replies)
Discussion started by: mohtashims
6 Replies

3. UNIX for Dummies Questions & Answers

I have this box thing, total newbie here can someone explain?

Hey guys, I'm really new to all this side of computing and have just had this box sort of left with me and it peaked my curiosity. So i would up here in a desperate bid to find out what the hell it is because although its sort of obvious what it is, no where online is able to give me a detailed... (5 Replies)
Discussion started by: anthony346
5 Replies

4. Shell Programming and Scripting

Case statement not working as expected

case "$freq" in " Hz") low=250; high=550;; "8 Hz") low=250; high=1000;; " Hz") low=400; high=1000;; "63 Hz") low=550; high=1000;; " Hz") low=400; high=550;; ... (2 Replies)
Discussion started by: Michael Stora
2 Replies

5. Shell Programming and Scripting

If statement with [[ ]] and regex not working as expected

Using BASH: $ if -- ::00" ]]; then echo "true"; else echo "false"; fi false Mike (5 Replies)
Discussion started by: Michael Stora
5 Replies

6. Shell Programming and Scripting

awk if statement not printing entire field

I have an input that looks like this: chr1 mm9_knownGene utr3 3204563 3206102 0 - . gene_id "Xkr4"; transcript_id "uc007aeu.1"; chr1 mm9_knownGene utr3 4280927 4283061 0 - . gene_id "Rp1"; transcript_id "uc007aew.1"; chr1 mm9_knownGene ... (5 Replies)
Discussion started by: pbluescript
5 Replies

7. Shell Programming and Scripting

How to insert numbers to a in between statement

Hi Guys, I want to create a shell script that will give me the output below. I want to insert the numbers from the input file to my url addresses below. And from the numbers below, I want to separate the last digit with a period (i.e. from 222222222222 to 22222222222.2). Appreciate any help.... (14 Replies)
Discussion started by: pinpe
14 Replies

8. Shell Programming and Scripting

Trouble with printing to other folders w/ awk

Again, I am in need of some advice. Earlier I was shown how to have awk create folders for me. That was so cool and helpful, but now I am exploring the posibilities of combining operations with bash scripts. Now, I am creating the directories with the bash script, and then I want awk to... (1 Reply)
Discussion started by: ccox85
1 Replies

9. Shell Programming and Scripting

Need to total numbers at end of script

I need to total the numbers at the end of script, what code can i use for this to total the lines up? 1232 1242 1234 Total count is 3708 (9 Replies)
Discussion started by: wereyou
9 Replies

10. UNIX for Advanced & Expert Users

can you explain this AWK "c+" statement ?

Well i use the following statement in a script to calculate the all column value of a ls command : find . -name *.leon -exec ls -al {} \;|awk '{c+=$5} END {print c}' And this is the result : 2.34546e+09 i would know what is exactly the "c+" role , and what i must modify to have the result... (8 Replies)
Discussion started by: Nicol
8 Replies
Login or Register to Ask a Question