Ubuntu – Data extraction from a text file using bash

bashtext processing

I am looking for a bash script. In a text file I have data like:

+------+------
| Id   | User | 
+------+------+
| 8192 | root | 
| 8194 | root |
| 8202 | root |
| 8245 | root | 
| 8434 | root |  
| 8754 | root | 
| 8761 | root | 
| 8762 | root | 
| 8764 | root | 
| 8771 | root | 
+------+------+

I want to extract the data like this:

8192,8194,8202,8245,8434,8754,8761,8762,8764

I mean, I need the first field containing numbers, but not the last one, and all the numbers extracted should be separated by commas (,).

Could somebody help me to get it ?

Best Answer

You don't need a script for such a simple thing. You can use awk:

awk '$2 ~ "^[0-9][0-9]*$" { print $2 }' file.txt | head -n -1 | awk '{print}' ORS=',' | sed 's/,$/\n/'

Some explanations:

  • awk '$2 ~ "^[0-9][0-9]*$" { print $2 }' file.txt - print from the file.txt only the fields which are numbers.
  • head -n -1 - remove last line / last number.
  • awk '{print}' ORS=',' - concatenate all lines in one single line, each number separated by ,.
  • sed 's/,$/\n/' - replace last , with a newline character.

Or, shorter:

awk '$2 ~ "^[0-9][0-9]*$" { print $2 }' ORS=',' file.txt | sed 's/,[0-9]*,$/\n/'