Visit Our UNIX and Linux User Community


Output text from 1st paragraph in file w/ a specific string through last paragraph of file w/ string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Output text from 1st paragraph in file w/ a specific string through last paragraph of file w/ string
# 1  
Old 04-09-2010
Tools Output text from 1st paragraph in file w/ a specific string through last paragraph of file w/ string

Hi, I'm trying to output all text from the first paragraph in a file that contains a specific string through the last paragraph in that file that contains that string.

Previously, I was outputting just each paragraph with that search string with:

Code:
cat in_file | nawk '{RS=""; FS="\n"; ORS="\n\n"; OFS="\n"} /searchstring/ {print}' > out_file

The problem I've ran into is that that search string does not always appear in some paragraphs that are logged between the 1st and last occurrences.

Any ideas to make this happen?

Thanks.
# 2  
Old 04-09-2010
First off you get a useless use of cat award.

What you want is a bit trickier than just grepping for something, since it means needing to store lines until you've processed the whole file... Does your system have bash or perl? I'd write a script.

Code:
#!/bin/bash

STRING="MARK"
MODE="start"

TMP=`mktemp`
exec 5>$TMP

while IFS="" read LINE
do
        case "$MODE" in
        start)
                if [[ "$LINE" =~ $STRING ]]
                then
                        MODE="mid"
                        echo "$LINE"
                fi
                ;;
        mid)    if [[ "$LINE" =~ $STRING ]]
                then
                        cat $TMP
                        echo $LINE
                        echo > $TMP
                        exec 5>$TMP
                else
                        echo "$LINE" >&5
                fi
                ;;
        esac
        :
done <<EOF
a b
MARK
asdf1
asdf2
MARK
asdf3
asdf4
MARK
qwerty5
EOF

rm -f $TMP

To pipe stdin through it instead of the here-document, simply delete everything from <<EOF to EOF.
# 3  
Old 04-09-2010
Thanks, that should work great
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #987
Difficulty: Medium
The FreeBSD TCP/IP stack is based on the 4.2BSD implementation of TCP/IP.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join Lines every paragraph in a file.txt

Hi all, Is there any idea on how to automate convert the paragraph in one line in a file, this will happen after OCR the documents, OCR split every paragraph. I need to join all the paragraph in one line. #cat file.txtThe Commission on Higher Education (CHED) was created through Republic Act... (7 Replies)
Discussion started by: lxdorney
7 Replies

2. Shell Programming and Scripting

How to extract a paragraph containing a given string?

Hello: Have a very annoying problem: Need to extract paragraphs with a specific string in them from a very large file with a repeating record separator. Example data: a file called test.out CREATE VIEW view1 AS something FROM table1 ,table2 as A, table3 (something FROM table4) FROM... (15 Replies)
Discussion started by: delphys
15 Replies

3. Shell Programming and Scripting

Read paragraph from file

Hi, I have a file containing SQL commands in following format. I need to run the SQLs separately and also print the status of SQL, successful/unsuccessful. File : SQL.dat ## SQL1 select * from dual; ## SQL2 select user from dual10; Expected output: SQL1:PASS SQL2:FAIL Started... (3 Replies)
Discussion started by: bhupinder08
3 Replies

4. Shell Programming and Scripting

How to read all data after a specific string from a text file ?

Hi, I have a file(input.txt) and trying to format as output.txt. See the attached file format. Note: This is a windows file (DOS format) and the commands are also going to execute on windows. Basically I am trying to capture all the data in between Local Group Memberships and Global Group... (10 Replies)
Discussion started by: Monoj2014
10 Replies

5. Windows & DOS: Issues & Discussions

Removing anything from text file except specific string

So, I have a text file that looks like this: 0,0: (168,168,176) #A8A8B0 srgb(168,168,176) 1,0: (168,168,176) #A8A8B0 srgb(168,168,176) 2,0: (166,166,174) #A6A6AE srgb(166,166,174) 3,0: (166,166,174) #A6A6AE srgb(166,166,174) 4,0: (168,168,176) #A8A8B0 srgb(168,168,176) 5,0:... (0 Replies)
Discussion started by: pasc
0 Replies

6. Shell Programming and Scripting

Print each paragraph in a text file into excel file

Hi, I have a below text file, which needs to be printed into attached excel file. Please help me. Thanks (4 Replies)
Discussion started by: prash358
4 Replies

7. Shell Programming and Scripting

Easy way to pull a paragraph from a text file?

I have a collection of text files that comprise a mailing list archive. I grep them to find an email that interests me, then open the file in a text editor to find the surrounding paragraph of text. Is there an easy way to do this from the shell instead? (2 Replies)
Discussion started by: CRGreathouse
2 Replies

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

9. Shell Programming and Scripting

Replacing a paragraph between pattern , with the content 4m another file

hi, i wanted to put the output of file f1 into the pattern space of file f2 f1: wjwjwjwjwjwjwj //these line go in file f2 jwjwjwjwjwjjwjw wjwjwjwjjwjwjwj f2: Pattern_start __________ //these are the line to be replaced __________ Pattern_end i m... (4 Replies)
Discussion started by: go4desperado
4 Replies

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

Featured Tech Videos