My text file has no delimiter to specify separator just spaces, how do I cut out column 2 to output file,

39    207  City and County of San Francisc   REJECTED          MAT = 0
78    412  Cases and materials on corporat   REJECTED          MAT = 0
82    431  The preparation of contracts an   REJECTED          MAT = 0

So output I need is


It is easiest with awk which treats multiple consecutive spaces as a single one, so

awk '{print $2}' file



But obviously there are many, many other tools which will do the job, even not designed to such task as grep:

grep -Po '^[^ ]+[ ]+\K[^ ]+' file

Use pipes to squeeze the extra whitespaces and send your data (e.g, in columns.txt) into cut:

tr -s ' ' < columns.txt | cut -d" " -f2

In the example data you provided, a single space delimiter puts the data you want in field 5. However, if the first column was numerical and had leading spaces in order to align it to the right, you will need to adjust the field number. Squashing whitespace with tr -s ' ' first avoids having to deal with this.

To send that output into another file use redirection:

tr -s ' ' < columns.txt | cut -d" " -f2 > field2.txt

Using the awk command you could do something like the below which recognises automatically the field you are after because there is data there(?) I need to learn more about awk.

awk -F' ' '{print $2}' columns.txt

as @jimmij said, awk '{print $2}' file is the simplest answer.

If, for some reason, you don't want to use awk and insist on using cut, you can use sed to convert every instance of two or more spaces into a single tab (cut's default delimiter) before piping into cut:

$ sed -e 's/  \+/\t/g' riley.txt | cut -f2 

You can still use single spaces as your delimiter, you'll just have more columns. Increase the value you give to cut -d' ' -f from 2 to 5, or maybe 6. Increment the number until you get the desired results.

grep -Po '^[^ ]+[ ]+\K[^ ]+' file

Above one is very useful when Linux utilities like awk for data extraction does not work if you are changing to "root" user inside a shell script like below one:

sudo -i <<EOF
ps aux | grep -E -i "[l]js"  |grep -v "javaagent" | awk '{print $2}' # So awk won't work here
ps aux | grep -E -i "[l]js"  | grep -v "javaagent" | grep -Po '^[^ ]+[ ]+\K[^ ]+'
  • 1
    The only reason "awk won't work" in your example is that the here-document is unquoted, meaning the $2 would be expanded by the shell to the second positional parameter. The solution is probably not to switch to a grep command but to either escape the $ in $2 as \$2 or to simply quote the whole document by using <<'EOF' in place of <<EOF. Also note that the task in your example is more simply carried out by pgrep.
    – Kusalananda
    Dec 25 '19 at 7:52
  • Thanks a lot @Kusalananda. It worked like a charm. Dec 25 '19 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.