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

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-11-2017
echo manolis echo manolis is offline
Registered User
 
Join Date: Nov 2016
Last Activity: 6 March 2017, 10:55 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 01-11-2017
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 21 July 2017, 2:59 PM EDT
Location: Saskatchewan
Posts: 22,291
Thanks: 1,110
Thanked 4,204 Times in 3,888 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 01-11-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 July 2017, 7:00 AM EDT
Location: Aachen, Germany
Posts: 11,057
Thanks: 284
Thanked 3,408 Times in 3,138 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 01-12-2017
echo manolis echo manolis is offline
Registered User
 
Join Date: Nov 2016
Last Activity: 6 March 2017, 10:55 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; 01-12-2017 at 05:13 AM..
Sponsored Links
    #5  
Old Unix and Linux 01-12-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 July 2017, 7:00 AM EDT
Location: Aachen, Germany
Posts: 11,057
Thanks: 284
Thanked 3,408 Times in 3,138 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 01-12-2017
MadeInGermany MadeInGermany is online now Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 23 July 2017, 4:12 AM EDT
Location: Simplicity
Posts: 3,636
Thanks: 285
Thanked 1,217 Times in 1,100 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 (01-13-2017)
Sponsored Links
    #7  
Old Unix and Linux 01-13-2017
echo manolis echo manolis is offline
Registered User
 
Join Date: Nov 2016
Last Activity: 6 March 2017, 10:55 AM EST
Posts: 18
Thanks: 6
Thanked 0 Times in 0 Posts
Ohhhhhhhhhhhhhhhh Yessssssssssssssssssssss !!!

It works !!!

Many thanks at all !!!
Sponsored Links
Closed

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 12:41 PM
How do I number my for loop outputs with this method? etranman1 Shell Programming and Scripting 2 08-02-2011 10:59 AM
create outputs from other command outputs rajsharma Shell Programming and Scripting 1 07-14-2010 07:43 AM
Grep Different Files Using a Loop? foleyml Shell Programming and Scripting 6 01-02-2009 12:41 AM
grep and loop files kashik Shell Programming and Scripting 4 06-16-2008 08:41 AM



All times are GMT -4. The time now is 04:40 AM.