The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to print only lines in between two strings using awk jisha Shell Programming and Scripting 4 01-11-2008 04:13 AM
print selected lines tonet Shell Programming and Scripting 6 10-08-2007 06:50 AM
How to print number of lines with awk ? maheshsri Shell Programming and Scripting 1 11-18-2005 02:19 AM
print adjacent lines apalex UNIX for Dummies Questions & Answers 1 04-29-2002 12:59 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-27-2006
newlearner newlearner is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 8
Help Needed - print mutliple lines

I have the following input

-- appl = host = user = / pid = 76 elapsed = 0.000 seconds server_elapsed = 2.999
select emp_no, dept_no
from emp
where empname like 'Rob%'
and empno=10
;

-- appl = host = user = / pid = 76 elapsed = 0.000 seconds server_elapsed = 0.999
select emp_no, dept_no
from dept
where deptname like 'IT%'
;

The above input need to print the following output.

2.999 |select emp_no, dept_no from emp where empname like 'Rob%' and empno=10;
0.999 |select emp_no, dept_no from dept where deptname like 'IT%' ;

I have files with thousands of statements, I need to format them as above.

I appreciate your kind help and reply.
  #2 (permalink)  
Old 06-28-2006
thestevew thestevew is offline
Registered User
  
 

Join Date: Mar 2006
Location: South Yorkshire, UK
Posts: 114
Quick and dirty code to re-format your queries - assumes that each one starts with a comment preceded by "--"

Code:
while read a
do
if [[ $a = --* ]]; then
  print
  lf=$(echo $a|wc -w);
  print -n "$(echo $a|cut -d' ' -f $lf) |"
else
  print -n " "$a
fi
done < YourOriginalFile > YourReformattedFile

cheers
  #3 (permalink)  
Old 06-29-2006
newlearner newlearner is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 8
Wow ...

Thanks for your quick and dirty code to solve my problem. As I am a new to this kind of work, I could not able to understand the solution but it is great. I have one more request to the output. If I need only specific rows like I do not want 0.000 rows to the out put. then How can I proceed. I know one way, to do this by grep -v. But anyother way.

Thanks for your reply and help.
  #4 (permalink)  
Old 06-29-2006
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
Try...
Code:
awk '/^--/ && $NF>0 {printf $NF "|"; do {getline; printf $0 (/;/?ORS:OFS)} while (!/;/)}' file1 > file2

  #5 (permalink)  
Old 06-30-2006
matrixmadhan matrixmadhan is online now Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,958
how this the current post different from this one
  #6 (permalink)  
Old 06-30-2006
thestevew thestevew is offline
Registered User
  
 

Join Date: Mar 2006
Location: South Yorkshire, UK
Posts: 114
That awk line is terrific!
But using shell script with same assumptions as before, to suppress zeros:
Code:
while read a
do
if [[ $a = --* ]]; then
  lf=$(echo $a|wc -w)
  secs=$(echo $a|cut -d' ' -f $lf)
  msecs=$(( $secs * 1000))
  if [ $msecs -gt 0 ]; then
    print
    print -n "$secs |"
  fi
else
  if [  $msecs -gt 0 ]; then
    print -n " "$a
  fi
fi
done < YourOriginalFile > YourReformattedFile

cheers
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0