Search for the information at file


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Search for the information at file
# 1  
Old 09-13-2018
Search for the information at file

I'm having few question.

i'm have a input file.

Code:
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
Code:
BP-COLLECTOR
DEV_I2C_SDA


will get the information below BP-COLLECTOR until meet ; , but without " and ; symbol at final output.
expected output file:
Code:
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.
Code:
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 :
Code:
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.




Moderator's Comments:
Mod Comment Please use CODE tags consistently as required by forum rules!

Last edited by RudiC; 09-13-2018 at 06:29 AM.. Reason: Added some CODE tags.
# 2  
Old 09-13-2018
Quote:
Originally Posted by kttan
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.
Code:
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

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

Code:
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:

Code:
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
This User Gave Thanks to bakunin For This Post:
# 3  
Old 09-13-2018
Quote:
Originally Posted by bakunin
First off: You can do all that in a single sed and forego the whole script as well as the half dozen temporary files:

Code:
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:

Code:
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

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

symbol.

exp: only can print out
Code:
U21.1
instate of
C34.1
U21.1

Code:
sed -n '/CONNECTIONS$/,/DEVICES/ {
             s/"//g
             /+5VS$/,/;/ {
                  s/ //g
                  s/;.*$//p
             }
}' Board


Last edited by kttan; 09-13-2018 at 06:37 AM..
# 4  
Old 09-13-2018
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:
Code:
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
Code:
awk '
NR == FNR       {T[$1]
                 next
                }
                {gsub (/[" ]/, _)
                }
$1 in T, /;/    {if ($1 in T) next
                 sub (/;.*$/, _)
                 print
                }
' file2 file1


Last edited by RudiC; 09-13-2018 at 07:30 AM..
This User Gave Thanks to RudiC For This Post:
# 5  
Old 09-14-2018
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
Code:
grep

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

i using normal grep function.

Code:
Variable_data

got around 5 variable,
Code:
file_x

is get from list


Code:
grep Variable_data file_a > output_file
grep Variable_data file_b >> output_file

# 6  
Old 09-14-2018
Please rephrase.
# 7  
Old 09-24-2018
Quote:
Originally Posted by RudiC
Please rephrase.

Im having few varible.
example :
Code:
resistor
capacitor
jumper
inductor

i'm need grep data from a list of file.
Code:
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. :
Code:
grep resistor analog/r1 > outputfile
grep capacitor analog/r1 >>outputfile
grep jumper analog/r1 >>outputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

2. UNIX for Dummies Questions & Answers

Search file and print everything except multiple search terms

I'm trying to find a way to search a range of similar words in a file. I tried using sed but can't get it right:sed 's/\(ca01\)*//'It only removes "ca01" but leaves the rest of the word. I still want the rest of the information on the lines just not these specific words listed below. Any... (3 Replies)
Discussion started by: seekryts15
3 Replies

3. Shell Programming and Scripting

Retrieving the relevant search from search file in the main file

I have two files: file 1: hello.com neo.com,japan.com,example.com news.net xyz.com, telecom.net, highlands.net, software.com example2.com earth.net, abc.gov.uk file 2: neo.com example.com abc.gov.uk file 2 are the search keys to search in file 1 if any of the search... (7 Replies)
Discussion started by: csim_mohan
7 Replies

4. UNIX for Dummies Questions & Answers

Obtaining File information based on String Search

Is there a single Command in Unix to get the following Information when searching for files containing one or more strings in a Unix Directory (including sub directories within it) : 1) Complete filename ( path and filename) 2) Owner of the file 3) Size of the file 4) Last Modified date... (3 Replies)
Discussion started by: pchegoor
3 Replies

5. UNIX for Dummies Questions & Answers

Grep? - using a file of terms to search another file when the information is on a different line

I have a flat file that looks like this, let's call it Chromosome_9.txt: FT /Gene_Name="Guanyl-Acetylase 9" FT /Gene_Number"36952" FT /Gene_Name="Endoplasmic Luciferase" FT /Gene_Number"36953" FT ... (4 Replies)
Discussion started by: Twinklefingers
4 Replies

6. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

7. Shell Programming and Scripting

search information in multiple files and save in new files

hi everyone, im stuck in here with shell :) can you help me?? i have a directory with alot files (genbank files ... all ended in .gbk ) more than 1000 for sure ... and i want to read each one of them and search for some information and if i found the right one i save in new file with new... (6 Replies)
Discussion started by: andreia
6 Replies

8. Shell Programming and Scripting

Create shell script to extract unique information from one file to a new file.

Hi to all, I got this content/pattern from file http.log.20110808.gz mail1 httpd: Account Notice: close igchung@abc.com 2011/8/7 7:37:36 0:00:03 0 0 1 mail1 httpd: Account Information: login sastria9@abc.com proxy sid=gFp4DLm5HnU mail1 httpd: Account Notice: close sastria9@abc.com... (16 Replies)
Discussion started by: Mr_47
16 Replies

9. Shell Programming and Scripting

sed help - search/copy from one file and search/paste to another

I am a newbie and would like some help with the following - Trying to search fileA for a string similar to - AS11000022010 30.4 31.7 43.7 53.8 60.5 71.1 75.2 74.7 66.9 56.6 42.7 32.5 53.3 I then want to replace that string with a string from fileB - ... (5 Replies)
Discussion started by: ncwxpanther
5 Replies

10. Shell Programming and Scripting

sed search and read the RHS information: Linux 2.6.9-89

Hi, I have a config_file.cfg with the content: FILE_ID_1=1 FILE_FTP_ID_1=<FTP_SERVER1.COM> .... FILE_ID_2=2 FILE_FTP_ID_2=<FTP_SERVER2.COM> .... so on for 28 times. As you might have guessed it; the script I have to write is to read this config file and get the FTP server... (3 Replies)
Discussion started by: dips_ag
3 Replies
Login or Register to Ask a Question