Sed/grep: check if line exists, if not add line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed/grep: check if line exists, if not add line?
# 1  
Old 02-19-2018
Sed/grep: check if line exists, if not add line?

Hello,
I'm trying to figure out how to speed up the following as I want to use multiple commands to search thousands of files.

is there a way to speed things up?

Example I want to search a bunch of files for a specific line, if this line already exists do nothing, if it doesn't exist add it below

So far I have

Code:
searchString="PortSystem 1.0"

addString="PortGroup user 1.0"
file="Portfile"
export -f doStuff
find . -depth  -name ${file} -type f -exec bash -c 'doStuff "$0"' {} \;

function doStuff(){
if grep -Fxq "$searchString" ${1:-} 
then   
sed '/$searchString/s/.*/&\n$addString' ${1:-}  
fi  
return 0
}

Thanks

Last edited by f77hack; 02-19-2018 at 02:10 AM.. Reason: typos
# 2  
Old 02-19-2018
What line do you search for,
searchstring or addstring or both?
What to do in which case?
Your function looks only for searchstring and adds addstring behind it.
But the subject and text says a string should be added if something does NOT exist.
This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 02-19-2018
Speeding up could be done by NOT executing a bash shell with command / function for every single file name found. Have find write all file names to a temp file, and read that in a while loop, callig the function from within - if a function is needed at all. grepping and sedding the same file means read / process it twice - use sed only to process it once. And, on top of what MadeInGermany already stated, you need to specify the position where to add the addstring if searchstring is missing, unless that were end-of-file.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 02-19-2018
Quote:
Originally Posted by MadeInGermany
What line do you search for,
searchstring or addstring or both?
What to do in which case?
Your function looks only for searchstring and adds addstring behind it.
But the subject and text says a string should be added if something does NOT exist.
Thanks. Inside the sed command is '\n' which inserts a new line. So I really don't get your point, the string is added on a new line if the searchString is not found. DId I misunderstand your statement?
# 5  
Old 02-19-2018
I think
grep -Fxq "$searchString"
is true if $searchString is found.
How can the sed run if $searchString is not found?

Last edited by MadeInGermany; 02-19-2018 at 02:40 PM.. Reason: typo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed or awk grep, that will only get the line with more characters.

Is there a command for sed and awk that will only sort the line with more characters? #cat file 123 12345 12 asdgjljhhho bac ss Output: asdgjljhhho #cat file2 11.2 12345.00 21.222 12345678.10 (2 Replies)
Discussion started by: invinzin21
2 Replies

2. Shell Programming and Scripting

awk or sed or grep filter a line and/or between strings

Hi, I have multiple files on a directory with the following content: blahblah blahblah hostname server1 blahblah blahblah ---BEGIN--- aaa bbb ccc ddd ---END--- blahblah blahblah blahblah I would like to filter all the files with awk or sed or something else so I can get below... (6 Replies)
Discussion started by: bayupw
6 Replies

3. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. Shell Programming and Scripting

GREP/SED - get string in a line

Hi, I simply try to get a string in a line but I do smth. wrong. Hopfefully you can help me ;) tried smth like: ggrep -Eo " /folder1/folder2/folder3/* end" get_info_file > temp.file I played a bit around but could not specify the end string command... So this is the... (9 Replies)
Discussion started by: unknown7
9 Replies

6. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies

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

8. UNIX for Dummies Questions & Answers

Need to serach if a new line character exists on the last line in a file

I have a file in which I need to search if a new line character exists on the last line in the file. Please let me know how can I achieve it using Unix commands? (10 Replies)
Discussion started by: sunilbm78
10 Replies

9. Shell Programming and Scripting

SED help (remove line::parse again::add line)

Aloha! I have just over 1k of users that have permissions that they shouldn't under our system. I need to parse a provided list of usernames, check their permissions file, and strip the permissions that they are not allowed to have. If upon the permissions strip they are left with no permissions,... (6 Replies)
Discussion started by: Malumake
6 Replies

10. UNIX for Dummies Questions & Answers

grep a string in a line using sed

Hi, I'm trying to grep a string in a line using sed. My original data looks like this: MRTG$ grep -i "System" $file <H1>Traffic Analysis for 15 -- sERITHC3602.t-mobile.co.uk</H1> <TABLE> <TR><TD>System:</TD> <TD>sERITHC3602 in </TD></TR> <TR><TD>Maintainer:</TD> <TD></TD></TR>... (4 Replies)
Discussion started by: viadisky
4 Replies
Login or Register to Ask a Question