How to search for multiple lines and put them into one paragraph?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to search for multiple lines and put them into one paragraph?
# 1  
Old 11-27-2010
How to search for multiple lines and put them into one paragraph?

Dear all,

I'm trying to manipulate a data file and putting a certain lines into one paragraph.
What am I actually want to do is that search some lines in a data file. These lines begin with "1\1\GINC-" and end with "\\@" or the following two empty lines as shown in blue.

A part of the text shows as follows, (the sign "==>| |<==" is used to show that each line begins with a space, which is not contained in the original file. The original file also shown at the end of this post for your script testing.)

Code:
==>| Symmetry A    KE= 5.003963609647D+02|<==
==>| Symmetry B    KE= 2.202693107013D+02|<==
==>| Leave Link  601 at Sat Nov 27 09:26:00 2010, MaxMem=  671088640 cpu:|<==
==>| (Enter /home/bpliu/gdata/gaussian/g09/l9999.exe)|<==
==>| 1\1\GINC-NODE59\SP\UCCSD(T)-FC\GenECP\Cr1H2O2Si1(3)\BPLIU\27-Nov-2010\|<==
==>| 0\\#P scf=(maxcycle=1000,qc) uccsd-t(maxcycle=1000)/genecp gfprint\\3a|<==
==>| 0-b3lyp\\0,3\Si,0,0.014066,-1.355809,0.\O,0,1.238257,-0.219629,0.00000|<==
==>| 1\Cr,0,-0.01268,1.131665,0.\O,0,-1.234272,-0.246214,-0.000002\H,0,0.02|<==
==>| 3229,-2.208103,1.211679\H,0,0.023232,-2.208103,-1.21168\\Version=EM64L|<==
==>| -G09RevA.01\State=3-A\HF=-525.4164345\MP2=-526.0063196\MP3=-526.013047|<==
==>| 9\MP4D=-526.0298231\MP4DQ=-526.0236019\PUHF=-525.3995614\PMP2-0=-525.9|<==
==>| 887651\PMP3-0=-525.9948332\MP4SDQ=-526.0382803\CCSD=-526.0625561\CCSD(|<==
==>| T)=-526.1090413\S2=2.885749\S2-1=2.957951\S2A=2.012381\RMSD=0.000e+00\|<==
==>| PG=C02 [C2(Si1Cr1),X(H2O2)]\\@|<==
==>||<==
==>||<==
==>| ... UNTIL SCIENCE IS MIXED WITH EMOTION AND APPEALS TO THE HEART AND|<==
==>| IMAGINATION , IT IS LIKE DEAD INORGANIC MATTER; AND WHEN IT IS SO MIXED|<==
==>| AND SO TRANSFORMED IT IS LITERATURE.|<==

The expect output file is,

Code:
==>|1\1\GINC-NODE59\SP\UCCSD(T)-FC\GenECP\Cr1H2O2Si1(3)\BPLIU\27-Nov-2010\0\\#P scf=(maxcycle=1000,qc) uccsd-t(maxcycle=1000)/genecp gfprint\\3a0-b3lyp\\0,3\Si,0,0.014066,-1.355809,0.\O,0,1.238257,-0.219629,0.000001\Cr,0,-0.01268,1.131665,0.\O,0,-1.234272,-0.246214,-0.000002\H,0,0.023229,-2.208103,1.211679\H,0,0.023232,-2.208103,-1.21168\\Version=EM64L-G09RevA.01\State=3-A\HF=-525.4164345\MP2=-526.0063196\MP3=-526.0130479\MP4D=-526.0298231\MP4DQ=-526.0236019\PUHF=-525.3995614\PMP2-0=-525.9887651\PMP3-0=-525.9948332\MP4SDQ=-526.0382803\CCSD=-526.0625561\CCSD(T)=-526.1090413\S2=2.885749\S2-1=2.957951\S2A=2.012381\RMSD=0.000e+00\PG=C02 [C2(Si1Cr1),X(H2O2)]\\@|<==

the original file is put as follows for testing.
Code:
 Symmetry A    KE= 5.003963609647D+02
 Symmetry B    KE= 2.202693107013D+02
 Leave Link  601 at Sat Nov 27 09:26:00 2010, MaxMem=  671088640 cpu:
 (Enter /home/bpliu/gdata/gaussian/g09/l9999.exe)
  1\1\GINC-NODE59\SP\UCCSD(T)-FC\GenECP\Cr1H2O2Si1(3)\BPLIU\27-Nov-2010\
 0\\#P scf=(maxcycle=1000,qc) uccsd-t(maxcycle=1000)/genecp gfprint\\3a
 0-b3lyp\\0,3\Si,0,0.014066,-1.355809,0.\O,0,1.238257,-0.219629,0.00000
 1\Cr,0,-0.01268,1.131665,0.\O,0,-1.234272,-0.246214,-0.000002\H,0,0.02
 3229,-2.208103,1.211679\H,0,0.023232,-2.208103,-1.21168\\Version=EM64L
 -G09RevA.01\State=3-A\HF=-525.4164345\MP2=-526.0063196\MP3=-526.013047
 9\MP4D=-526.0298231\MP4DQ=-526.0236019\PUHF=-525.3995614\PMP2-0=-525.9
 887651\PMP3-0=-525.9948332\MP4SDQ=-526.0382803\CCSD=-526.0625561\CCSD(
 T)=-526.1090413\S2=2.885749\S2-1=2.957951\S2A=2.012381\RMSD=0.000e+00\
 PG=C02 [C2(Si1Cr1),X(H2O2)]\\@


 ... UNTIL SCIENCE IS MIXED WITH EMOTION AND APPEALS TO THE HEART AND
 IMAGINATION , IT IS LIKE DEAD INORGANIC MATTER; AND WHEN IT IS SO MIXED
 AND SO TRANSFORMED IT IS LITERATURE.

