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.
# 1  
Old 01-05-2015
Search String, Out matched text and input text for no match.

I need to search a string for some specific text which is no big deal using grep. My problem is when the search fails to find the text. I need to add text like "na" when my search does not match.

I have tried this command but it does not work when I put the command in a loop in a bash script:

Test_File.csv
Code:
one,two,three
two,four,six
three,four,six
four,six,one

Command that works but not when I run it in a "while read" loop:
Code:
grep "five" Test_File.txt | grep [a-z] || echo ",,,"

Thanks in advance for the help!

Last edited by vgersh99; 01-05-2015 at 06:28 PM.. Reason: code tags, please!
# 2  
Old 01-05-2015
something to start with:
Code:
awk '!/five/ {$0=",,,"}1'  myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 01-05-2015
If looping for several files, try:
Code:
for fl in *.csv
do
   grep -q "five" $fl || echo "no match: $fl"
done

This User Gave Thanks to rdrtx1 For This Post:
# 4  
Old 01-06-2015
Thanks for your replies. I am getting closer. My bad for not providing enough info. Lets say I have 10,000 lines of data, but only 4 to 10 lines start with "A". I only want to search for "five" in the lines that start with "A" so I don't get results of ,,, for all 10,000 lines of data.

Code:
A,one,two,three
A,two,four,six
A,three,four,six
A,four,six,one


Last edited by vgersh99; 01-06-2015 at 12:57 PM.. Reason: once again - code tags, please!
# 5  
Old 01-06-2015
Code:
awk '$1!="A" && !/five/ {$0=",,,"}1'  myFile

This User Gave Thanks to vgersh99 For This Post:
# 6  
Old 01-06-2015
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'
# 7  
Old 01-06-2015
I'm lost. None of your file's lines has "five". With above pipe, you get the first line of a list of lines of all commas, one each for every line in your file, even if "A" is not the first char in the line.

WHAT exactly are you after?

BTW, add FS="," to vgersh99's proposal.
 
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