• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

UNIX schell scripting, find string, replace at the start...

Koing

Elite Member <br> Super Moderator<br> Health and F
How do I find a string within a file say loadRuleLatest and check if it has a # at the start and if it does, can it take out the # and leave the line as is? That can be one script

And also the reverse?
How can I check that if it it finds loadRuleLatest and then takes out # at the start of the line if it is present?

Currently I we have to manually edit the pair of config files and there are 8 pairs of files. This would speed up the progress dramatically for me!

I can use the SED command but not sure how to get it to replace at the start of the file

e.g
Finds:
#/server/box1/algo/loadRuleLatest pathA/ruleAAB

changed to
/server/box1/algo/loadRuleLatest pathA/ruleAAB


Finds
/server/box1/algo/loadRuleLatest pathA/ruleAAB
#/server/box1/algo/loadRuleLatest pathA/ruleAAB

Every occurrence in file: :%s/OLD/NEW/g
I've used SED but ion this case the path after /box1 is different 🙁

Cheers guys. I've been searching online how to do it but need more help (n00b). Cheers!

Koing
 
Originally posted by: degibson
sed -i "s/^#//" myFile.txt

Cheers, mate but unfortunately that isn't going to work in my case as I have other # in other lines that I need to stay.

This is why I need to find the lines with the string 'loadRuleLatest and only replace # if its present or add it if it is not depending which file I want to change.

But cheers mate, appreciate the helpl!

Koing
 
sed '/loadRuleLatest/s/^#//g'

This will work for when I want to take out the # at the start of the line when I have 'loadRuleLatest' in the line. Now to bodge the other one to work...

Koing
 
Originally posted by: Koing
sed '/loadRuleLatest/s/^#//g'

This will work for when I want to take out the # at the start of the line when I have 'loadRuleLatest' in the line. Now to bodge the other one to work...

Koing

well, if that syntax works then you should be able to do (or very similar to):

sed '/^(^#).*loadRuleLatest/s/^/#/g'
 
Originally posted by: Onund
Originally posted by: Koing
sed '/loadRuleLatest/s/^#//g'

This will work for when I want to take out the # at the start of the line when I have 'loadRuleLatest' in the line. Now to bodge the other one to work...

Koing

well, if that syntax works then you should be able to do (or very similar to):

sed '/^(^#).*loadRuleLatest/s/^/#/g'

/server/box1/algo/loadRuleLatest pathA/ruleAAB
#/server/box1/algo/loadRuleLatest pathA/ruleAAB

That didn't seem to work on it?

box01 kTEST $ sed '/^(^#).*loadRuleLatest/s/^/#/g' A.txt
/server/box1/algo/loadRuleLatest pathA/ruleAAB
#/server/box1/algo/loadRuleLatest pathA/ruleAAB

/server/box1/algo/loadRuleLatest pathA/ruleAAB
#/server/box1/algo/loadRuleLatest pathA/ruleAAB

/server/box1/algo/loadRuleLatest pathA/ruleAAB
#/server/box1/algo/loadRuleLatest pathA/ruleAAB

/server/box1/algo/loadRuleLatest pathA/ruleAAB
#/server/box1/algo/loadRuleLatest pathA/ruleAAB

BTW what does the ^ mean? I understand your script should work:

sed '/^(^#).*loadRuleLatest/s/^/#/g' A.txt

Sorry mate I was being a f0cking n00b (no change here...) and that you meant ^(^#) as an OPTION and not both of them 😛 doh! Cheers mate. MUCH APPRECIATED!

sed '/^.*loadRuleLatest/s/^/#/g' A.txt add #
sed '/^#.*loadRuleLatest/s/^#//g' A.txt minus #

Cheers!

Kind Regards,
Koing
 
Originally posted by: Koing
Originally posted by: Onund
Originally posted by: Koing
sed '/loadRuleLatest/s/^#//g'

This will work for when I want to take out the # at the start of the line when I have 'loadRuleLatest' in the line. Now to bodge the other one to work...

Koing

well, if that syntax works then you should be able to do (or very similar to):

sed '/^(^#).*loadRuleLatest/s/^/#/g'

/server/box1/algo/loadRuleLatest pathA/ruleAAB
#/server/box1/algo/loadRuleLatest pathA/ruleAAB

That didn't seem to work on it?

box01 kTEST $ sed '/^(^#).*loadRuleLatest/s/^/#/g' A.txt
/server/box1/algo/loadRuleLatest pathA/ruleAAB
#/server/box1/algo/loadRuleLatest pathA/ruleAAB

/server/box1/algo/loadRuleLatest pathA/ruleAAB
#/server/box1/algo/loadRuleLatest pathA/ruleAAB

/server/box1/algo/loadRuleLatest pathA/ruleAAB
#/server/box1/algo/loadRuleLatest pathA/ruleAAB

/server/box1/algo/loadRuleLatest pathA/ruleAAB
#/server/box1/algo/loadRuleLatest pathA/ruleAAB

BTW what does the ^ mean? I understand your script should work:

sed '/^(^#).*loadRuleLatest/s/^/#/g' A.txt

Sorry mate I was being a f0cking n00b (no change here...) and that you meant ^(^#) as an OPTION and not both of them 😛 doh! Cheers mate. MUCH APPRECIATED!

sed '/^.*loadRuleLatest/s/^/#/g' A.txt add #
sed '/^#.*loadRuleLatest/s/^#//g' A.txt minus #

Cheers!

Kind Regards,
Koing

no... i did mean ^(^#) in the expression (but I'm wrong, see below).

If you do this:
sed '/^.*loadRuleLatest/s/^/#/g' A.txt add #

and you fed it this:
/server/box1/algo/loadRuleLatest pathA/ruleAAB
#/server/box1/algo/loadRuleLatest pathA/ruleAAB

the ouput would be:
#/server/box1/algo/loadRuleLatest pathA/ruleAAB
##/server/box1/algo/loadRuleLatest pathA/ruleAAB

the ^ means different things depending on where it is. In /^a/ the ^ is position and means 'at the start of the string' so this will find 'a' at the start of a string.

If... ah crap... I think I should have told you [^#]... so you can define groups of characters right? [abc] means match any of 'a' 'b' or 'c'. If you do [^abc] then you're doing the compliment or, match anything but 'a', 'b' or 'c'. I told you (^#). when you put stuff in parenthesis you're telling regex to match that sequence so (abc) means match 'abc'. In this case, it's probably looking for '^#'. My bad.


This rule below says "find any zero or more characters at the start of the string, followed by 'loadruleLatest'. Change the start of the string into a #"
sed '/^.*loadRuleLatest/s/^/#/g' A.txt add #

This rule below says "find a line that does NOT start with #, follow by any zero or more characters, followed by loadRuleLatest. change the start of that string into a #"
sed '/^[^#].*loadRuleLatest/s/^/#/g' A.txt add #

 
Onund got you mate! I appreciate man. Works a treat man 😀

sed '/^[^#].*loadRuleLatest/s/^/#/g' A.txt add #
sed '/^#.*loadRuleLatest/s/^#//g' A.txt remove #

I have a few scripts that I can use to create a backup of a file and the then I can get it to read in a filename and then make the changes on to it 🙂

Again cheers!

Koing
 
Back
Top