Sorry for my poor English. I hope I have made myself clear. In short, get the blue part and put into one paragraph.
Thank you in advanced for your kind help.

ZHEN

Last edited by liuzhencc; 11-27-2010 at 06:18 AM..
# 2  
Old 11-27-2010
What have you attempted so far?
# 3  
Old 11-27-2010
Quote:
Originally Posted by Scrutinizer
What have you attempted so far?
I may misunderstand your meaning by "What have you attempted so far?". I was thing I posted an unclear question on my problem.

The problem still there. I'm not get the correct script to do that. Please help.

ZHEN
# 4  
Old 11-27-2010
Zhen, your question was, clear, I was just asking what you had tried yourself...

---------- Post updated at 13:29 ---------- Previous update was at 12:41 ----------

You could give this a try:
Code:
awk '!NF{p=0}p{$1=$1;print}/^ \(/{p=1}' ORS= infile > outfile

# 5  
Old 11-27-2010
Thanks a lot. It works well.

I also search google and find another way to do this work,

Code:
sed -n '/^\ 1\\1\\GINC-/,/\\\\\@/p'   < input                       >  temp
tr "\n" " "                           < temp   | sed "s. ..g"       >  output
rm temp

Both work for me!

Thanks for your kind help!

ZHEN
# 6  
Old 11-27-2010
@scrutinizer

Can you please explain your command ?
Code:
'!NF{p=0}p{$1=$1;print}/^ \(/{p=1}'

# 7  
Old 11-27-2010
Quote:
Originally Posted by liuzhencc
Thanks a lot. It works well.

I also search google and find another way to do this work,

Code:
sed -n '/^\ 1\\1\\GINC-/,/\\\\\@/p'   < input                       >  temp
tr "\n" " "                           < temp   | sed "s. ..g"       >  output
rm temp

The idea is to use the 'sed' work to get some continuous data begin with "1\1\GINC-" and end with "\\@". These two points are unique in my data sheet. So, It works!
The second line 'tr' is used to delete the "\n" at each line and put these multiple lines into single line. The parallel command 'sed "s. ..g"' is used to remove the empty space in the text, which is introduced in the original data file. Although it may remove all the single white space in the data file and put some some words into a continuous string. I don't mind this, becasuse my interest is to get some numbers stored in the data, which may be beaked by the "\n" at the end of each line and the "withe space" the the beginning of each line. So, Work is done.

Thanks. If I was not able to make myself clear, pardon me for my English. Learning a good English is my dream.


ZHEN
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search multi lines in to multiple files

how to find multiple lines in to multiple files if matched lines then do f1 f2 f3 are files ,lines contain matched lines f1 asdasdasdasd asdasdasdasd abc def Business date cannot be as of date ghi sdasdasdasda asdasdasdada f2 (1 Reply)
Discussion started by: Kalia
1 Replies

2. Shell Programming and Scripting

Search a character and replace it with multiple lines

This is for AIX 6.1, I've a flat file and the format is like this DECLARE some statements; BEGIN some statements; END; I've to search BEGIN and replace it with the following 4 lines BEGIN For x in 1..1 LOOP BEGIN Similarly I've to search END and replace it with the... (7 Replies)
Discussion started by: Mukul Sharma
7 Replies

3. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

4. Shell Programming and Scripting

Search for a particular string in a paragraph in a text

Hi all, i'm new to this community. I am trying to write a script which will fetch ftp completion time of a file from a paragraph of a big text file ( which contains multiple paragraphs) . Each paragraph will have ftp details.. Now I dont know how to fetch process time within a paragraph of... (3 Replies)
Discussion started by: prachiagra
3 Replies

5. Shell Programming and Scripting

Search for multiple lines in large file

Hi, I have a requirement to search for a string in a large log file along with few lines before and after the the string. The following script was sufficient to search such an entry. STRING_TO_GREP="$1" FILE_TO_GREP="$2" NUMBER_OF_LINES_BEFORE=$3 NUMBER_OF_LINES_AFTER=$4 for i in `grep... (3 Replies)
Discussion started by: praveen123
3 Replies

6. Shell Programming and Scripting

Pattern search in multiple lines

Hi, I have to search those statements from the file which starts from "shanky"(only shanky, shanky09 or 09shanky is not allowed) and ends with ");". These two string can be in a same line or different line. And also i have to negate those lines which starts with #. Can any one please give me... (2 Replies)
Discussion started by: shanky09
2 Replies

7. Shell Programming and Scripting

put the contents of this file into a variable with multiple lines

I have a file that contains the following lines the brown quick fox jumped over the white laze dog 0123456789 I wanted to put the contents of this file into a variable so I used this code: VAR_LIST=`cat $2` where $2 is the file name passed as an argument to the script If I... (3 Replies)
Discussion started by: Nomaad
3 Replies

8. Shell Programming and Scripting

using tr to put multiple lines of output into one line

Hi all, For a intro UNIX course I'm taking, I need to use the command "tr" to display a file on standard output without any newlines (all on one line). I assume I would start with "cat filename | tr" but don't know what to put after tr. Any ideas would be lovely! Thanks. (3 Replies)
Discussion started by: otes4
3 Replies

9. Shell Programming and Scripting

selecting each paragraph and put it into a file...help me

Dear Friends, I need a shell script. I am facing a problem while selecting the text that is between start and end tags. and insert the paragraph into a file1, next paragraph in file2...... experts please help. i have a file which contains, -------------- <abc> 111some text some text some... (2 Replies)
Discussion started by: swamymns
2 Replies
Login or Register to Ask a Question