sed text extraction between 2 patterns using variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed text extraction between 2 patterns using variables
# 1  
Old 03-21-2013
sed text extraction between 2 patterns using variables

Hi everyone!

I'm writting a function in .bashrc to extract some text from a file. The file looks like this:
" random text
Begin CG step 1
random text
Begin CG step 2
...
Begin CG step 100
random text"

For a given number, let's say 70, I want all the text between "Begin CG step 70" and "Begin CG step 71" to be saved in one file. I saw one can use sed for this, but I don't know how to change the code in such a way that variables and sed work along. For the moment the code looks like this:

Code:
 step()
 {
# $1 step number
# $2 data file
 STEP=$1
 STER=$(($STEP + 1))
 FILE=$2

  sed -n "/Begin CG move =   "{$STEP}"/,/Begin CG move =    "{$STER}"/p" $FILE > step.$STEP.tmp

I also tried
Code:
sed -n '/Begin CG move =   "$STEP"/,/Begin CG move =    "$STER"/p' $FILE > step.$STEP.tmp

none of them write something usefull (in fact they don't write anything)

Thank you!

r
# 2  
Old 03-21-2013
Code:
x=70
y=$(( $x + 1 ))
sed -n "/Begin CG step $x/,/Begin CG step $y/p" $file

or
Code:
awk '/Begin CG step '$x'/ {f=1;next} /Begin CG step '$y'/ {f=0;next} f==1 {print}' file


Last edited by balajesuri; 03-21-2013 at 01:16 PM..
This User Gave Thanks to balajesuri For This Post:
# 3  
Old 03-21-2013
Code:
STEP=$1
FILE=$2

sed -n "/Begin CG move =   $STEP/,/Begin CG move =    $((STEP+1))/p" $FILE > step.$STEP.tmp

This User Gave Thanks to anbu23 For This Post:
# 4  
Old 03-21-2013
Thank you balajesuri & anbu23, for sharing your ideas; they both worked.

Anyway if somebody is going to use this for his/her work, mind the spaces between "=" and the number (I unintentionally wrote "Begin CG step 1" (in the sample) when one should read "Begin CG move = 1").
# 5  
Old 04-03-2013
One more thing; I noticed that the input file has some strange behavior, meaning the last digit always is written on the same position (I've put the an A instead of space character)
Code:
Begin CG move =AAAAAA1
random text
Begin CG move =AAAAA41
random text
Begin CG move =AAAA410
random text

Can sed&awk still be used to extract the random text between two consecutive steps?

Thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extraction problem with sed command

Hi All I am trying to remove the line having specific pattern from a file by using sed command I have the file named ODS_REP_SRCE_File.txt with content as: ... (1 Reply)
Discussion started by: Shilpi Gupta
1 Replies

2. Shell Programming and Scripting

Text extraction

Dear All, I am trying to extract text from a file containing cron entries. cat /var/tmp/cron_backups/debmed_tmp < * * * * * /bell > * * * * * /belly what I am trying to do is create two text files containing all entries that begin with < and another text files containing entries with > .... (4 Replies)
Discussion started by: Junaid Subhani
4 Replies

3. UNIX for Dummies Questions & Answers

awk/sed match and extraction

Hi, I have a file like this- aa 12 23 34 aa 21 34 56 aa 78 45 56 I want to print out only the lines after the last aa. How do I do this? I tried using grep -A and sed -n, but both didnt work as I wanted to. Could someone help me out please.. (3 Replies)
Discussion started by: jamie_123
3 Replies

4. Shell Programming and Scripting

Obscure sed extraction syntax

Hi, Could anyone clearly explain me the below sed construct in detail to get to know what it actually does? sed 's/\(* *\)//4' echo 'test;10;20' | sed 's/*;\(*\)/\1/' (1 Reply)
Discussion started by: royalibrahim
1 Replies

5. Shell Programming and Scripting

Extraction of text using sed or awk command

Hi All, I need to extract 543 from the command below : # pvscan PV /dev/sdb1 VG vg0 lvm2 Total: 1 543.88 GB] / in use: 1 / in no VG: 0 I have the following command which does the job, but I think this could be achieved in a more simple way using sed or awk. Any help is... (7 Replies)
Discussion started by: nua7
7 Replies

6. UNIX for Dummies Questions & Answers

String extraction from a text file

The following script code works great for extracting 'postmaster' from a line of text stored in a variable named string: string="PenaltyError:=554 5.7.1 Error, send your mail to postmaster@LOCALDOMAIN" stuff=$( echo $string | cut -d@ -f1 | awk '{ print $NF }' ) echo $stuff However, I need to be... (9 Replies)
Discussion started by: cleanden
9 Replies

7. Shell Programming and Scripting

extraction of samba shares with sed

Hi there, My samba configuration file looks like that : ... ... path = /home/samba/profiles/ ... path = /home/samba/shares/family valid users = family path = /home/samba/shares/admins valid users = admins path = /home/samba/shares/publicI want to extract the list of standard... (3 Replies)
Discussion started by: chebarbudo
3 Replies

8. UNIX for Dummies Questions & Answers

Copying Text between two unique text patterns

Dear Colleagues: I have .rtf files of a collection of newspaper articles. Each newspaper article starts with a variation of the phrase "Document * of 20" and is separated from the next article with the character string "===================" I would like to be able to take the text composing... (3 Replies)
Discussion started by: spindoctor
3 Replies

9. Shell Programming and Scripting

extraction of perfect text from file.

Hi All, I have a file of the following format. <?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rolename="tomcat"/> <role rolename="role1"/> <role rolename="manager"/> <role rolename="admin"/> <user username="tomcat" password="tomcat" roles="tomcat"/> <user... (5 Replies)
Discussion started by: nua7
5 Replies

10. Shell Programming and Scripting

SED scipt help - line extraction

Forgive me if this is a dumb question...I'm a Windows sys admin with little programming knowledge. I have files containing anywhere from 3 to 200 lines. Using SED, I want to extract only lines containing a specific instance of "ISS." It is possible that "ISS" will occur several times in a... (10 Replies)
Discussion started by: thuston22
10 Replies
Login or Register to Ask a Question