How to extract a text portion from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract a text portion from a file
# 1  
Old 07-09-2009
How to extract a text portion from a file

Can some one help me with shell script to extract a text block between two known strings.

The given input file is as below:

Name: abs
Some tesxt....
Some tesxt....
Some tesxt....
end of text
Name: xyz
Some tesxt....
Some tesxt....
Some tesxt....
end of text
Name: efg
Some tesxt....
Some tesxt....
Some tesxt....
end of text

output file should contain

Name: xyz
Some tesxt....
Some tesxt....
Some tesxt....
end of text
# 2  
Old 07-09-2009
Example with sed:
Code:
sed '/Name: xyz/,/end of text/!d' infile

Example with awk:
Code:
awk '/Name: xyz/,/end of text/ {print}' infile

Check for "addressing" or "ranges" if you want to read up about it.
# 3  
Old 07-09-2009
try:

Code:
sed '/Name: xyz/,/end of text/!d' filename

# 4  
Old 07-09-2009
If you are looking for the entire name searches in the file try

sed '/Name: /,/end of text/!d' <filename>
# 5  
Old 07-09-2009
Hi Thanks

awk '/Name: xyz/,/end of text/ {print}' infile

is working fine!!

One more small request..

If the pattern of the input file is

line1..
line2..
Name: abs
Some tesxt....
Some tesxt....
Some tesxt....
end of text
line1..
line2..
Name: xyz
Some tesxt....
Some tesxt....
Some tesxt....
end of text
line1..
line2..
Name: efg
Some tesxt....
Some tesxt....
Some tesxt....
end of text

how to get the output file as

line1..
line2..

Name: xyz
Some tesxt....
Some tesxt....
Some tesxt....
end of text

using awk
# 6  
Old 07-10-2009
hi , if your file is in fixed format that each section contains 5 lines, simply use below should be ok
Code:
sed -n '/xyz/{N;N;N;N;p;}' yourfile

otherise, may try below awk

Code:
awk '{
if($0 ~ /xyz/)
	flag=1
if(flag==1)
	print
if($0 ~ /end of text/)
	flag=0
}' yourfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract a portion of string from each line in Linux

Hi I have to extract the destination path information from each record the file is of variable length so I will not be able to use the print command.The search should start on variable "destinationPath" and it should end at immediate "," also the first field has to be printed Input File:... (7 Replies)
Discussion started by: rkakitapalli
7 Replies

2. Shell Programming and Scripting

Extract portion of data

Hi Gurus, I need some help in extracting some of these information and massage it into the desired output as shown below. I need to extract the last row with the header in below sample which is usually the most recent date, for example: 2012-06-01 142356 mb 519 -219406 mb 1 ... (9 Replies)
Discussion started by: superHonda123
9 Replies

3. Shell Programming and Scripting

How to extract portion of a string?

Hi Gurus, Would like to seek some help on how to extract a portion of string from log's output as shown below. Sample of raw data: piece handle=/test123/disk_dump/test123/df0_cntrl_PCPFCI20120404_68498 tag=TAG20120404T180035 comment=NONE piece... (13 Replies)
Discussion started by: superHonda123
13 Replies

4. Shell Programming and Scripting

parsing a portion of Data from a text file

Hi All, I need some help to effectively parse out a subset of results from a big results file. Below is an example of the text file. Each block that I need to parse starts with "Output of GENE for sequence file 100.fasta" (next block starts with another number). I have given the portion of... (8 Replies)
Discussion started by: Lucky Ali
8 Replies

5. Shell Programming and Scripting

Extracting a portion of data from a very large tab delimited text file

Hi All I wanted to know how to effectively delete some columns in a large tab delimited file. I have a file that contains 5 columns and almost 100,000 rows 3456 f g t t 3456 g h 456 f h 4567 f g h z 345 f g 567 h j k lThis is a very large data file and tab delimited. I need... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

6. Shell Programming and Scripting

extract string portion using sed

Hi All I have 3 files as listed below and highlighted in bold the portions of the filenames I need to extract: TOS_TABIN218_20090323.200903231830 TOS_TABIN219_1_20090323.200903231830 TOS_TABIN219_2_20090323.200903231830 I tried source_tabin_name=`echo $fname | sed 's/_.*//'` but I... (6 Replies)
Discussion started by: santam
6 Replies

7. UNIX for Dummies Questions & Answers

How to extract a portion of text from a log file

I am using Unix on Mac OS X 10.5.6. I am trying to extract the last entry of a log (text) file. As seen below, each log entry looks like the following (date and time change with each log entry): I want the script to extract everything quoted above, including the "===" dividers. ... (2 Replies)
Discussion started by: atilano
2 Replies

8. Shell Programming and Scripting

extract date portion from file

Hi, I have a file where there is a date field (single line variable length file) how to extract just the date portion from it the position of date field may vary anywhere in the line but will always have the format mm-dd-yyyy for eg . xxxxxxxxxxxxxxx09-10-2006xxxxxxxxxxxxxxxxxxxx (5 Replies)
Discussion started by: misenkiser
5 Replies

9. Shell Programming and Scripting

extract a portion of log file

hello, I want to grep the log file according to time and get the portion of log from one particular time to other. I can grep for individual lines by time but how should I print lines continuously from given start time till end till given end time. Appreciate your ideas, Thanks chandra (8 Replies)
Discussion started by: chandra004
8 Replies

10. Shell Programming and Scripting

Separate a portion of text file into another file

Hi, I have my input as follows : I have given two entries- From system Mon Aug 1 23:52:47 2005 Source !100000006!: Impact !100000005!: High Status ! 7!: New Last Name+!100000001!: First Name+ !100000003!: ... (4 Replies)
Discussion started by: srikanth_ksv
4 Replies
Login or Register to Ask a Question