Number of words in line, while loop, search and grep
Hello,
What I wish to attain is:
- to readfileA line by line
- search entire line as string in fileB
- when found, grep the next line in fileB
- then merge "searched line" and "found line" in a new file, fileC
Here is my fileA:
Code:
T S Eliot
J L Borges
L Aragon
L L Aragon
T S Eliot 4 0 3333
L L Aragon 2
J L Borges 5
FileB:
Code:
0290202092090
J F Kennedy America Brookline
+92999929999990
Abraham Lincoln Hodgenville
+2828288889999
L Aragon
+330000000000
Tinto Brass Milano Italy
+330022110033
J L Borges
+440011223344
T S Eliot
Expected Output, fileC:
Code:
T S Eliot
+440011223344
J L Borges
+330022110033
L Aragon
+2828288889999
This is what I tried:
Code:
while read line
do
count=`echo $line | wc -w`
if [ $count -eq 6 ]; then
grep "$COL1 $COL2 $COL3 $COL4 $COL5 $COL6" -B1 fileB
if [ $count -eq 5 ]; then
grep "$COL1 $COL2 $COL3 $COL4 $COL5" -B1 fileB
if [ $count -eq 4 ]; then
grep "$COL1 $COL2 $COL3 $COL4" -B1 fileB
if [ $count -eq 3 ]; then
grep "$COL1 $COL2 $COL3" -B1 fileB
if [ $count -eq 2 ]; then
break;
fi
done < fileA > fileC
It gives syntax error near unexpected token `done'
PS: I just set count variable limit to 6, not sure how to set to max word nr.
Hello Don,
Thank you, I am under ubuntu 18.04 bionic.
I edited upon your notification. Now it gives empty result.
Code:
while read line
do
count=`echo $line | wc -w`
if [ $count -eq 6 ]; then
grep "$COL1 $COL2 $COL3 $COL4 $COL5 $COL6" -B1 fileB
else
if [ $count -eq 5 ]; then
grep "$COL1 $COL2 $COL3 $COL4 $COL5" -B1 fileB
else
if [ $count -eq 4 ]; then
grep "$COL1 $COL2 $COL3 $COL4" -B1 fileB
else
if [ $count -eq 3 ]; then
grep "$COL1 $COL2 $COL3" -B1 fileB
else
if [ $count -eq 2 ]; then
break;
fi
fi
fi
fi
fi
done < fileA > fileC
Well that's just a clutter of GNU greps in broken loops to be sincere.
Assumptions are made that line with number always start with number or a + sign.
Give this a shot :
Code:
awk 'FNR==NR { person[$0]; next } /^[0-9]|+/ { number=$0 } $0 in person { printf ("%s\n%s\n", $0, number) } ' fileA fileB > fileC
Try to use awk for such assignments, it will prove irreplaceable and fast.
Also baris35 after 200 posts, a more sane approach to coding and asking questions would be advised.
Looks like you didn't even read the questions from other posters, just added 5 if's without any consideration / thinking or reading about it.
They could have just as well pasted an awk line to solve your problem (probably better then mine ), but we are here to try to make you think about a problem yourself.
Hope that helps.
Regards
Peasant.
These 2 Users Gave Thanks to Peasant For This Post:
Dear Peasant,
Thanks but that code gives empty file at my end. The reason is most likely related to your assumption + sign etc...
Regarding 5x if loop, you are right. I did not like it but I excerpted that code from another thread since it was the most similar one to my case.
Anyway,
Thank You All for your time. I will try to find a different way to sort it out.
I'm in need of some kind of script that will search for a string in each logfile in a directory but we don't want to use GREP. GREP seems to use up to much of our memory causing the server to use up a lot of swap space. Our log files are bigger than 500M on a daily basis. We lately started... (8 Replies)
Hi All
I have to search servers name say like 1000+ "unique names" line by line in child.txt files in another file that is a master file where all server present say "master.txt",if child.txt's server name matches with master files then it print yes else no with server name. (4 Replies)
Hi,
Need your help for this scripting issue I have. I am not really good at this, so seeking your help.
I have a file looking similar to this:
Hello, i am human and name=ABCD.
How are you?
Hello, i am human and name=PQRS.
I am good.
Hello, i am human and name=ABCD.
Good bye.
Hello, i... (12 Replies)
I have a text file ( basically a log file) and i have 2 words (alpha, beta),
Now i want to search these two words in one line and then print next 15 lines in a temp file. there would be many lines with alpha and beta But I need only last occurrence with "alpha" and "beta" and next 15 lines.
... (4 Replies)
Hi,
Please suggest a way to print number of words in the end of each line.
<input file>
red aunt house
blue sky
bat and ball game
<output file>
red aunt house 3
blue sky 2
bat and ball game 4
Thanks! (2 Replies)
I have a file that has multiple lines separated by an asterisk as a delimiter:
FILE.txt
A*123*BCD*456*TOM
A*789*EFG*947*CHRIS
A*840*BCD*456*TOM
I would like to search multiple files for the strings 'BCD' AND 'TOM' and return the number of lines, per file, that match these two reg... (2 Replies)
Hi frnds
i want to desplay file names that should be word1 and word2
ex :
i have 10 *.log files
5 files having word1 and word2
5 files having only word1,
i have used below command
egrep -l 'word1|word2' *.log
its giving all 10 files, but i want to display only 5... (20 Replies)
I have some txt files. I have to create another text file which contains the portion starting from the format "Date Sex Address" to the end of the file. While using grep -n on Date it also gives me the previous line containg Date. and also Date may be DATE in some files.
My file is like this... (10 Replies)
Hi all,
Please help me in the following dbt
i want to know the different files in the current and the sub directory which have some search key in that . for example
i want to know all filenames followed by the word 'unix' in all files.
the file name and the matching word have... (1 Reply)
I want to search a heap of files but using an either OR or AND condition for two or more strings. How can I do this?
i.e. file1 has the following
file testfile primary
and file2 has this ...
file testfile2 secondary
If I use this ...
find . -type f -exec grep "testfile" {}... (2 Replies)