LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 10-12-2009, 01:24 AM   #1
simransab
LQ Newbie
 
Registered: Sep 2009
Posts: 23

Rep: Reputation: 15
scripting: how to grep either a or b?


Hi ,

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,
 
Old 10-12-2009, 02:11 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by simransab View Post
Hi ,

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.
 
Old 10-13-2009, 04:12 AM   #3
simransab
LQ Newbie
 
Registered: Sep 2009
Posts: 23

Original Poster
Rep: Reputation: 15
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.

Need urgent advice.

Thanks.
 
Old 10-13-2009, 04:16 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681
Read the manpage for grep that exists on the Unix host. You could probably search for '^[ -]'.
 
Old 10-13-2009, 06:27 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 10-13-2009, 11:54 AM   #6
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by simransab View Post
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.
 
Old 10-14-2009, 03:10 AM   #7
simransab
LQ Newbie
 
Registered: Sep 2009
Posts: 23

Original Poster
Rep: Reputation: 15
hi all,

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
 
Old 10-14-2009, 03:30 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
precede your copy command's arguments with --
Code:
# cp -- -* /destination
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting question with find and grep and .bashrc - a multi year problem brfindla Linux - Software 9 08-24-2009 09:54 AM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
LXer: Using Grep To Streamline Your Shell And Command Line Scripting LXer Syndicated Linux News 0 08-08-2008 07:11 AM
perl scripting using grep gazman1 Programming 9 06-09-2006 01:49 PM
line addressing with grep (shell scripting) j2dizzo Linux - General 13 03-03-2004 09:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration