0

I have following two lines in my Input file.

Sample Input file ( name of file - file.txt)

String1   value   'string2'
..
..
..
Call  string1 

Desired output : File.txt. ( i.e. Name of file )

Basically i want names of file - if it contains these two lines 1) string1 value 'string2' 2) call string1

1) and 2) above are two different lines and there could be many lines in between.

P.S. a) i am searching for 'string2' . 'string1' could be any 8 characters. I do not know 'string1' b) 'string2' will be always in single quote (') c) 'string2' will always be preceded by string 'value'

Thanks

0

2 Answers 2

1

Try this...

 gc input.txt |?{$_ -match '.*value ''(.*)'''}|%{$matches[1]}
Sign up to request clarification or add additional context in comments.

Comments

0

The below script goes through each file in the current directory and looks for the files with the 2 lines in the content. Not sure if this meets your situation but it is not difficult to customize it to you environment.

dir *.* | foreach {if (get-content $_| Select-String -pattern  "^[A-Za-z]{8} value 'string2'","Call string1") {"$_"}}

The output (in my lab) is:

C:\Documents\ManualScripts\test.txt

test.txt is the file I created for testing this script. The content is as following:

abcdefgh value 'string2'
hello world
I love church
I love Jesus
call string1
Alibaba, sesame open the door!

To @dazedandconfused I believe your script return "String1" which is part of the second line instead of file name he/she requested. Besides your script doesn't reflect another one of his/her needs: "'string1' could be any 8 characters". Forgive me if I am wrong.

3 Comments

Thanks a lot!! I am almost there. Pardon my inexperience. Just a small change i would require - in your script. 'abcdefgh' (first line before 'value') should match 'string1' (after call statement in 5th line). In other words, string1 should be equal to 'abcdefgh' - then only i want it in Output. Thanks.
No problem I am newbie too for powershell. If it makes sense please accept the answer:)
Actually, the original post has been edited substantially. My original answer was relevant at the time but not so much now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.