Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove characters 1

Status
Not open for further replies.

TheDash

MIS
Mar 25, 2004
171
US
Hello,

I am trying to extract a few characters, characters between Begin and End of a line.

i.e from "abcde Begin xyzpqrs END fghijk" , I want to extract "xyzpqrs".

These might be cols 6 7 8 (they might change), I have to awk 1 and 4 before that.

Any help?
 
awk '{print $3}' filename

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant.
 
Hi

xyzpqrs is not $3, $3 is something else... basically it is unknown column. But it is between Begin and END
 
OK Try

sed "s/^*BEGIN//g" file > file2
sed "s/END*^//g" file2 > file3

The will remove from the start of the line -> BEGIN & END -> to end of line.

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant.
 
Sorry that should be

sed "s/END*$//g" file2 > file3

not

sed "s/END*^//g" file2 > file3



Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant.
 
Or using awk

{
j = index($0,"Begin")
if (!j) next
k = index($0,"END")
if (!k) next
print substr($0,j+5,k-j-5)
}

CaKiwi
 
the same with awk handling multiple 'blocks' - a bit rough, but you get the idea.....

abcde Begin xyzpqrs END fghijk Begin fooooo barrrr END 123
Code:
BEGIN {
  s="^Begin$"
  e="^END$"
}
{
   fl=0
   out=""
   for(i=1; i <= NF; i++) {
     if( $i ~ s) { fl++; continue}
     if( $i ~ e && fl) { print out; fl=0; out=""; continue}
     if (fl) {
        out = ((fl==1) ? "" : (out OFS) ) $i;
        fl++
     }
   }
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Another way:
sed -n 's!.*Begin !!;s! END.*!!p' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Not tested...
[tt]
awk -v FS='(Begin|END)' '{print $2}' file1
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top