Search for the information at file

I'm having few question.

i'm have a input file.

Other information
CONNECTIONS
  "BP-COLLECTOR"
    J6.4
    "BP-TEST".4;
  +5VS
    C34.1
     U21.1;
  DEV_I2C_SDA
    J6.6
    R4.1
    U18.1;
DEVICES
  "BP-TEST"
    1."BP-LED_ANODE"
    2."BP-LED_CATHODE"
    3."BP-VCC"
    4."BP-COLLECTOR"
    5."BP-EMITTER";
  D1
    1."-5V"
    2.N15483385;
END

fileB

BP-COLLECTOR
DEV_I2C_SDA

will get the information below BP-COLLECTOR until meet ; , but without " and ; symbol at final output.
expected output file:

J6.A1
BP-TEST.4
J6.6
R4.1
U18.1

below is the code i'm try search from internet and able to use, but only work on 1 variable, but i might having 100 + variable from 1 file.

rm -f n2p.temp1
rm -f n2p.temp2
rm -f n2p.temp3
rm -f n2p.temp4
rm -f n2p.temp5
rm -f n2p.temp6

perl -lne "print if/CONNECTIONS$/../DEVICES/" board > n2p.temp1  
sed 's/\"//g' n2p.temp1 > n2p.temp2


perl -lne "print if/BP-COLLECTOR$/../;/" n2p.temp2 >n2p.temp3
sed 's/\;//g' n2p.temp3 > n2p.temp4
sed 's/\ //g' n2p.temp4 > n2p.temp5

sed 1d n2p.temp5 > n2p.temp6

rm -f n2p.temp1
rm -f n2p.temp2
rm -f n2p.temp3
rm -f n2p.temp4
rm -f n2p.temp5

another question is, i'm always use one line perl scrip to solve some of my problem.
example :

perl -lne "print if/BP-COLLECTOR$/../;/" n2p.temp2 >n2p.temp3

is that possible , make the BP-COLLECTOR as a variable that from 1 file ? so i no need do multiple line for all variable to done 1 job.

First off: You can do all that in a single sed and forego the whole script as well as the half dozen temporary files:

sed -n '/CONNECTIONS$/,/DEVICES/ {
             s/"//g
             /BP-COLLECTOR$/,/;/ {
                  s/ //g
                  s/;.*$//p
             }
}' /your/input/file

Test this first if it gives you the correct answer in every case. If you are satisfied with the outcome of this you can put it into loop, like this, which solves your second problem:

while read LINE ; do
     sed -n '/CONNECTIONS$/,/DEVICES/ {
                  s/"//g
                  /'"$LINE"'$/,/;/ {
                       s/ //g
                       s/;.*$//p
                  }
     }' /your/input/file
done < /your/file/with/search-words

I hope this helps.

bakunin

1 Like

Thank bakunin.
for the 1st 1 it only can print out the line with

;

symbol.

exp: only can print out

U21.1
instate of
C34.1
U21.1
sed -n '/CONNECTIONS$/,/DEVICES/ {
             s/"//g
             /+5VS$/,/;/ {
                  s/ //g
                  s/;.*$//p
             }
}' Board

I'm afraid neither your "expected output file" nor the solution proposed meet your verbal specification of what you need. Why, for instance, does the first line read J6.A1 in lieu of J6.4 (taken from the input file)? And, the proposed solution seems to print the first respective line only...?

A small modification to bakunin's proposal seems to come close to the desired output:

while read LINE
  do    sed -n '/CONNECTIONS$/,/DEVICES/        {s/"//g
                                                 /'"$LINE"'$/,/;/       {/'"$LINE"'$/ n
                                                                         s/ //g
                                                                         s/;.*$//
                                                                         p
                                                                        }
                                                }
        ' file1
  done < file2
J6.4
BP-TEST.4
J6.6
R4.1
 U18.1

EDIT: Looping through a 100+ line file, running sed a 100+ times, might not be the most efficient way to do things. How about a single run awk solution? Try

awk '
NR == FNR       {T[$1]
                 next
                }
                {gsub (/[" ]/, _)
                }
$1 in T, /;/    {if ($1 in T) next
                 sub (/;.*$/, _)
                 print
                }
' file2 file1
1 Like

Thank you Rubic,

it work for awk , and faster.

i have another question regrading grep.
after complete the list, i'm need use it as information to

grep

data from each file(>1k file, each file size ~5kB) , it taking huge time to do so.

i using normal grep function.

Variable_data

got around 5 variable,

file_x

is get from list

grep Variable_data file_a > output_file
grep Variable_data file_b >> output_file

Please rephrase.

Im having few varible.
example :

resistor
capacitor
jumper
inductor

i'm need grep data from a list of file.

analog/r1
analog/c3
analog/j1
analog/l2

each file might only have one type only either resistor ,capacitor ,jumper or inductor.

so im need use grep,it westing alot of time to do so. :

grep resistor analog/r1 > outputfile
grep capacitor analog/r1 >>outputfile
grep jumper analog/r1 >>outputfile

Still quite unspecific; a lot to guess left over. Try

grep -Ffvariable_data analog/[a-z][0-9]

thank for answer.

i think should said like this .

grep -F variable data (is from a list of file that declare out which file need grep out)