Help Needed - print mutliple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help Needed - print mutliple lines
# 1  
Old 06-27-2006
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  
Old 06-28-2006
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  
Old 06-29-2006
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  
Old 06-29-2006
Try...
Code:
awk '/^--/ && $NF>0 {printf $NF "|"; do {getline; printf $0 (/;/?ORS:OFS)} while (!/;/)}' file1 > file2

# 5  
Old 06-30-2006
how this the current post different from this one
# 6  
Old 06-30-2006
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies

3. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

4. Shell Programming and Scripting

Print n lines from top and n lines from bottom of all files with .log extenstion

Oracle Linux 6.4 In a directory I have more than 300 files with the extension .log I want the first 5 and last 5 lines of these .log files to be printed on screen with each file's name. Expected output : Printing first 5 and last 5 lines of FX_WT_Feb8_2014.log !! Authentication... (7 Replies)
Discussion started by: kraljic
7 Replies

5. Shell Programming and Scripting

Print mutliple patterns in a line using sed

Hi, I am trying to print multiple patterns in a line using sed. But it is printing only the last occurance of a pattern. If the line is the the output should be Lookup Procedure|Stored proc But the output I am getting is Stored proc The code I am using is echo... (9 Replies)
Discussion started by: kedar_laveti
9 Replies

6. Shell Programming and Scripting

sed print all lines between second and third identical lines

I am trying to extract a table of data (mysql query output) from a log file. I need to print everything below the header and not past the end of the table. I have spent many hours searching with little progress. I am matching the regexp +-\{99\} with no problem. I just can't figure out how to print... (5 Replies)
Discussion started by: godfreydanials
5 Replies

7. Shell Programming and Scripting

Print lines between two lines after grep for a text string

I have several very large file that are extracts from Oracle tables. These files are formatted in XML type syntax with multiple entries like: <ROW> some information more information </ROW> I want to grep for some words, then print all lines between <ROW> AND </ROW>. Can this be done with AWK?... (7 Replies)
Discussion started by: jbruce
7 Replies

8. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 Replies

9. Shell Programming and Scripting

print lines AFTER lines cointaining a regexp (or print every first and fourth line)

Hi all, This should be very easy but I can't figure it out... I have a file that looks like this: @SRR057408.1 FW8Y5CK02R652T length=34 AGCAGTGGTATCAACGCAGAGTAAGCAGTGGTAT +SRR057408.1 FW8Y5CK02R652T length=34 FIIHFF6666?=:88@@@BBD:::?@ABBAAA>8 @SRR057408.2 FW8Y5CK02TBMHV length=52... (1 Reply)
Discussion started by: kmkocot
1 Replies

10. Shell Programming and Scripting

AIX equivalent to GNU grep's -B and -A [print lines after or before matching lines]

Hi folks I am not allowed to install GNU grep on AIX. Here my code excerpt: grep_fatal () { /usr/sfw/bin/gegrep -B4 -A2 "FATAL|QUEUE|SIGHUP" } Howto the same on AIX based machine? from manual GNU grep ‘--after-context=num’ Print num lines of trailing context after... (4 Replies)
Discussion started by: slashdotweenie
4 Replies
Login or Register to Ask a Question