Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 17862  / 1 Year ago, wed, april 5, 2023, 10:03:52

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 ?


More From » bash

 Answers
7

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/,$/
/'


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/,$/
    /'
    - replace last , with a newline character.



Or, shorter:



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

[#26833] Wednesday, April 5, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
raacket

Total Points: 198
Total Questions: 114
Total Answers: 111

Location: Czech Republic
Member since Mon, May 15, 2023
1 Year ago
raacket questions
;