Return Only A Portion Of A Line After Matching A Pattern

Lets say a file (fileName) contains the following data

abcdddddfffbbbbbdddStartPatternSomeData1EndPattern
abcdddddfffdfdfsdsfStartPatternSomeData2EndPattern
fsdfdsddfdffbbbbbdddStartPatternSomeData3EndPattern
abcdddfsdfsdfdsbdddStartPatternSomeData4EndPattern

and you are interested in

SomeData1

SomeData2

SomeData3

SomeData4

Following command Would help do that

cat fileName| sed -n -e  ‘s/^.*StartPattern//p’ | sed -n -e ‘s/EndPattern.*$//p’

Here :

  • -n means not to print anything by default.
  • -e is followed by a sed command.
  • s is the pattern replacement command.
  • The final p means to print the transformed line.

Accessing Siteminder Protected HTTP Resources

 

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.List;
import javax.net.ssl.SSLContext;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.util.EntityUtils;
//http://www.codeproject.com/Articles/80314/How-to-Connect-to-a-SiteMinder-Protected-Resource
public class AccessSiteminderProtectedResource {
private static final String PASSWORD = "pwd";
private static final String USER_NAME = "userId";
private static final String SITEMINDER_PROTECTED_RESOURCE = "protectedResource";
private static final String SITEMINDER_LOGIN_URL = "siteMinderLoginUrl?TARGET=-SM-" + SITEMINDER_PROTECTED_RESOURCE;
public static void main(String[] args) throws Exception {
BasicCookieStore cookieStore = new BasicCookieStore();
SSLContext sslcontext = buildSSLContext();
SSLConnectionSocketFactory sslsf = buildSSLConnectionSocketFactory(sslcontext);
CloseableHttpClient httpclient = buildHttpClient(cookieStore, sslsf);
try {
String nextLocation = executeLogin(cookieStore, httpclient);
accessApp(httpclient, nextLocation);
} finally {
httpclient.close();
}
}
private static SSLContext buildSSLContext()
throws NoSuchAlgorithmException, KeyManagementException,
KeyStoreException {
SSLContext sslcontext = SSLContexts.custom()
.setSecureRandom(new SecureRandom())
.loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
return sslcontext;
}
private static SSLConnectionSocketFactory buildSSLConnectionSocketFactory(
SSLContext sslcontext) {
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
return sslsf;
}
private static CloseableHttpClient buildHttpClient(
BasicCookieStore cookieStore, SSLConnectionSocketFactory sslsf) {
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf).setDefaultCookieStore(cookieStore)
.setRedirectStrategy(new LaxRedirectStrategy())
.build();
return httpclient;
}
private static String executeLogin(BasicCookieStore cookieStore,
CloseableHttpClient httpclient) throws URISyntaxException,
IOException, ClientProtocolException {
HttpUriRequest loginPost = RequestBuilder
.post()
.setUri(new URI(SITEMINDER_LOGIN_URL))
.addParameter("USER", USER_NAME)
.addParameter("PASSWORD", PASSWORD).build();
System.out.println("executing request" + loginPost.getRequestLine() + "\n");
CloseableHttpResponse loginResponse = httpclient.execute(loginPost);
String nexLocation;
try {
HttpEntity loginResponseEntity = loginResponse.getEntity();
System.out.println("Login form post Status: " + loginResponse.getStatusLine());
EntityUtils.consume(loginResponseEntity);
System.out.println();
System.out.println("Post logon cookies:");
System.out.println();
displayCookies(cookieStore);
System.out.println();
System.out.println();
System.out.println("Login Post Headers----------------------------------------");
displayHeaders(loginResponse);
System.out.println();
System.out.println();
nexLocation = SITEMINDER_PROTECTED_RESOURCE;
} finally {
loginResponse.close();
}
return nexLocation;
}
private static void accessApp(CloseableHttpClient httpclient, String nextLocation) throws IOException, ClientProtocolException {
HttpGet appGet = new HttpGet(nextLocation);
System.out.println("executing request" + appGet.getRequestLine());
CloseableHttpResponse response = httpclient.execute(appGet);
try {
HttpEntity entity = response.getEntity();
System.out.println("\n\n\n\n---------------------------------------- \n");
System.out.println("App Get Status: " + response.getStatusLine());
System.out.println(EntityUtils.toString(entity));
EntityUtils.consume(entity);
} finally {
response.close();
}
}
private static void displayHeaders(CloseableHttpResponse loginResponse) {
for (Header header : loginResponse.getAllHeaders()) {
System.out.println(header);
}
}
private static void displayCookies(BasicCookieStore cookieStore) {
List<Cookie> cookies = cookieStore.getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
}
}

 

Locations of visitors to this page