Number of words in line, while loop, search and grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Number of words in line, while loop, search and grep
# 8  
Old 09-25-2018
Well, a representative input should be given then.
A smallest possible portion of your data which cover your request entirely.

Without that, it is not possible for person here to cover all cases magically.

Regards
Peasant.
# 9  
Old 09-25-2018
Quote:
Originally Posted by Peasant
Well, a representative input should be given then.
A smallest possible portion of your data which cover your request entirely.

Without that, it is not possible for person here to cover all cases magically.

Regards
Peasant.

Thank you Peasant

Regards
Boris
# 10  
Old 09-25-2018
Instead of "else if" and nesting deeper the shell has "elif" that remains on the same level.
Code:
if ... then ... elif ... then ... elif ... then ... else ... fi

But if each condition tests the same variable then it is more appropriate to use a "case-esac":
Code:
set -f
while read line
do
  set -- $line
  case $# in
  6) grep -B1 -w "$1 $2 $3 $4 $5 $6" fileB
  ;;
  5) grep -B1 -w "$1 $2 $3 $4 $5" fileB
  ;;
  4) grep -B1 -w "$1 $2 $3 $4" fileB
  ;;
  3) grep -B1 -w "$1 $2 $3" fileB
  ;;
  2) grep -B1 -w "$1 $2" fileB
  ;;
  *)
  esac
done < fileA

This User Gave Thanks to MadeInGermany For This Post:
# 11  
Old 09-25-2018
Dear MadeInGermany,
Thanks for your answer. It worked at my end with expected result
I have just sorted out by trying another way. As I could not have handle it with previously explained way by other valuable board members, I replaced space by underscore in all my source files. By this way, fileA turned to one colon lines. Then I ran below script. And at last, replaced underscore by space once again to turn output file to expected view:

Code:
#!/bin/bash
while read COL1
do
        count=`echo $COL1 | wc -w`
        if [ $count -eq 2 ]; then
                break;
        else
                grep $COL1 -B1 fileB
fi
done<fileA > fileC
sed -i 's/_/ /g' fileC

Many Thanks
Boris
# 12  
Old 09-25-2018
But how can $count be 2 then? Isn't it always 1?
And, if you put the $COL1 in quotes, you can again work with the original space-separated files, and it boils down to
Code:
while read line
do
  grep -B1 -- "$line" fileB
done <fileA


Last edited by MadeInGermany; 09-25-2018 at 04:17 PM.. Reason: options first ending with -- so a leading - in $line is allowed
This User Gave Thanks to MadeInGermany For This Post:
# 13  
Old 09-25-2018
You are right. I took into account another case but now I see that it is not possible.
I will be using the code which you posted.


Many thanks
Boris
# 14  
Old 09-25-2018
How far would the -f (file) option to grep get you?



Code:
grep -B1 -ffile1 file2
+2828288889999
L Aragon
--
+330022110033
J L Borges
+440011223344
T S Eliot

This User Gave Thanks to RudiC 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

Search string or words in logs without using Grep

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)
Discussion started by: senormarquez
8 Replies

2. Shell Programming and Scripting

Search words in multiple file line by line

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)
Discussion started by: netdbaind
4 Replies

3. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

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)
Discussion started by: royzlife
12 Replies

4. Shell Programming and Scripting

Search words in a line and print next 15 lines.

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)
Discussion started by: kashif.live
4 Replies

5. Shell Programming and Scripting

print number of words in each line

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)
Discussion started by: mira
2 Replies

6. UNIX for Dummies Questions & Answers

How to grep for two or more words in a line at the same time ...

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)
Discussion started by: doza22
2 Replies

7. UNIX for Dummies Questions & Answers

search multiple words using grep

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)
Discussion started by: pb18798
20 Replies

8. Shell Programming and Scripting

how to get line number of different words if exists in same line

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)
Discussion started by: Amiya Rath
10 Replies

9. UNIX for Dummies Questions & Answers

grep with find to search for matchiing words

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)
Discussion started by: akhil313
1 Replies

10. Shell Programming and Scripting

How to grep for two or more words in a line at the same time?

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)
Discussion started by: ElCaito
2 Replies
Login or Register to Ask a Question