Re: Bash/grep -e filtering quickie question
From: The Loeki (the.loeki_at_gmail.com)
Date: 05/02/05
- Previous message: dan.colascione_at_gmail.com: "Bash function advice library"
- Next in thread: Ed Morton: "Re: Bash/grep -e filtering quickie question"
- Reply: Ed Morton: "Re: Bash/grep -e filtering quickie question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 2 May 2005 02:02:52 -0700
Logan Shaw wrote:
> The Loeki wrote:
> > Say I've got this:
> > foo;bar;12;foobar
> I assume by "got this" you mean that you've got it in a file. As
> opposed to as input to the shell, which it would be syntactically
> valid for!
> > The 12 are a variable number of figures.
> OK, so the third field in your file is a string of digits.
Sorry 'bout that. I indeed have 2 lists, and I wish to parse one field
of one list with all the digits in one field of the other, and output
it to file no.3
Or, to put it into perspective:
I've got a list of people who don't work on certain days (e.g.MOndays
and Tuesdays, 1 and 2) and I wish to parse my generated list of dates
with it.
> > I want another list to be filtered with all the numbers
individually,
> > as in grep -e"$(echo "^1" ; echo "^2")" (or something).
> The first task is to extract that field from the file. That will
> look something like this:
> linewithdigits=`cat file` # or something else if it's
multiline
> digitstr=`echo $linewithdigits | cut -d';' -f3`
I'll have to look that cut command up, it looks cuhl (I've been
massively awk-ing)
> Now you need to split digitstr up into individual digits. I don't
> know of a really clean way to do this, but one possible way is to
> use sed to insert spaces, then allow the shell to split it up into
> separate digits:
> for digit in `echo "$digitstr" | sed -e 's/\(.\)/\1 /g'`
> do
> grep "^$digit" whatever
> done
the sed command was about what I was looking for. I'll be testing it
and if it looks good enough, I'll use it!
For now, I've solved it like this:
partt=$(echo $i | awk -F";" '{ print $2 }')
if [[ ! -z $partt ]]
then if [[ ${#partt} == 1 ]]
then grep -v "^$partt," /tmp/list1 > /tmp/d$iteration
else /usr/xpg4/bin/grep -v -e"$(IFS=' ' ; for wd in $(echo $i |
awk -F";" '{ print $2 }') ; do echo "^$wd," ; done)" /tmp/list1 >
/tmp/d$iteration
fi
else cp /tmp/list1 /tmp/d$iteration
fi
This works well, however, due to the IFS (which, IMHO, is one of thé
single cuhlest variables in the book) variable, only in Solaris. And as
you can see by the IFS=' ', I've had to manually adapt the field spec
to be in the order of
foo;bar;1 2;foobar
> However, your message is a little unclear about what you want to do.
> I'm not sure if you want to do a separate grep for each digit or
> you want to do a single grep for all the digits at once. If you
> want to do a single grep for all the lines that, say, begin with
> "1", "2", or "3", you can just transform the digit string into a
> grep pattern and avoid the loop and the splitting and all that:
> digitstr=`echo $linewithdigits | cut -d';' -f3`
> pattern="^[$digitstr]"
> grep "$pattern" whatever
That's about right
Ed Morton wrote:
> awk -F\; 'NF==FNR{m=m s $3;s="|"}$0 ~ m' file1 file2
>ITIM:
>awk -F\; 'NF==FNR{m=m s $3;s="|";next}$0 ~ m' file1 file2
I can't get it to filter. It just passes everything through.
- Previous message: dan.colascione_at_gmail.com: "Bash function advice library"
- Next in thread: Ed Morton: "Re: Bash/grep -e filtering quickie question"
- Reply: Ed Morton: "Re: Bash/grep -e filtering quickie question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|