Find string1, when true find string2 in reverse direction


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find string1, when true find string2 in reverse direction
# 8  
Old 09-02-2018
If you want to do it with one process instead of two (or if you don't like the separator lines that grep -B produces, you could also try ed or awk (written to match your original specification):
Code:
string1=testtt
string2=@
echo 'Following output from "ed" script:'
ed -s MyFile <<END
	g/$string1/.p\\
	?$string2?p
END
printf '\nFollowing output from "awk" script:\n'
awk '
$0 ~ string1 {
	print
	print saved
}
$0 ~ string2 {
	saved = $0
}' string1="$string1" string2="$string2" MyFile

which, with your sample input, produces the output:
Code:
Following output from "ed" script:
testtt 11223344
His email address alfredo@alfredo.ru
testtt 55667788
His email address daniel@alfredo.ru

Following output from "awk" script:
testtt 11223344
His email address alfredo@alfredo.ru
testtt 55667788
His email address daniel@alfredo.ru

This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 09-02-2018
Many thanks Don!

kind regards
Boris
# 10  
Old 09-02-2018
Alternatively:

Code:
 perl -ne '/testtt/ and print "$_$keep"; $keep=$_ if /@/' test.file
     5  testtt 11223344
     1  His email address alfredo@alfredo.ru
    14  testtt 55667788
     8  His email address daniel@alfredo.ru

or
Code:
perl -ne '/testtt/ and print "$keep$_"; $keep=$_ if /@/' test.file
     1  His email address alfredo@alfredo.ru
     5  testtt 11223344
     8  His email address daniel@alfredo.ru
    14  testtt 55667788

Code:
cat test.file
     1  His email address alfredo@alfredo.ru
     2  test1
     3  test2
     4  test3
     5  testtt 11223344
     6  test4
     7  test5
     8  His email address daniel@alfredo.ru
     9  kkkk
    10  ddd
    11  ggg
    12  www
    13  qqq
    14  testtt 55667788
    15  His email address lucas@alfredo.ru
    16  aaa
    17  bbb
    18  ccc
    19  ddedd
    20  eeeeeeee
    21  ffffff

This User Gave Thanks to Aia For This Post:
# 11  
Old 09-02-2018
Dear Aia,
I am running the python script to produce a new output.
I will let Don and you know when it's done.

Kind regards
Boris
# 12  
Old 09-02-2018
Quote:
Originally Posted by baris35
Dear Aia,
I am running the python script to produce a new output.
I will let Don and you know when it's done.

Kind regards
Boris
If you are using Python already why don't you do it on it?

Code:
import re

string1 = re.compile('@')
string2 = re.compile('testtt')
with open("test.file") as f:
    for line in f:
        if string1.search(line):
            keep = line.rstrip()
            continue
        if string2.search(line):
           print("{0}{1}".format(line, keep))

# 13  
Old 09-02-2018
With awk
Code:
awk '$0~S1 {s1=$0} $0~S2 {print s1; print}' S1="@" S2="testtt" MyFile

You can restrict the search to the first field $1 (as opposed to the full line $0):
Code:
$1~S2

Or even have a full field match
Code:
$1==S2

(Now am seeing that Don had the same solution in principal)

Last edited by MadeInGermany; 09-02-2018 at 06:11 PM..
# 14  
Old 09-02-2018
Dear Don and Aia,
Both gives the expected out.

Dear MadeInGermany,
$0~S2 is okay at my end but as it's not printing the @ line, $1~S2 or $1==S2 is not okay

Thank you million times...

Kind regards
Boris

Last edited by baris35; 09-02-2018 at 06:15 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace string2 by string3 where string1 is found in line

I found this in the forum that searches a file for string1, substitute all occurrences of string2 with string3. (Title: Replace string2 by string3 where string1 is found in line) >> sed -i '/string1/s/string2/string3/g' TextFile How will I perform the same sed command and only substitute... (3 Replies)
Discussion started by: apalex
3 Replies

2. Shell Programming and Scripting

Replace string2 by string3 where string1 is found in line

Hello, My aim is to search string1 in all lines. When found, find and replace string2 by string3 if possible. TextFile: Here is my first line Second line with string1 & string2 Not last line but it contains string1 Expected output: Here is my first line The second line with string1 &... (6 Replies)
Discussion started by: baris35
6 Replies

3. UNIX for Beginners Questions & Answers

Replace string2 with string3 on all lines starting with string1

My OS is Windows 10 and I am using Cygwin. The file 1 content is: USE solution 2; -4.000 USE solution 3; -4.000 … USE solution 29; -4.000 USE solution 30; -4.000 USE solution 31; -4.000 …. USE solution 89; -4.000 ... USE solution 202; -4.000 etc... I need to replace... (8 Replies)
Discussion started by: supernono06
8 Replies

4. Shell Programming and Scripting

How to find a file with a specific pattern for current sysdate & upon find email the details?

I need assistance with following requirement, I am new to Unix. I want to do the following task but stuck with file creation date(sysdate) Following is the requirement I need to create a script that will read the abc/xyz/klm folder and look for *.err files for that day’s date and then send an... (4 Replies)
Discussion started by: PreetArul
4 Replies

5. Shell Programming and Scripting

awk - print record with both string1 and string2

How do I use awk to find the records in a file that contains two specific strings? I have tried piping and using awk two times, but I don't know how to do it in one action. (2 Replies)
Discussion started by: locoroco
2 Replies

6. Shell Programming and Scripting

Remove lines before string1 and after string2

Hello All... I have a text file (.ics) which I need to read into a variable but ONLY the part including and after 'BEGIN:VEVENT' and ending with END:VEVENT Anything before BEGIN:VEVENT or after END:VEVENT should be ignored. Thanks for input Jeff BEGIN:VCALENDAR VERSION:2.0... (3 Replies)
Discussion started by: uptimejeff
3 Replies

7. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies

8. UNIX for Dummies Questions & Answers

Replacing string1 with string2 in many files

I have 70 files and want to replace string1 with string2. How can i do that?. Thanks (4 Replies)
Discussion started by: shashikandi
4 Replies

9. Shell Programming and Scripting

command find returned bash: /usr/bin/find: Argument list too long

Hello, I create a file touch 1201093003 fichcomp and inside a repertory (which hava a lot of files) I want to list all files created before this file : find *.* \! -maxdepth 1 - newer fichcomp but this command returned bash: /usr/bin/find: Argument list too long but i make a filter all... (1 Reply)
Discussion started by: yacsil
1 Replies
Login or Register to Ask a Question