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
# 1  
Old 03-19-2017
Grab text after pattern on the same line

data:


Code:
8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5i
PACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+
8nwR15UzfeZafaf2bGr8akx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjp
lVpOoMLXJ   BUNLOES="343433434343mgg3383028383983mgg383827173494"  #BGLOFaakx6VwxBX+NafafxJMWX
8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5i
PACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPAC
Ib5fafafEU24f3EOOjp


i have a huge data file that contains text similar to the above. the text is a very, very long one line and it wraps around several times.

what i want to do is grab only a specific text.

in the above, i want a command that will grab the following text:

Code:
BUNLOES="343433434343mgg3383028383983mgg383827173494"

and will turn the "mgg" text into new lines, so that the final output looks like this:

Code:
343433434343
3383028383983
383827173494

Im looking for something efficient and portable. so naturally, that would be sed. i found the following:

Code:
sed -n -e 's/^.*\(BUNLOES=.*#BGLOFa\)/\1/p' data.txt

this sed command starts attempting to grab from point "BUNLOES up until #BGLOFa.

The command almost seems to work, but not exactly. im trying to do everything with one command instead instead of piping to awk.
# 2  
Old 03-19-2017
How about
Code:
sed -n '/^.*BUNLOES="/{s///;s/".*$//; s/mgg/\n/g;p}' file
343433434343
3383028383983
383827173494

This User Gave Thanks to RudiC For This Post:
# 3  
Old 03-19-2017
I get this error:

Code:
sed -n '/^.*BUNLOES="/{s///;s/".*$//; s/mgg/\n/g;p}' data.txt
sed: 1: "/^.*BUNLOES="/{s///; ...": extra characters at the end of p command

# 4  
Old 03-19-2017
Try a semicolon after the p command.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 03-19-2017
Hello SkySmart,

Could you please try following and let me know if this helps you.
1st code:
Code:
awk '{match($0,/BUNLOES.*[^"]/);VAL=substr($0,RSTART,RLENGTH);if(VAL){gsub(/BUNLOES="|"  #.*/,"",VAL);gsub(/mgg/,RS,VAL);print VAL}}'  Input_file
OR
awk '{
        match($0,/BUNLOES.*[^"]/);
        VAL=substr($0,RSTART,RLENGTH);
        if(VAL){
                gsub(/BUNLOES="|"  #.*/,"",VAL);
                num=split(VAL, a,"mgg");
                for(i=1;i<=num;i++){
                                        print a[i]
                                   }
               }
     }
    '   Input_file

2nd code:
Code:
awk '{match($0,/BUNLOES.*[^"]/);VAL=substr($0,RSTART,RLENGTH);if(VAL){gsub(/BUNLOES="|"  #.*/,"",VAL);num=split(VAL, a,"mgg");for(i=1;i<=num;i++){print a[i]}}}'   Input_file
OR
awk '{
        match($0,/BUNLOES.*[^"]/);
        VAL=substr($0,RSTART,RLENGTH);
        if(VAL){
                gsub(/BUNLOES="|"  #.*/,"",VAL);
                gsub(/mgg/,RS,VAL);
                print VAL
               }
     }
    '   Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 03-19-2017 at 09:11 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 03-19-2017
is bash portable, under your definition?
Code:
 regex='BUNLOES=\"([0-9]+)mgg([0-9]+)mgg([0-9]+)\"'
 while read line
 do
      if [[ $line =~ $regex ]]; then
         echo "${BASH_REMATCH[1]}"
         echo "${BASH_REMATCH[2]}"
         echo "${BASH_REMATCH[3]}"
      fi
 done < filename

These 2 Users Gave Thanks to jim mcnamara For This Post:
# 7  
Old 03-19-2017
Code:
awk -F\" 'gsub(/mgg/,RS,$2){print $2}' file

or
Code:
awk -F\" '/BUNLOES/ && gsub(/mgg/,RS,$2){print $2}' file



---
Note: On Solaris use /usr/xpg4/bin/awk rather than awk

Last edited by Scrutinizer; 03-19-2017 at 05:02 PM..
This User Gave Thanks to Scrutinizer 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