Unix/Linux Go Back    


UNIX for Beginners Questions & Answers If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

Loop grep, outputs in files

UNIX for Beginners Questions & Answers


Tags
files, grep, loop, solved, words

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago
echo manolis echo manolis is offline
Registered User
 
Join Date: Nov 2016
Last Activity: 13 January 2017, 11:02 AM EST
Posts: 18
Thanks: 6
Thanked 0 Times in 0 Posts
Loop grep, outputs in files

Hi, I try to create a simply code but I have problems...


Code:
Ubuntum, Bash version: 4.3.46 Bash


Code:
INPUT file (csv):
TS133_29BC,xx2274305
TS133_29BC,rps4576240
av137_37BC,wfs2274305
av137_37BC,ftrs4576240
T1S138_30BC,rfss2255526
T1S138_30BC,rgas2274305


Code:
OUTPUT files (csv):
File TS133_29BC
TS133_29BC,xx2274305
TS133_29BC,rps4576240

File av137_37BC
av137_37BC,wfs2274305
av137_37BC,ftrs4576240

File T1S138_30BC
T1S138_30BC,rfss2255526
T1S138_30BC,rgas2274305

My code is:


Code:
for ((b=$1; b<=96; b++))

    do

dddd=*"_"${b}"BC"

grep -w "$dddd" > $dddd

    done

find . -size 0 -delete

Where is the error?
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 20 January 2017, 5:46 PM EST
Location: Saskatchewan
Posts: 21,780
Thanks: 1,027
Thanked 4,038 Times in 3,746 Posts

Code:
for((b=$1 ...

$1 is not the numeral one, but the script's first commandline parameter. Try it without the dollar sign.

Are you trying to simultaneously read from and write to the same file? That won't work, that will destroy your data.
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 January 2017, 8:14 AM EST
Location: Aachen, Germany
Posts: 10,156
Thanks: 230
Thanked 3,073 Times in 2,849 Posts
Quote:
Originally Posted by echo manolis View Post
.
.
.
Where is the error?
WHAT is the error?

Some comments:
- There is no file that grep can work upon.
- In regexes, the star * is a repetition indicator for the preceding atom (man regex) which you do not specify when defining dddd.
- You seem to grep for a structure like _nnBC. Will nn always be two digits, also below 10, e.g. 03? Then, extra care must be taken. Or do you start from the first positional parameter ($1)?
    #4  
Old Unix and Linux 1 Week Ago
echo manolis echo manolis is offline
Registered User
 
Join Date: Nov 2016
Last Activity: 13 January 2017, 11:02 AM EST
Posts: 18
Thanks: 6
Thanked 0 Times in 0 Posts
Hi!

I delete $ ... from $1 to 1
I add the input file name (inputfile)

new version of the code...


Code:
for ((b=1; b<=96; b++))      do  dddd=*"_"${b}"BC"  grep -w "$dddd" inputfile > $dddd      done

The variable "b" start from 1 and finisch to 96 ... no 01,02...

I take a look in the "man regex" and I found that:

Code:
$ man regex

REG_BADRPT
              Invalid use of repetition operators such as using '*' as the first character.

How can I build my variable dddd?

dddd=*"_"${b}"BC :doesn't work... How can I replace "*" with an other simbol with the same function...?


I found:

Code:
. Matches any single character
? The preceding item is optional and will be matched, at most, once
* The preceding item will be matched zero or more times.

My variable would also be:
Code:
dddd=....."_"${b}"BC"

... ehm!?

I'm confuse!

Last edited by echo manolis; 1 Week Ago at 06:13 AM..
Sponsored Links
    #5  
Old Unix and Linux 1 Week Ago
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 January 2017, 8:14 AM EST
Location: Aachen, Germany
Posts: 10,156
Thanks: 230
Thanked 3,073 Times in 2,849 Posts
You don't need the * . As the regex is not anchored (at begin-of-line), grep will match anywhere in the string. For the leading zeroes, try either a printf "%02d", or for b in {01..96} (recent shells only).
Sponsored Links
    #6  
Old Unix and Linux 1 Week Ago
MadeInGermany MadeInGermany is online now Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 21 January 2017, 8:21 AM EST
Location: Simplicity
Posts: 3,367
Thanks: 259
Thanked 1,103 Times in 999 Posts
Another bash-4 construct gives you two digit values

Code:
echo {01..96}


Code:
for b in {01..96}
do
  ...
done

Maybe you want to cycle through all the numbers in the file?
And is it sorted?
Then consider this one

Code:
while IFS=, read left right
do
  if [[ $left == *[0-9][0-9]BC ]]
  then
    if [[ $left != $prev ]]
    then
      exec 3> "$left".csv
      prev=$left
    fi
    echo "$left,$right" >&3
  fi
done < inputfile.csv

The Following User Says Thank You to MadeInGermany For This Useful Post:
echo manolis (1 Week Ago)
Sponsored Links
    #7  
Old Unix and Linux 1 Week Ago
echo manolis echo manolis is offline
Registered User
 
Join Date: Nov 2016
Last Activity: 13 January 2017, 11:02 AM EST
Posts: 18
Thanks: 6
Thanked 0 Times in 0 Posts
Ohhhhhhhhhhhhhhhh Yessssssssssssssssssssss !!!

It works !!!

Many thanks at all !!!
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
suppress some grep outputs EAGL€ Shell Programming and Scripting 3 05-09-2012 01:41 PM
How do I number my for loop outputs with this method? etranman1 Shell Programming and Scripting 2 08-02-2011 11:59 AM
create outputs from other command outputs rajsharma Shell Programming and Scripting 1 07-14-2010 08:43 AM
Grep Different Files Using a Loop? foleyml Shell Programming and Scripting 6 01-02-2009 01:41 AM
grep and loop files kashik Shell Programming and Scripting 4 06-16-2008 09:41 AM



All times are GMT -4. The time now is 09:23 AM.