0

Requirement: I need to use only grep/cut/join.

I have data like:

  3 abcd
 23 xyz
1234 abc

I want to pipe this data to cut and then extract columns. But, when I am using cut -d' ' -f 1,2 it treats each space as its own column divider. I would prefer the first two rows be trimmed prior to cut. Is there a way?

Example (I have used tr for demonstration purposes of where the spaces are here; it is not allowed in the solution):

$ echo '  3 abcd
23 xyz
1234 abc' | cut -d' ' -f 1,2 | tr ' ' '_'
_
_23
1234_abc

Expected output:

$3 abcd
23 xyz
1234 abc
user10143594
  • 59
  • 1
  • 1
  • 8
  • 1
    Show what you tried as an [mcve], and the resulting output. For example, you should show the `-d` option you use in this context. –  Jan 18 '19 at 16:44
  • @jdv I have editted. Please have a look. – user10143594 Jan 18 '19 at 17:12
  • There was an answer that showed one potential way of massaging the input to remove the leading delimiters with "sed". If you need to preserve those leading delimiters then you have an extra challenge. There are other hints here: https://stackoverflow.com/q/4143252/1531971 (though this talks about squeezing multiple delimiters to a single delimiter, the same tools can be used to massage leading delimiters with a little work.) –  Jan 18 '19 at 17:22
  • At the end, I wish to have only like 3 abcd, 23 xyz,1234 abc – user10143594 Jan 18 '19 at 17:26
  • Strip leading spaces before passing the args onto cut. https://unix.stackexchange.com/q/102008/156990 –  Jan 18 '19 at 17:29
  • The only problem is that I am restricted to not use awk/sed. I can only use grep/cut/join – user10143594 Jan 18 '19 at 17:30
  • That is the sort of detail that SHOULD BE IN THE QUESTION. –  Jan 18 '19 at 17:34
  • I have already mentioned I need to use only cut/join – user10143594 Jan 18 '19 at 17:35
  • Well, and now you say grep as well. Which is it? –  Jan 18 '19 at 17:39
  • `grep -oe "[0-9]*[[:space:]]*[a-z]*$" < ~/data.txt | cut -f1,2 -d" "` is a start, but it will not handle multiple delimiters and may choke on trailing delimiters. –  Jan 18 '19 at 17:48
  • that is awesome. A great thanks. – user10143594 Jan 18 '19 at 17:49

1 Answers1

0

Using just grep, you can accomplish this with the following pipe:

grep -oe "[^ ][^ ]*  *[^ ][^ ]*$"

grep    # a tool for matching text
  -o    # only prints out matching text
  -e    # uses a regex
  [^ ]  # match anything that isn't a space
  *     # match zero or more of the previous element
  $     # the end of the line

Note: This does not account for trailing whitespace.

Demonstration:

$ echo '  3 abcd
 23 xyz
1234 abc' | grep -oe "[^ ][^ ]*  *[^ ][^ ]*$"
3 abcd
23 xyz
1234 abc
Ian MacDonald
  • 13,472
  • 2
  • 30
  • 51