473,651 Members | 2,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Shell Script awk problem , please advice

20 New Member
Hi, i am currently writing a script to delete the directory named with number.

Example:
in /usr/
i have
/usr/101
/usr/102 ...... /usr/200

This is the command i type and it works, i got the directory listed where the number is <150

Expand|Select|Wrap|Line Numbers
  1. ls -l /usr/ | grep ^d | awk '{ if ( $9 < 150) print $9 }'
So i applied it to the script, and it is not working especially at awk if else comparison, screen printed ALL number (101 -200). I am suspecting the error is in awk command there. Please help.

Expand|Select|Wrap|Line Numbers
  1. #!/bin/bash -xv
  2. COPYKEEP="10"
  3. LATEST="200"
  4. KEEPFROM=`expr $LATEST - $COPYKEEP`
  5.  
  6. if [ $KEEPFROM -gt 0 ]
  7. then
  8.  
  9. # Find which to delete.
  10. ls -l /usr/ | grep ^d |  awk '{ if ( $9 < $KEEPFROM ) print $9 }'
  11. fi
Aug 7 '08 #1
6 3470
ashitpro
542 Recognized Expert Contributor
Expand|Select|Wrap|Line Numbers
  1. #!/bin/bash -xv
  2. COPYKEEP="10"
  3. LATEST="200"
  4. KEEPFROM=`expr $LATEST - $COPYKEEP`
  5. echo $KEEPFROM
  6. if [ $KEEPFROM -gt 0 ]
  7. then
  8. # Find which to delete.
  9. ls -l /usr/ | grep ^d |  awk -v p=$KEEPFROM '{ if ( $9 < p ) print $9 }'
  10. fi
  11.  
check the above script...
you can not use shell variables in awk directly..
it needs to be assigned to awk variable and then use it...
in Example we'r assigning it to 'p'
Aug 7 '08 #2
DannyMc
20 New Member
It works!!

Thank you so much!
Aug 11 '08 #3
DannyMc
20 New Member
Hi again,

i have tried to delete the file in awk using method found via google.

Expand|Select|Wrap|Line Numbers
  1. ls -l /usr1/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf " "\""$9"\"") }'
This is working , however when i apply this to the script

Expand|Select|Wrap|Line Numbers
  1. echo " ls -l /usr1/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf " "\""$9"\"") }' "
  2.  
The quoting is very confusing, can anyone help?
Aug 11 '08 #4
ashitpro
542 Recognized Expert Contributor
Hi again,

i have tried to delete the file in awk using method found via google.

Expand|Select|Wrap|Line Numbers
  1. ls -l /usr1/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf " "\""$9"\"") }'
This is working , however when i apply this to the script

Expand|Select|Wrap|Line Numbers
  1. echo " ls -l /usr1/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf " "\""$9"\"") }' "
  2.  
The quoting is very confusing, can anyone help?


I didn't need any complex quoting...
Next statement worked for me in shell script as well as on command line...

ls -l /usr/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf /usr/" $9) }'
Aug 11 '08 #5
DannyMc
20 New Member
Again, Thank you. It works :)
Aug 12 '08 #6
ghostdog74
511 Recognized Expert Contributor
there's actually another less cumbersome way to do it.
Expand|Select|Wrap|Line Numbers
  1. rm -rf /usr/[0-1][0-9][0-9]
  2. rm -rf /usr/200
  3.  
Aug 16 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1743
by: new guy | last post by:
Hello I have a problem running following shell command from php exec or system call: exec("/bin/cat $(/bin/cat list.txt) > concatenated.txt"). This works when i run it directly from the shell as user nobody (apache user), so its not a path/permissions problem. Basically list.txt contains a list of filenames, which are to be read and concatenated into one big file (concatenated.txt). I have even tried writing a shell script, wrapping this...
0
3045
by: Aashif | last post by:
I want to call Unix Shell script which is available in other Server (Unix server) from windows application using C#. Currently the shell script runs the C program but the GUI is not good, So I want to create GUI in C# windows application and call that C program using Shell script so first I have to call unix shell script from C#. Please guide me friends.
7
2238
by: Frank Potter | last post by:
I learned some python in windows. And now I've turned to linux. I read a book and it teaches how to write shell script with bash, but I don't feel like the grammar of bash. Since I know about python, I want to get a linux shell which use python grammar. I searched by google and I found pysh, which is not maintained any more. There's another script named pyshell, which is not likely what I'm searching for.
2
2842
by: Gerard Flanagan | last post by:
Hello, I have a third party shell script which updates multiple environment values, and I want to investigate (and ultimately capture to python) the environment state after the script has run. But running the script as a child process only sets values for that process, which are lost after execution. So I thought I could simply tack on an 'env' command line to the script input lines as shown below. However, using subprocess.Popen gives...
9
14195
by: niteck07 | last post by:
I am using following shell script to ftp files to another server but this is failing as the shell script changes the user name for the ftp login the correct user name is 'ag\invprint' which the script is fetching from a file using grep command but the ftp log says the script is trying to logon as user 'aginvprint' Looks like the script is removing the character '\' when trying to ftp Can someone please help and let me know how to...
3
5446
by: telduivel | last post by:
Can someone please help me with this: I have a python script, that at some point calls a linux bash script (.sh). Starting the shell script is the last thing my python script needs to do, so I would like for the python script to exit once the call has been made, without waiting for the shell script to finish. My question is: how do I call a shell script from a python script? and how do I do that such that control is tranferred to the...
1
2358
by: Svenn Are Bjerkem | last post by:
Hi, as a user on a linux system I am member of the groups "users" and "design" with users as my default group. To controll the accessibility of some parts of the file system, creation of files and directories in those parts must be done with group "design". This is currently done manually with "newgrp design" on the command line before doing anything else. I have been looking for a way to execute this command as a part of a script, but it...
3
3942
by: mmm | last post by:
I am looking for advice on Python Editors and IDEs I have read other posts and threads on the subject and my two questions at this time are mainly about the IDLE-like F5-run facilities. While I am fairly happy using IDLE, the debugger is unintuitive to me and I wanted a project manager and a better variable/ class browser and also the potential to edit/run other languages such as R and Tex/Latex. Windows and LINUX compatibility is...
7
6227
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke this shell script using the Subprocess module. Here is my code: def resultFromRunning_(command):
0
8347
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8792
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8571
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7294
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.