Grab text after pattern on the same line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grab text after pattern on the same line
# 8  
Old 03-19-2017
Hello SkySmart,

Taking some inspiration from S and this could be helpful too on same.
Code:
awk '{gsub(/.*   BUNLOES="|"  #.*/,"");gsub(/mgg/,ORS);print}' RS=""   Input_file

If you want to check like both the strings BUNLOES and mgg are present as like shown Input_file then following additional check we could do and get it done then.
Code:
awk '{n=gsub(/.*   BUNLOES="|"  #.*/,"");n+=gsub(/mgg/,ORS);if(n==4){print}}' RS=""   Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 9  
Old 03-19-2017
Quote:
Originally Posted by RudiC
Try a semicolon after the p command.
this seems to work, but it doesnt put the numbers in new lines. i get them all in one line:

Code:
time sed -n '/^.*BUNLOES="/{s///;s/".*$//; s/mgg/\n/g;p;}' data.txt

Code:
'269118084457'n'3626086549312632'

i've tried repplacing the
Code:
\n

with
Code:
\\n

but that still didnt work.
# 10  
Old 03-19-2017
We can't see your terminal, nor do we know the versions of your OS, shell, nor sed. So - be creative. Look into your sed's man or info page on how to escape control characters (mine accepts \n for a <new line> char). Try bash's (if avaiable) $'string' mechanism. Try entering CTRL-V CTRL-J at the command line, but don't forget to escape this with a backslash.
If nothing helps, use one of the other proposals.
This User Gave Thanks to RudiC For This Post:
# 11  
Old 03-19-2017
Hi, SkySmart .
Quote:
Originally Posted by SkySmart
Im looking for something efficient and portable
When you ask for that, I think it's important for you to tell us what the class of systems on which you need to run this for portability.

For example, some sed don't seem to run suggested code. So what would be the earliest accepted version of sed that you have available, etc., etc.?

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 12  
Old 03-19-2017
Note: the use of \n in the replacement part of the sed's substitute command is a GNU extension.
The absence of a semicolon between p and } is a GNU extension. The use of semicolon p;} is a permitted, but not required extension to the POSIX specification, so even that may not be supported by all implementations.
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 13  
Old 03-19-2017
This (escaped explicit <NL> char) works on FreeBSD (which insists on the ; following p ):
Code:
sed -n '/^.*BUNLOES="/{s///; s/".*$//; s/mgg/\
/g; p; }' file
343433434343
3383028383983
383827173494

This User Gave Thanks to RudiC For This Post:
# 14  
Old 03-19-2017
RudiC's solution above works on my Solaris as well, which I think has non-GNU sed.

Code:
$ 
$ uname -a
SunOS solaris11-3 5.11 11.3 i86pc i386 i86pc
$ 
$ cat -n data.txt
     1    8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5i
     2    PACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+
     3    8nwR15UzfeZafaf2bGr8akx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjp
     4    lVpOoMLXJ   BUNLOES="343433434343mgg3383028383983mgg383827173494"  #BGLOFaakx6VwxBX+NafafxJMWX
     5    8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5i
     6    PACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPAC
     7    Ib5fafafEU24f3EOOjp
$ 
$ /usr/bin/sed -n '/^.*BUNLOES="/{s///;s/".*$//; s/mgg/\
/g;p;}' data.txt
343433434343
3383028383983
383827173494
$ 
$ /usr/xpg4/bin/sed -n '/^.*BUNLOES="/{s///;s/".*$//; s/mgg/\
/g;p;}' data.txt
343433434343
3383028383983
383827173494
$ 
$

I agree with drl that the OP should take stock of the systems and tools (s)he will be working with, in order to determine the portability of the solution.
This User Gave Thanks to durden_tyler For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to grab data in range then search for pattern

im using the following code to grab data, but after the data in the range im specifying has been grabbed, i want to count how many instances of a particular pattern is found? awk 'BEGIN{count=0} /parmlib.*RSP/,/seqfiles.*SSD/ {print; count++ } /103 error in ata file/ END { print count }'... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

Grab text after pattern and replace

i have a file which contains data seperated by comma. i want to replace text after 3rd occurrence of a comma. the input file looks like this abcdef,11/02/2015 11:55:47,1001,1234567812345678,12364,,abc abcdefg,11/02/2015 11:55:47,01,1234567812345678,123,,abc abcdefhih,11/02/2015... (4 Replies)
Discussion started by: gpk_newbie
4 Replies

3. Shell Programming and Scripting

How to grab a block of data in a file with repeating pattern?

I need to send email to receipient in each block of data in a file which has the sender address under TO and just send that block of data where it ends as COMPANY. I tried to work this out by getting line numbers of the string HELLO but unable to grab the next block of data to send the next... (5 Replies)
Discussion started by: loggedout
5 Replies

4. Shell Programming and Scripting

How to print text between two pattern in one line?

Hello all, I have one input file like this: List 1 mail a mail b mail c List 2 mail d mail e mail f mail g List 3 mail h mail i And I want something like that by using linux: List 1,mail a,mail b,mail c (4 Replies)
Discussion started by: benitto
4 Replies

5. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

6. Shell Programming and Scripting

Extract pattern from text line

The text line has the following formats: what.ever.bla.bla.C01G06.BLA.BLA2 what.ever.bla.bla.C11G33.BLA.BLA2 what.ever.bla.bla.01x03.BLA.BLA2 what.ever.bla.bla.03x05.BLA.BLA2 what.ever.bla.bla.Part01.BLA.BLA2 and other similar ones, I need a way to select the "what.ever.bla.bla" part out... (4 Replies)
Discussion started by: TehOne
4 Replies

7. Shell Programming and Scripting

Extract pattern from text line

Hi, the text line looks like this: "test1" " " "test2" "test3" "test4" "10" "test 10 12" "00:05:58" "filename.bin" "3.3MB" "/dir/name" "18459" what's the best way to select any of it? So I can for example get only the time or size and so on. I was trying awk -F""" '{print $N}' but... (3 Replies)
Discussion started by: TehOne
3 Replies

8. Shell Programming and Scripting

Extract pattern from text line

Gents, from these sample lines: ZUCR.MI ZUCCHI SPA RISP NC 2,5000 6 ott 0,0000 ZV.MI ZIGNAGO VETRO 3,6475 16:36 Up 0,0075 is it possible to get this: ZUCR.MI 2,5000 ZV.MI 3,6475 i.e. the first field, a separator and the first decimal number? (in Europe we... (9 Replies)
Discussion started by: vampirodolce
9 Replies

9. Shell Programming and Scripting

find 2 line numbers, grab text in between

hi, i have a large text file that I just want to extract the important information from. It will be a random number of lines but between two specific line numbers/markers. I was thinking I could get the line number for the first marker: Tablespace Percent Total Free Then get the line... (11 Replies)
Discussion started by: Da_Duck
11 Replies

10. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question