Print field after pattern in all lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print field after pattern in all lines
# 1  
Old 01-03-2017
Print field after pattern in all lines

data:
Code:
hello--hello1--hello2--#growncars#vello--hello3--hello4--jello#growncars#dello--gello--gelloA--gelloB#growncars#

I want to be able to print all the values that are found between the patterns "#growncars#" and the next "#growncars#" on the same line.

so the output should be:

Code:
vello--hello3--hello4--jello
dello--gello--gelloA--gelloB

The actual data will be much larger than what I provided above. but the goal is the same. i need to grab only the fields that are between that patterns.

i would like to use awk for this and hopefully it'll be a solution that is portable.
# 2  
Old 01-03-2017
Hello SkySmart,

Could you please try following and let me know if this helps you.
Code:
awk '{match($0,/#growncars#.*#growncars#/);VAL=substr($0,RSTART,RLENGTH);gsub(/^#growncars#|#growncars#$/,X,VAL);gsub(/#growncars#/,"\n",VAL);print VAL}'  Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-03-2017
Hi,

In sed:
Code:
echo "xy--#growncars#vello--hello3--hello4--jello#growncars#dello--gello--gelloA--gelloB#growncars#" |   sed -e 's/.*#growncars#\(.*\)#growncars#\(.*\)#growncars#/\1\n\2/'

Gives the output:
Code:
vello--hello3--hello4--jello
dello--gello--gelloA--gelloB

These 2 Users Gave Thanks to greet_sed For This Post:
# 4  
Old 01-03-2017
Why the scenic route with VAL?
Code:
awk '{gsub (/#growncars#/, "\n"); sub ("^[^\n]*\n", ""); sub ("\n[^\n]*$", "")} 1' file
vello--hello3--hello4--jello
dello--gello--gelloA--gelloB

EDIT:
Or sed:
Code:
sed 's/#growncars#/\n/g; s/^[^\n]*\n\|\n[^\n]*$//g' file

These 2 Users Gave Thanks to RudiC For This Post:
# 5  
Old 01-03-2017
Try, works with current data

Code:
akshay@db-3325:/tmp$ cat f
hello--hello1--hello2--#growncars#vello--hello3--hello4--jello#growncars#dello--gello--gelloA--gelloB#growncars#

Code:
akshay@db-3325:/tmp$ awk '$0==p{s=1;next}s && NF; $0==p{s=0}' RS="#" p="growncars" f
vello--hello3--hello4--jello
dello--gello--gelloA--gelloB

This User Gave Thanks to Akshay Hegde For This Post:
# 6  
Old 01-03-2017
Hello SkySmart,

Could you please try following also once and let me know if this helps, with sample Input_file it is working fine.
Code:
awk -F"#growncars#" '{for(i=2;i<=NF-1;i++){if($i){print $i}}}'  Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 01-04-2017
Hello RudiC, could you please explain below highlighted part of code.sed:
Code:
sed 's/#growncars#/\n/g; s/^[^\n]*\n\|\n[^\n]*$//g' file

.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

3. Shell Programming and Scripting

Print ALL lines except if field is 999

Hi All!!! :-) I need a command that will print each line of a text file UNLESS the 3rd field of that line is equal to the number 999. (space seperated fields) Solaris10/BASH SHELL: INPUT.TXT aaa bbb 111 222 ccc ddd 333 444 eee fff 999 555 ggg hhh 666 777 aaa bbb 999 222 ccc ddd 333... (7 Replies)
Discussion started by: ajp7701
7 Replies

4. Shell Programming and Scripting

Awk: print lines with one of multiple pattern in the same field (column)

Hi all, I am new to using awk and am quickly discovering what a powerful pattern-recognition tool it is. However, I have what seems like a fairly basic task that I just can't figure out how to perform in one line. I want awk to find and print all the lines in which one of multiple patterns (e.g.... (8 Replies)
Discussion started by: elgo4
8 Replies

5. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

6. UNIX for Dummies Questions & Answers

Match pattern in a field, print pattern only instead of the entire field

Hi ! I have a tab-delimited file, file.tab: Column1 Column2 Column3 aaaaaaaaaa bbtomatoesbbbbbb cccccccccc ddddddddd eeeeappleseeeeeeeee ffffffffffffff ggggggggg hhhhhhtomatoeshhh iiiiiiiiiiiiiiii ... (18 Replies)
Discussion started by: lucasvs
18 Replies

7. Shell Programming and Scripting

print lines up to pattern excluding pattern

11 22 33 44 55 66 77 When pattern 55 is met, print upto it, so output is 11 22 33 44 (1 Reply)
Discussion started by: anilcliff
1 Replies

8. Shell Programming and Scripting

print the last line of an recurring pattern on the 3rd field

How can i awk/sed to print the last line of an recurring pattern on the 3rd field? Input lines: 123456.1 12 1357911 11111.1 01 123456.2 12 1357911 11111.2 02 123456.3 12 1357911 11111.3 03 123456.4 12 1357911 11111.4 04 123456.5 12 1357911 11111.5 05 246810.1 12 1357911 22222.1 01... (4 Replies)
Discussion started by: ux4me
4 Replies

9. Shell Programming and Scripting

Print lines where there's no indent on the first field

Hi All, I need a code to print those lines where there's NO indents on the 1st field Example shown below. I tried to use the below codes but i am not able to see the expected result. Can any expert give any advise ? My Code cat filename| awk '$1 ~ /^+$/ {print $0}' Input 1199 ... (7 Replies)
Discussion started by: Raynon
7 Replies

10. Shell Programming and Scripting

Print line if first Field matches a pattern

Hi All, I would like my code to be able to print out the whole line if 1st field has a dot in the number. Sample input and expected output given below. My AWK code is below but it can;t work, can any expert help me ? Thanks in advance. {if ($1 ~ /*\.*/) { print $0 }} Input: ... (2 Replies)
Discussion started by: Raynon
2 Replies
Login or Register to Ask a Question