UNIX command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX command
# 1  
Old 06-02-2015
UNIX command

From the below list of the files I need to delete the files where any file name greater than having M1400.

I can use _ as splitter. but not sure what is the exact command.

Code:
/files/US> ls -ltr
 
A8399_M1426
A3950_M1374
A3793_M1478
A3793_M1374
A3793_W1374
HA3793_M1426
IA3752_M1426


Last edited by ramkumar15; 06-02-2015 at 08:34 AM..
# 2  
Old 06-02-2015
Hello Ramkumar15,

Could you please try following and let me know if this helps.
Code:
cat script.ksh
THD=1400
for FILE in *_M*
do
 val=${A##*_M}
 if [[ $val -gt $THD ]]
 then
  echo "Deleting file named " $FILE
  ## rm $FILE
 fi
done

If happy with results then you can remove comment before rm. Hope this helps.


Thanks,
R. Singh

Last edited by RavinderSingh13; 06-02-2015 at 09:02 AM..
# 3  
Old 06-02-2015
thanks for your response ravi.

I am not sure how this value sets

Code:
val=${A##*_M}

I am receiving a bad number below when I executed the script.

Code:
house.sh[4]: val: bad number

# 4  
Old 06-02-2015
Hello Ramkumar15,

Please always mention your os details it will help us to guide you more, anyways my command in POST#2 works fine for me, I am using bash.
Code:
touch A8399_M1426
THD=1400
for FILE in *_M*
do
 val=${FILE##*_M}
 if [[ $val -gt $THD ]]
 then
  echo "Deleting file named " $FILE
 fi
done
 
Deleting file named  A8399_M1426

As you can see above I have created a test file and then checked the code and it worked fine for me, you could try following too.
Code:
for FILE in *_M*
do
 val=`echo $FILE | awk '{sub(/.*_M/,X,$0);print}'`
 if [[ $val -gt $THD ]]
 then
  echo "Deleting file named " $FILE
 fi
done
Deleting file named  A8399_M1426

on a Solaris/SunOS system, change awkto /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk.

Thanks,
R. Singh

Last edited by RavinderSingh13; 06-02-2015 at 09:46 AM.. Reason: Added variable named FILE thanks to clx
# 5  
Old 06-02-2015
Quote:
Originally Posted by RavinderSingh13
Code:
for FILE in *_M*
do
 val=${A##*_M}

It must be referenced by the iterator variable FILE.
# 6  
Old 06-02-2015
hi ravinder,
its works now.
sorry for not mentioning the OS details.

can you please explain what the below syntax does.

Code:
 awk '{sub(/.*_W/,X,$1);print}'

# 7  
Old 06-02-2015
Quote:
Originally Posted by ramkumar15
hi ravinder,
its works now.
sorry for not mentioning the OS details.

can you please explain what the below syntax does.

Code:
 awk '{sub(/.*_W/,X,$1);print}'

Note that the code RavinderSingh13 provided had an "M" not a "W" in the substitution search pattern (as you requested). But, what that awk script does is to find the first occurrence of the longest string in the 1st field ($1) on an input line ending with the two characters "_" and "W" and, if a match was found, replace that string with the contents of the awk variable X. Since X has not been initialized in this script, it defaults to an empty string. Then, whether or not a change was made, print prints the line.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX command to copy files from Windows to UNIX box

Hi Folks, I have a file name abc.xml in my windows machine at the location c:\ytr\abc.xml which I want to place at the unix box machine inside cde directory.. at the following location that is /opt/app/cde/ now the credentials of unix box are abc345 -->(dummyid) ftyiu88--->(dummy passwd) ... (4 Replies)
Discussion started by: punpun66
4 Replies

2. Shell Programming and Scripting

Specify an entire UNIX command as a command line argument

I'm trying to write a bash script called YN that looks like the following YN "Specify a question" "doThis" "doThat" where "doThis" will be executed if the answer is "y", otherwise "doThat". For example YN "Do you want to list the file dog?" "ls -al dog" "" Here's my attempt... (3 Replies)
Discussion started by: LeoKSimon
3 Replies

3. UNIX Desktop Questions & Answers

Can Unix access Windows' File through Command Prompt in Unix

Hi all, I wish to know whether Unix can access window's file in Unix's terminal? Apart from that, how to copy files or share files between Window and Unix? I get to know of secure copy, however, my company's Unix does not support the feature of secure copy? Any other method for me to share/... (5 Replies)
Discussion started by: jessy83
5 Replies

4. UNIX for Dummies Questions & Answers

New to Unix command line and have a question about the "sort" command

I am going through the Unix Made Easy second edition book by John Muster. So far it's been very informative and I can tell it may be a bit out of date. In one of the exercises it talks about the "sort" command and using it to sort column's of data etc. The "sort" command has changed a bit and... (1 Reply)
Discussion started by: budfoxcat
1 Replies

5. UNIX for Advanced & Expert Users

unix command : how to insert text at the cursor location via command line?

Hi, Well my title isn't very clear I think. So to understand my goal: I have a script "test1" #!/bin/bash xvkbd -text blabla with xbindkeys, I bind F5 key in order it runs my test1 script So when I press F5, test1 runs. I'm under Emacs/Vi and I press F5 in order to have "blabla" be... (0 Replies)
Discussion started by: xib.be
0 Replies

6. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

7. HP-UX

How to use more than one MPE command STREAM with Unix command in a single shell?

Hello, I have problem in writing the shell script involving MPE command STREAM related to HP-UX and Unix command. Script is sh "nlshCMD 'STREAM <job name1>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name2>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name3>' |... (1 Reply)
Discussion started by: bosskr
1 Replies

8. Shell Programming and Scripting

How to use more than one MPE command STREAM with Unix command in a single shell?

Hello, I have problem in writing the shell script involving MPE command STREAM related to HP-UX and Unix command. Script is sh "nlshCMD 'STREAM <job name1>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name2>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name3>' |... (0 Replies)
Discussion started by: bosskr
0 Replies

9. UNIX for Dummies Questions & Answers

wc command help.... and other unix command....... urgent

hi all. thanks for looking i am doing some homework. one question is that when type wc and then how to tell the program that we have finished entering data? also why do some operating systems report 22 as the number of bytes in the file above, while others only 20? thanks so much,... (1 Reply)
Discussion started by: dashi2k
1 Replies
Login or Register to Ask a Question