Search String, Out matched text and input text for no match.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search String, Out matched text and input text for no match.
# 8  
Old 01-06-2015
Quote:
Originally Posted by jojojmac5
Just an FYI, I have to use nawk for this to work without an error message. But, it still prints out ,,, for every line in the file??

---------- Post updated at 12:04 PM ---------- Previous update was at 12:00 PM ----------

I did make it work with this string of commands:
grep "A" myfile | nawk '!/five/ {$0=",,,,,,,,,"}1'| sort -r | awk 'NR==1'
ok, didn't quite follow the last 'twist', but (thanks RudiC):
Code:
nawk -F, '$1="A" && !/five/ {$0=",,,"}1'  myFile

# 9  
Old 01-06-2015
For printing all lines starting with A and marking those NOT having "five", try
Code:
awk '/^A/ {if (!/five/) $0=",,,"; print}' file
,,,
,,,
A,three,five,six
,,,

---------- Post updated at 20:08 ---------- Previous update was at 20:07 ----------

@vgersh99: that's an assignment to $1, no?
# 10  
Old 01-06-2015
Quote:
Originally Posted by RudiC
@vgersh99: that's an assignment to $1, no?
shooot, too quick - thanks Rudi:
Code:
nawk -F, '$1=="A" && !/five/ {$0=",,,"}1'  myFile

# 11  
Old 01-06-2015
Often for such tasks 'grep -c ... yourdir/* |sed' is good, where sed can change the counts into na or whatever. Sometimes * makes the command line too long or the arg count too high, your OS may vary, but the usual fix is something that starts with 'find' configured for running grep with many file names, sometimes aided by '|xargs -n101' if 'find' lacks that option.

The disadvantage of that grep is the reading of a big file entirely, when the target line is at the front. You could use grep -l to get the set of file names with the pattern, and then use 'comm' to beat it against the set of the full list of tested files (lists must be binary sorted), then use 'sed' to deal with the hit and miss labeling.
# 12  
Old 01-06-2015
I want an output for what ever I search for and when the search has no match, I want an output of some text of my choosing, like comma's or "No Match".
# 13  
Old 01-07-2015
Exactly: 'grep -l' can find the list of files with matches in the list of all files, and 'comm -13' can compare that list to the list of all files, so you can find the files missing on the list and label them 'No Match'. Then you can merge the 'no match' list with the hit list, sorted if you care.
Code:
(
export LC_ALL=C
find * -type f | sort | tee /tmp/full.$$ | xargs -n101 grep -l '<pattern>' >/tmp/hits.$$
comm -13 /tmp/hits.$$ /tmp/full.$$ | sed 's/$/ No Match/' | sort -m /tmp/hits.$$ -
)

I like 101 as it captures >99% of available economy of scale.

Last edited by DGPickett; 01-07-2015 at 03:49 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Search last column of INPUT.txt in TABLEs text and add correspond columns to INPUT.txt

Hi dears i use bash shell i have INPUT.txt like this number of columns different in one some row have 12 , some 11 columns see last column INPUT.txt CodeGender Age Grade Dialect Session Sentence Start End Length Phonemic Phonetic 63 M 27 BS/BA TEHRANI 3 4 298320 310050... (2 Replies)
Discussion started by: alii
2 Replies

3. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

4. Shell Programming and Scripting

Search a certain char and insert new text if a match found

Have a file which has the create statement like below create table emp ( empno integer, empname char(50)) primary index(empno); i need to find a string starting with create and ends with semi-colon ;. if so insert the below statement before create statement rename table emp to emp_rename;... (2 Replies)
Discussion started by: Mohan0509
2 Replies

5. 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

6. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

7. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

8. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

10. Shell Programming and Scripting

appending string to text file based on search string

Hi, I need to append string "Hi" to the beginning of the lines containing some specific string. How can I achieve that? Please help. Malay (1 Reply)
Discussion started by: malaymaru
1 Replies
Login or Register to Ask a Question