Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I need some help here. I need to check in the folder if contains file name starting with either "-" or " "(space) and move it to bad_file folder. I did a script to move "-", works fine. But now to modify it to add in to check for starting with space, i donno how to do "or" function with grep.
can anyone help me?
my script for "-":
BAD_SUM_CNT=`rsh emsun2 "ls /export/home/db_inputs/nt3_ws/ | grep ^\- | wc -l" `
if [ $BAD_SUM_CNT -gt 0 ];then
printf "\n" >> $FILE_LOG
BAD_SUM=`rsh emsun2 "ls -l /export/home/db_inputs/nt3_ws/-*"`
echo "$BAD_SUM" >> $FILE_LOG
rsh emsun2 "mv /export/home/db_inputs/nt3_ws/-* /export/home/db_inputs/nt3_ws/bad_summary"
echo "The above $BAD_SUM_CNT summaries are moved to bad_summary folder" >> $FILE_LOG
else
echo "No Summaries with bad file name ">> $FILE_LOG
fi
I need some help here. I need to check in the folder if contains file name starting with either "-" or " "(space) and move it to bad_file folder. I did a script to move "-", works fine. But now to modify it to add in to check for starting with space, i donno how to do "or" function with grep.
can anyone help me?
my script for "-":
BAD_SUM_CNT=`rsh emsun2 "ls /export/home/db_inputs/nt3_ws/ | grep ^\- | wc -l" `
if [ $BAD_SUM_CNT -gt 0 ];then
printf "\n" >> $FILE_LOG
BAD_SUM=`rsh emsun2 "ls -l /export/home/db_inputs/nt3_ws/-*"`
echo "$BAD_SUM" >> $FILE_LOG
rsh emsun2 "mv /export/home/db_inputs/nt3_ws/-* /export/home/db_inputs/nt3_ws/bad_summary"
echo "The above $BAD_SUM_CNT summaries are moved to bad_summary folder" >> $FILE_LOG
else
echo "No Summaries with bad file name ">> $FILE_LOG
fi
Thanks,
Use:
Code:
grep -P "^( |-)"
Specifying "-P" tells grep to use Perl-style regular expressions, which are pretty advanced.
Thanks,
I can use the option in linux but in unix, i have below error:
emsun2[mbayse]% ls /export/home/db_inputs/nt3_ws/logfile_error | grep -P "^( |-)"
grep: illegal option -- P
Usage: grep -hblcnsviw pattern file . . .
I need to remote from linux to unix server,which is:
SunOS emsun2 5.6 Generic_105181-33 sun4u sparc SUNW,Ultra-4
any help?
is it i need to install perl?
another in the script is i need to "ls" the file starting with space, i can do [ ls /export/.../" "* ] on local , but in script when it does the rsh, it having error:
rsh emsun8 "ls -l /export/home/db_inputs/nt3_ft/" "*"
makes it list down the /export/home/db_inputs/nt3_ft/ and current dir.
It looks to me like gnu grep supports perl regex, but your unix one doesn't. gnu tools often have more features than other unix counterparts.
Personally, I'd avoid using grep altogether and restructure the code to do the matching entirely within the shell itself using its built-in pattern matching ability.
First of all, are you using bash, or another shell? And what version? I tried to avoid anything particularly bash-specific in the code below, but there might be some things that don't translate well into other shells.
I moved the line testing into a function that uses a case statement to generate a list of bad filenames. I've found that it's usually easier to store your filenames in a list before you start moving things around. Apologies in advance if I misunderstood your intentions in the code.
Code:
#!/bin/bash
IFS='
'
function list_bad_files(){
for file in `rsh emsun2 "ls /export/home/db_inputs/nt3_ws/"`; do
case `basename -- $file` in
# use '--' so that files beginning with '-' aren't seen as options
-*) echo "$file" ;;
\ *) echo "$file" ;;
esac
done
}
BAD_SUM_FILES=`list_bad_files`
BAD_SUM_CNT=`echo "$BAD_SUM_FILES"|wc -l`
if [ "$BAD_SUM_FILES" ];then
printf "\n" >> $FILE_LOG
for file in $BAD_SUM_FILES; do
echo "$file" >>$FILE_LOG
rsh emsun2 "mv -- "$file" /export/home/db_inputs/nt3_ws/bad_summary"
#edit: we need "--" here too.
echo "The above $BAD_SUM_CNT summaries are moved to bad_summary folder" >> $FILE_LOG
done
else
echo "No Summaries with bad file name ">> $FILE_LOG
fi
exit 0
Note: I had a difficult time testing this. Not having anything to rsh into, I had to use a local folder. But the files with spaces at the beginning weren't handled correctly until I changed the IFS to newline only. I don't know if this would be an issue for you. I usually change IFS any time a filename could contain spaces anyway--it's just easier that way.
Note also that bash recommends using $() instead of ` backticks for embedded commands. But again I purposely avoided it in the above code.
Last edited by David the H.; 10-13-2009 at 07:46 AM.
I need to remote from linux to unix server,which is:
SunOS emsun2 5.6 Generic_105181-33 sun4u sparc SUNW,Ultra-4
Okay, understood. Maybe a Sun OS newsgroup would be a better choice? You have noticed, haven't you, that this is a Linux discussion group? People are going to have a very difficult time offering something that works without testing it on an OS they don't have.
Thanks for your feedback. My late reply due to i'm working as internal customer support 24/7.
First thing, the answers you all gave me an idea why i need to do at unix. so what i did is i mount the unix drives straight to linux, and guess what, the command " ls | grep -P "^( |-)" works perfectly.
now to enhance the script, im facing problem:
all the files that have "-" and "space" that moved to bad_summary folder, need to cp out removing - and space in the file name.
I tried:
linux1[simransab]% ls
-01_SUMMARY_REPORT_ETS030531_10142009.txt
-842150451_ETS171528_10012009.log
linux1[simransab]% cp ./-* ./*
cp: copying multiple files, but last argument `./-842150451_ETS171528_10012009.log' is not a directory
On david code, at this moment i have try out yet as the original script was already running from last week but need urgent modification to get the data. I will try it as a enhancement as soon as possible.
Last edited by simransab; 10-14-2009 at 07:43 PM.
Reason: to prevent misunderstandg :need to cp out removing - and space in the file name
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.