Concatenating multiple lines to one line if match pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenating multiple lines to one line if match pattern
# 8  
Old 01-24-2008
Quote:
Originally Posted by phixsius
Hey mate,

Thanks alot for your suggestions. Unfort, i'm sorry, its still doesnt produce the result I'm looking...Smilie

Think there are any other logics to achieving this?

Why guess? Copy/paste the output from your terminal
so we can see what's wrong with the command(s).
# 9  
Old 01-24-2008
or the really hacky:

Code:
echo `tr "\n" " " <infile` | sed 's/Severity/%%Severity/g' | tr -s "%" "\n"

SmilieSmilie
# 10  
Old 01-24-2008
Hey Tytalus,

Fantastic sed codes!...Just what I needed...Smilie...works in my own environment.

Will test it in production tmr!..

Thanks a bunch!

Cheers
# 11  
Old 01-24-2008
Quote:
Originally Posted by radoulov
Why guess? Copy/paste the output from your terminal
so we can see what's wrong with the command(s).

Thanks for all your help mate!...appreciate it..Smilie
# 12  
Old 01-24-2008
Quote:
Originally Posted by phixsius
Thanks for all your help mate!...appreciate it..Smilie
Hm ...,
you're welcome Smilie
# 13  
Old 01-24-2008
Anyway,
given that the tr .. sed .. tr solution worked,
with nawk, gawk etc.

Code:
awk 'NR == 1 { 
  printf "%s", $0 FS
  next
} {
  printf "%s", /^Severity/ ? RS $0 FS : $0
} END {
  print ""
}' filename

For old, old awk's:

Code:
awk 'NR == 1 { 
  printf "%s ", $0 
  next 
} { 
  if ($0 ~ /^Severity/)
    printf "%s ",  RS $0
  else
    printf "%s", $0 
} END { 
  print "" 
}' filename

# 14  
Old 01-24-2008
sed+awk

Hi,

Follow one is a little complicated, but should works. Please try.

Code:
sed '/Severity/i\
aa' filename | sed 's/^aa$//' > filename.temp
nawk 'BEGIN{
FS="\n"
RS=""
}
{
for (i=1;i<=NF;i++)
printf("%s ",$i)
print ""
}' filename.temp
rm filename.temp

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 combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

2. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

3. Shell Programming and Scripting

Remove multiple lines that match pattern

Not sure how I can accomplish this. I would like to remove all interfaces that have the commands I would like to see: switchport port-security, spanning-tree portfast. One line is no problem. interface FastEthernet0/8 spanning-tree portfast interface FastEthernet0/9 spanning-tree... (4 Replies)
Discussion started by: mrlayance
4 Replies

4. 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

5. Shell Programming and Scripting

Multiple pattern match and print the output in a single line

I need to match two patterns in a log file and need to get the next line of the one of the pattern (out of two patterns) that is matched, finally need to print these three values in a single line. Sample Log: 2013/06/11 14:29:04 <0999> (725102) Processing batch 02_1231324 2013/06/11... (4 Replies)
Discussion started by: rpm120
4 Replies

6. Shell Programming and Scripting

awk print pattern match line and following lines

Data: Pattern Data Data Data Data Data Data Data Data Data ... With awk, how do I print the pattern matching line, then the subsequent lines following the pattern matching line. Varying number of lines following the pattern matching line. (9 Replies)
Discussion started by: dmesserly
9 Replies

7. Shell Programming and Scripting

How to insert line with between two consecutive lines that match special pattern?

I have following pattern in a file: 00:01:38 UTC abcd 00:01:48 UTC 00:01:58 UTC efgh 00:02:08 UTC 00:02:18 UTC and I need to change something like the following 00:01:38 UTC abcd 00:01:48 UTC XXXX 00:01:58 UTC efgh 00:02:08 UTC XXXX (6 Replies)
Discussion started by: jjnight
6 Replies

8. Shell Programming and Scripting

shell script: grep multiple lines after pattern match

I have sql file containing lot of queries on different database table. I have to filter specific table queries. Let say i need all queries of test1,test2,test3 along with four lines above it and sql queries can be multi lines or in single line. Input file contains. set INSERT_ID=1; set... (1 Reply)
Discussion started by: mirfan
1 Replies

9. Shell Programming and Scripting

Concatenating and appending string based on specific pattern match

Input #GEO-1-type-1-fwd-Initial 890 1519 OPKHIJEFVTEFVHIJEFVOPKHIJTOPKEFVHIJTEFVOPKOPKHIJHIJHIJTTOPKHIJHIJEFVEFVOPKHIJOPKHIJOPKEFVEFVOPKHIJHIJEFVHIJHIJEFVTHIJOPKOPKTEFVEFVEFVOPKHIJOPKOPKHIJTTEFVEFVTEFV #GEO-1-type-2-fwd-Terminal 1572 2030... (7 Replies)
Discussion started by: patrick87
7 Replies

10. Shell Programming and Scripting

Perl: Printing Multiple Lines after pattern match

Hello People, Need some assistance/guidance. OUTLINE: Two files (File1 and File2) File1 has some ids such as 009463_3922_1827 897654_8764_5432 File2 has things along the lines of: Query= 009463_3922_1827 length=252 (252 letters) More stufff here ... (5 Replies)
Discussion started by: Deep9000
5 Replies
Login or Register to Ask a Question