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?
# 8  
Old 11-28-2010
Quote:
Originally Posted by cabrao
@scrutinizer

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

Sure.

!NF{p=0}If the line is empty (there are no fields) then the end of the block is reached (var p becomes 0)
p{$1=$1;print}if the block is to be printed then reformat the record (line) by assigning a value to one of the fields and then print the line. One of the effects is that leading spaces are removed..
/^ \(/{p=1} if the line contains a space followed by a an opening parenthesis then the next line is the start of the block, so make p=1
ORS= Use "" as the output record separator so that the newlines are removed and lines are joined
This User Gave Thanks to Scrutinizer For This Post:
# 9  
Old 11-28-2010
I guess it's clear even if p{$1=$1;print} seems peculiar to me...

Thanks scrutinizer Smilie
# 10  
Old 11-28-2010
You're welcome. If p=1 then the part in curly brackets gets executed. $1=$1 is a way of applying formatting with OFS as field separator... (See https://www.unix.com/shell-programming-scripting/148216-view-ouput-file.html#7, perhaps I explained it more clearly there.)
# 11  
Old 11-28-2010
@scrutinizer:
Thanks for your answer again. One more thing, can p{$1=$1;print} be "read" like if p is true then print $1=$1 ?

I followed your previous link and I understand your command but why can we do $2=$2, $3=3 etc but not $0=0 ?
# 12  
Old 11-28-2010
Almost, it can be read like if p is true then $1=$1 and then print $0.
Second question: because $0 signifies the record itself, not one of the fields...
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