Loop grep, outputs in files


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Loop grep, outputs in files
# 1  
Old 01-11-2017
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?
# 2  
Old 01-11-2017
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.
# 3  
Old 01-11-2017
Quote:
Originally Posted by echo manolis
.
.
.
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 01-12-2017
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 06:13 AM..
# 5  
Old 01-12-2017
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).
# 6  
Old 01-12-2017
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

This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 01-13-2017
Ohhhhhhhhhhhhhhhh Yessssssssssssssssssssss !!!

It works !!!

Many thanks at all !!!
This User Gave Thanks to echo manolis For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare two files, then outputs line number

I have two files, "ranked.txt" and "sorted.txt". Sorted.txt is a smaller subset from ranked.txt that is sorted in alpha order. However ranked.txt preserves the ranking of words I would like to keep. How do I check the rank of every word in sorted.txt when matched to the original ranked.txt? I... (8 Replies)
Discussion started by: pxalpine
8 Replies

2. Shell Programming and Scripting

suppress some grep outputs

Hello Friends, Im working on Ksh (it is not my will :) ) I would like to get rid off the outputs lines which includes "can't open" .. i guess it must be an easy thing but could not find any usefull thing, ls -l *credit* | xargs grep -i "*data*" 22:set conv(DATA) B16 22:proc... (3 Replies)
Discussion started by: EAGL€
3 Replies

3. Shell Programming and Scripting

Compare two outputs/files based on criterias

Hello, I currently have a script that outputs to a file that contains the output below. It runs every X minutes. I would like to compare the first run against the second but only output if the minutes column is less than its original or if anything else changes. Thanks for the help. Original ... (2 Replies)
Discussion started by: tworkemon
2 Replies

4. Shell Programming and Scripting

How do I number my for loop outputs with this method?

Currently I am outputting users and I want to number them starting with 1... grep name list.txt | awk -F"=" '{ print $2 }' | while read user; do echo -e "1\t|$user" Currently I have: 1 | john 1 | amy 1 | max I want it to look like 1 | john 2 | amy 3 | max (2 Replies)
Discussion started by: etranman1
2 Replies

5. Shell Programming and Scripting

Shell script that will compare two config files and produce 2 outputs 1)actual config file 2)report

Hi I am new to shell scripting. There is a requirement to write a shell script to meet follwing needs.Prompt reply shall be highly appreciated. script that will compare two config files and produce 2 outputs - actual config file and a report indicating changes made. OS :Susi linux ver 10.3. ... (4 Replies)
Discussion started by: muraliinfy04
4 Replies

6. Shell Programming and Scripting

simple join for multiple files and produce 3 outputs

sh script file1 filea fileb filec ................filez. >>output1 & output2 &output3 file1 z10 1873 1920 z_number1_E59 z10 2042 2090 z_number2_E59 Z22 2476 2560 z_number3_E59 Z22 2838 2915 z_number4_E59 z1 1873 1920 z_number1_E60 z1 ... (9 Replies)
Discussion started by: stateperl
9 Replies

7. Shell Programming and Scripting

create outputs from other command outputs

hi friends, The code: i=1 while do filename=`/usr/bin/ls -l| awk '{ print $9}'` echo $filename>>summary.csv #Gives the name of the file stored at column 9 count=`wc -l $filename | awk '{print $1}'` echo $count>>summary.csv #Gives just the count of lines of file "filename" i=`expr... (1 Reply)
Discussion started by: rajsharma
1 Replies

8. UNIX for Dummies Questions & Answers

cat-ing a group of files into two outputs

I have some logic which uses CAT to concatenate group of files, 'SED'-ing some field values and piping the output to one file :- cat `echo $infilename|sed '{ s/.filestream./'${file_stream}'/g s/.field2./'${jobopt2}'/g s/.sb_odate./'${SB_ODATE}'/g }'` >$outfilename ... (1 Reply)
Discussion started by: flyingswan
1 Replies

9. Shell Programming and Scripting

Grep Different Files Using a Loop?

I have a script to GREP for a text expression within certain files, the files being named file.11012008 thru file.11302008. 30 files in all, one for each day of the month. Instead of entering the following 3 lines of code 30 different times, I'm trying to find a way to loop the process: ... (6 Replies)
Discussion started by: foleyml
6 Replies

10. Shell Programming and Scripting

grep and loop files

Hi , 1. I want to grep two or three lines from a set of files and put the grepped lines into again a set of files.like file1-greppedfile1 file2-greppedfile2 then again do some format to the grepped files with sed or awk then create another set of files. How can I do this in loop? 2.I... (4 Replies)
Discussion started by: kashik
4 Replies
Login or Register to Ask a Question