Concatenate text between patterns in individual strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenate text between patterns in individual strings
# 1  
Old 05-31-2012
Concatenate text between patterns in individual strings

In any given file, wherever a certain data block exists I need to concatenate the values(text after each "=" sign) from that block. in that block. The block starts and ends with specific pattern, say BEGIN DS and END DS respectively. The block size may vary. A file will have multiple such blocks. So say if there are 6 such blocks then I need 6 strings which have data in those respective blocks. Kindly guide.

Eg. "$file" has content :
Code:
 
Some lines of 
text
Begin DS
var1=some
var2=text 2 b concatenated 
End DS 
some more 
data
Begin DS
var1=some more
var2=text
var3=2 concatenate in 
var4=next string
End DS
:
:
so on

OUTPUT :::
Code:
 
text 2 b concatenated 
some more text 2 concatenate in next string
:

I know how to use sed for this if the block size was fixed ...

Last edited by Scrutinizer; 06-01-2012 at 04:53 AM.. Reason: code tags instead of quote tags
# 2  
Old 05-31-2012
Your example output seems missing a "some" in the 1st line. Anyway:

Code:
# awk -F= '/^Begin DS/ {i++} i && /^var[0-9]=/ {a ? a=a " " $NF : a=$NF } /^End DS/ {print a; a=x; i--}' infile
some text 2 b concatenated
some more text 2 concatenate in next string

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 05-31-2012
Another one:
Code:
awk -F= '/^Begin/{s=x}NF==2{s=s?s OFS $2:$2} /^End/ && s {print s}' file

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 06-01-2012
Thanks both of you .. brilliant. Can you kindly explain your solution? It would be really helpful if there is slight change in the format of input file, plus , I am interested. Smilie
# 5  
Old 06-01-2012
Quote:
Originally Posted by Prev
Thanks both of you .. brilliant. Can you kindly explain your solution? It would be really helpful if there is slight change in the format of input file, plus , I am interested. Smilie
Sure.

Code:
awk -F= '/^Begin/{s=x}NF==2{s=s?s OFS $2:$2} /^End/ && s {print s}' file

Explanantion:
Code:
-F=

Set "=" as field separator

Code:
/^Begin/{s=x}

If the line begins with the word "Begin" set variable s to NOTHING (dummy variable x=empty)
Code:
NF==2{s=s?s OFS $2:$2}

If there are 2 fields (records with a "=") then if s is not empty then s=s OFS $2 or s=$2
OFS is the output field separator, a built-in variable. The default value is a space.
Code:
/^End/ && s {print s}

If The line begins with the word "End" and s is not empty print s
# 6  
Old 06-01-2012
Concatenate all data between two patterns

Great. Thanks. So, to concatenate all data(not jst the part after =) in a block (all lines b/w Begin .. and end .. line ) , I tried with
Code:
 awk '/^BEGIN/{s=x} {s=s?s OFS $NR:$NR} /^End/ && s {print s}' file

but it doesnt work ... Please guide again.

Last edited by Scrutinizer; 06-01-2012 at 06:11 AM.. Reason: quote tags -> code tags
# 7  
Old 06-01-2012
Something like this?
Code:
awk '/^Begin/{f=1}f;/^End/{f=0}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Concatenate strings in a a for loop

hi guys, I have this question. I am creating an script to that read a text file(.ini) with the list of the patterns to find for example: EPMS_VO EMPS_PARTS Then it check if file have been delivered in a folder and process it with each pattern, but I am having problems concatenting the... (7 Replies)
Discussion started by: Danman
7 Replies

2. Shell Programming and Scripting

Concatenate strings

Hi,I'm trying to concatenate @example.com to each line of a file f1.txt. and push it into f2.txt. Here is the code i'm using. for i in `cat /home/linux1/xxxxxxx/f1.txt`; do echo ${i}@example.com > /home/linux1/xxxxxx/f2.txt; done But above code only printing @example.com in f2.txt. what... (18 Replies)
Discussion started by: sam_bd
18 Replies

3. Programming

Concatenate two strings

Hello! Can anyone explain line 28 for me? I was thinking *a would be replaced by *b, but it actually appends *a to *b. I know it is related to pointer address, but could not figure it out by myself. Thanks a lot! 1 //Concatenate two strings 2 3 #include<stdio.h> 4 char *stradd (char *,... (5 Replies)
Discussion started by: yifangt
5 Replies

4. Web Development

Concatenate Strings

hi..:) this is my sample part of my program.. $csv_output .= $row.",". $row.",". $row.",". $row.",". $row.",". ... (2 Replies)
Discussion started by: Jeneca
2 Replies

5. UNIX for Dummies Questions & Answers

concatenate strings

if i use echo "ravi" echo "sankar" it showing output ravi sankar but i want output as ravi sankar remember sankar should be in another echo statement only (2 Replies)
Discussion started by: shankr3
2 Replies

6. UNIX for Dummies Questions & Answers

Concatenate Strings

Hi Friends, I have a requirement I need to concatenate the below two strings. String 1 = /@jobid_at_ String 2 = value stored in ORACLE_SID String 3 = string1 concatenated with String 2. Please let me know how should i do it in UNIX. Thanks, (2 Replies)
Discussion started by: diva_thilak
2 Replies

7. Shell Programming and Scripting

patterns between strings...

I have a small requirement How to get patterns between string this way Input.. 05 ABC. TAGTAG 10 AAA PIC 9 10 BBB PIC X COMMET 10 CCC COMP PIC 9 05 DEF I wanted to get all the variable between 05 ABC and next 05 level out put should be AAA BBB (6 Replies)
Discussion started by: pbsrinivas
6 Replies

8. Shell Programming and Scripting

How to concatenate two strings or several strings into one string in B-shell?

like connect "summer" and "winter" to "summerwinter"? Can anybody help me? thanks a lot. (2 Replies)
Discussion started by: fontana
2 Replies
Login or Register to Ask a Question