Select text between specific columns in everyline of the output

60 Views Asked by At

I have a output something like :

xxxxxxxxxFOOxxxxxxxxxxxx
blablabl BAR lablblablaa
=========tst============

what can i do for have something like :

FOO
BAR
tst

Which command should i use for only display text between specific column in all line?

cut -d will not work cause its not the same delimiter in every lines ..

PS : Its on android and few command missed ..

thx for helping ...

3

There are 3 best solutions below

1
On BEST ANSWER

cut -d will not work cause its not the same delimiter in every lines ..

Yes but cut -c10-12 does the job, extracting only the 10th, 11th and 12th character of each line. You don’t provide examples of longer lines, so it is not clear what you want to do in that case.

2
On
String[] str = {"xxxxxxxxxFOOxxxxxxxxxxxx",
                "blablabl BAR lablblablaa",
                "=========tst============"};

for (String s : str) {
  int half = s.length() / 2;
  String cutted = s.substring(half - 3, half);
  // or String cutted = s.substring(9, 12);
  // => FOO, BAR, tst
}
1
On

Use a simple method like this:

private static String selectBetween(String input, int numberOfCharacters) {
    int startIndex = (input.length() - numberOfCharacters) / 2;
    return input.substring(startIndex, startIndex + numberOfCharacters);
}

And then you just call this: String output = selectBetween("123123ABC123123", 3);// output will be ABC