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.
# 8  
Old 09-02-2010
Quote:
It appears on solaris 8 I have extended set here --> /usr/xpg4/bin/ls
These are the X/Open versions of some Unix programs, but the filename generation/globbing
is a shell feature, the ls program doesn't see the glob, it sees the expanded list of filenames.

Quote:
But I am not sure where in your reg.exp you are telling it to act only on folder names(column 9).
I'm just passing the filenames to the ls program, ls operates directly on filenames, no need to specify any column
(if I understand the question correctly ...)

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

No, but you can use the pattern I posted.
# 9  
Old 09-02-2010
Do you need user/group/size/etc?

If not, you can use:
Code:
# touch 1 2 3 4
# ls -1t | egrep '^[0-9]+$'
1
2
3
4

# 10  
Old 09-02-2010
Quote:
Originally Posted by felipe.vinturin
Do you need user/group/size/etc?

If not, you can use:
Code:
# touch 1 2 3 4
# ls -1t | egrep '^[0-9]+$'
1
2
3
4

And you don't even need the -1 option in this context.


P.S. Actually, if you don't need the modification time info, you don't need the external commands ls and grep either, with ksh (or bash and extglob enabled) you could simply:

Code:
printf '%s\n' !(*[!0-9]*)

# 11  
Old 09-02-2010
Code:
shopt -s extglob
ls -lt !(*[!1-9]*)   # This is trying to list contents under the folders it is finding. I wanted only folder names

Code:
ls -lt <->   # This is trying to list contents under the folders it is finding. I wanted only folder names

Above commands would have listed only folders that are matching if it can get something like this working
Quote:
ls -lt | grep !(*[!1-9]*)
Regarding why I started using awk instead of egrep, I also needed to filter folders based on thier owner. Here is what I have so far working.. I am trying to delete lot of old folders under /var/tmp

Code:
VAR_USERS='userA|userB|userC'
/usr/xpg4/bin/awk -v vUsers="$VAR_USERS" '$3 ~ vUsers && $9 ~ /^[1-9]+$|sjs*|JCE*/ {print $9}'

Ideally I would want to use parameter for this also -->
Quote:
^[1-9]+$|sjs*|JCE*
. But I could not get it to work.

Code:
VAR_USERS='userA|userB|userC'  # Folders owned by only these users.
VAR_FILTER='^[1-9]+$|sjs*|JCE*'    # (Folders with only Numbers) OR (that start with 'sjs') OR (that start with 'JCE')

/usr/xpg4/bin/awk -v vUsers="$VAR_USERS" -v vFilter="$VAR_FILTER" '$3 ~ vUsers && $9 ~ vFilter {print $9}'

I am all for not using expensive awk,, instead get this done by printf or bash's extglob or egrep( but I want to use parameters for all my search strings,,).

Last edited by kchinnam; 09-02-2010 at 11:22 PM.. Reason: formatting
# 12  
Old 09-03-2010
Quote:
Originally Posted by kchinnam
I am trying to get folder names that contain only numbers.
This should work
Code:
ls -lt | awk '/^d/&&($NF+0)==$NF'

# 13  
Old 09-03-2010
Quote:
Originally Posted by kchinnam
Code:
shopt -s extglob
ls -lt !(*[!1-9]*)   # This is trying to list contents under the folders it is finding. I wanted only folder names

Code:
ls -lt <->   # This is trying to list contents under the folders it is finding. I wanted only folder names

Above commands would have listed only folders that are matching if it can get something like this working
So just add -d option to ls:

Code:
ls -ltd ...

Quote:
Regarding why I started using awk instead of egrep, I also needed to filter folders based on thier owner. Here is what I have so far working.. I am trying to delete lot of old folders under /var/tmp

Code:
VAR_USERS='userA|userB|userC'
/usr/xpg4/bin/awk -v vUsers="$VAR_USERS" '$3 ~ vUsers && $9 ~ /^[1-9]+$|sjs*|JCE*/ {print $9}'

Ideally I would want to use parameter for this also --> . But I could not get it to work.


Code:
VAR_USERS='userA|userB|userC'  # Folders owned by only these users.
VAR_FILTER='^[1-9]+$|sjs*|JCE*'    # (Folders with only Numbers) OR (that start with 'sjs') OR (that start with 'JCE')

/usr/xpg4/bin/awk -v vUsers="$VAR_USERS" -v vFilter="$VAR_FILTER" '$3 ~ vUsers && $9 ~ vFilter {print $9}'

I am all for not using expensive awk,, instead get this done by printf or bash's extglob or egrep
( but I want to use parameters for all my search strings,,).
Well, as it often happens, you begin with a simple question and you end up with a completely different requirement.
You do use parameters for all your search strings. Could you please elaborate further?
# 14  
Old 09-03-2010
This works -->

Code:
VAR_USERS='userA|userB|userC'  # Folders owned by only these users.
/usr/xpg4/bin/awk -v vUsers="$VAR_USERS" '$3 ~ vUsers && $9 ~ /^[1-9]+$|sjs*|JCE*/ {print $9}'


This is not working -->

Code:
VAR_USERS='userA|userB|userC'  # Folders owned by only these users.
VAR_FILTER='^[1-9]+$|sjs*|JCE*'    # (Folders with only Numbers) OR (that start with 'sjs') OR (that start with 'JCE')

/usr/xpg4/bin/awk -v vUsers="$VAR_USERS" -v vFilter="$VAR_FILTER" '$3 ~ vUsers && $9 ~ vFilter {print $9}'

How can I pass regular expression as a parameter VAR_FILTER ?

Last edited by kchinnam; 09-03-2010 at 12:12 PM.. Reason: formatting
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