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
# 15  
Old 03-19-2017
this is the command I ended up using and it works across all necessary platforms:

Code:
export LC_ALL=C ; time tr '`' '\n' < data.txt | sed -n '/BUNLOES=/p' | awk -F"BUNLOES=" '{print $2}' | awk '{print $1}' | sed -e 's_"__g' -e 's_ __g' -e '/^$/d' 2>/dev/null | awk '{gsub("mgg","\n");printf"%s",$0}' | sed -e "s/'/ /g" -e 's~ ~~g' | sed '/^$/d'


can someone please help me combine this into one command, if possible?

the content of data.txt is a very very log one line:

Code:
8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5i PACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+ 8nwR15UzfeZafaf2bGr8akx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjp lVpOoMLXJ   BUNLOES="'269118084457'mgg'3626086549312632'"mgg1659344516312337mgg1659344516304657mgg5851430858050896mgg2968137013313563  #BGLOFaakx6VwxBX+NafafxJMWX 8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5i PACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPAC Ib5fafafEU24f3EOOjp8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5i PACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5i8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5i PACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5i8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5i PACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxfafakJMWX8iW5i

When i run the command of this post, i get this:

Code:
269118084457
3626086549312632
1659344516312337
1659344516304657
5851430858050896
2968137013313563

Now i just want to combine all the commands into one.

Moderator's Comments:
Mod Comment Please avoid the need to change the input specification in the middle of the thread

Last edited by Scrutinizer; 03-20-2017 at 01:47 AM..
# 16  
Old 03-19-2017
If you have Perl in all your platforms, then here's one way:

Code:
$ 
$ perl -lne '/BUNLOES=(.*?)\s+/ and do{$x=$1; $x=~s/[\x{27}" ]//g; $x=~s/mgg/\n/g; print $x}' data.txt
269118084457
3626086549312632
1659344516312337
1659344516304657
5851430858050896
2968137013313563
$ 
$

This User Gave Thanks to durden_tyler For This Post:
# 17  
Old 03-19-2017
Code:
perl -nle 'BEGIN{$,="\n"}/BUNLOES=(.+?)\s/ and print $1=~/(\d+)/g' skysmart.file
269118084457
3626086549312632
1659344516312337
1659344516304657
5851430858050896
2968137013313563

or
Code:
perl -nle '/BUNLOES=(.+?)\s/ and print join "\n", $1=~/(\d+)/g' skysmart.file

or
Code:
perl -nle 'map{s/\D+/\n/g and print} /BUNLOES\D+(.+?)\s/' skysmart.file


Last edited by Aia; 03-20-2017 at 12:39 AM.. Reason: Adds a third option
This User Gave Thanks to Aia For This Post:
# 18  
Old 03-20-2017
perl wont be present on some of these systems i have to run the command on. a lot of them are docker systems with the bare minimum.
# 19  
Old 03-20-2017
Try :
Code:
awk 'p==s{gsub(qr,x); gsub(fs,ORS,$1); print $1}{p=$NF}' s=BUNLOES fs=mgg qr="'|\"" RS== file


--
@SkySmart: you just changed the specification in post #15, by introducing random single quotes and double quotes into the data, plus by mentioning that your data is just one very long line.

That means that all the people who reacted earlier, trying to help you, were not using the right data and therefore were unable to produce adequate code and were basically working for no purpose.

Please do not do that. Have the complete data specification ready when you create the thread..

Last edited by Scrutinizer; 03-20-2017 at 01:44 AM..
These 3 Users Gave Thanks to Scrutinizer For This Post:
# 20  
Old 03-20-2017
Quote:
Originally Posted by SkySmart
perl wont be present on some of these systems i have to run the command on. a lot of them are docker systems with the bare minimum.
Out of curiosity. Would any of these work?

Code:
grep -Eo '[0-9]{4,}' skysmart.file

Code:
grep -Po '\d{4,}' skysmart.file

Output:
Code:
269118084457
3626086549312632
1659344516312337
1659344516304657
5851430858050896
2968137013313563

This User Gave Thanks to Aia 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