1
Controller loading lists... ------------------------------------------------- command: select SERVICE_NAME from <table_name> ------------------------------------------------- SERVICE_NAME 1 first service 2 second service 

i need to filter out this output in Linux and get the result as

first_service,second_service

2
  • Is it one record per file, or are records concatenated (i.e. it restarts at the bottom with Controller loading lists... or similar)?CommentedNov 2, 2022 at 3:31
  • You would do this in the database engine (using e.g. GROUP_CONCAT() or similar functionality), not by postprocessing the output. Unfortunately, you never say what SQL database backend you're using.
    – Kusalananda
    CommentedMar 3, 2024 at 11:40

3 Answers 3

1

Using GNU sed

$ sed -Ez 's/[^0-9]*[^[:alpha:]]*([^ ]*) ([^\n]*)\n?/\1_\2,/g;s/,$/\n/' input_file first_service,second_service 
1
  • @LeoShukla Did you copy and paste the code correctly? I have added an optional new line in case that is where your issue is. You can check here for a demo ideone.com/F3AMmC
    – sseLtaH
    CommentedNov 2, 2022 at 13:35
0

Using Raku (formerly known as Perl_6)

~$ raku -e 'my $i=0; my @a; for lines() { \ $i=1 and next if /^ \s+ SERVICE_NAME /; \ if ($i.Bool) { @a.push: .words.skip.join("_") } else {next};}; \ .put for @a.join(",");' file 

OR

~$ raku -e 'my @a; for lines() { \ @a.push( $_.words.skip.join: "_" ) if /^ \s+ SERVICE_NAME / ^fff * }; \ .put for @a.join(",");' file 

Sample Input:

Controller loading lists... ------------------------------------------------- command: select SERVICE_NAME from <table_name> ------------------------------------------------- SERVICE_NAME 1 first service 2 second service 

Sample Output:

first_service,second_service 

Raku is a programming language in the Perl-family. The project was started in year 2000 by Larry Wall and colleagues. The first release was in December 2015. The Perl6 language was renamed Raku in 2019.

First answer above: the code declares a scalar $i and an array @a. Input is taken with lines(). Inside the {...} block, a sentinel line ^ \s+ SERVICE_NAME is searched for. If found, $i is set to 1 and the code proceeds to the next line.

From here, the last two statements run if $i coerced to a Boolean is True. When $i.Bool is True, the {...} block is entered: the line is broken into whitespace separated .words, the first word (i.e. the number) is skipped and the remainder joined with underscores. Finally, the line is output, joined with commas.

Second answer above: The code uses Raku's sed-like "flip-flop" operator, fff and variants. The ^fff variant in the code turns ON for capturing lines at the line after the SERVICE_NAME sentinel line is found. The * means all lines until the end of the target file are pushed onto the array. See discussion above for text-processing and output.

https://docs.raku.org/language/operators#infix_^fff
https://raku.org

    0

    There's definitely room for improvement in this solution. If I was better with sed, I feel like this out to be condensable into a single invocation of sed. Even the grep could be eliminated if I knew how to tell sed to delete pattern spaces that don't begin with a string of digits. Better still would likely be awk.

    grep -E '^[0-9]+' input_file | sed -E -e 's/^[0-9]+ +//' -e 's/ /_/g' | sed -e ':a' -e 'N;$!ba' -e 's/\n/,/g' $ cat << EOF > input_file Controller loading lists... ------------------------------------------------- command: select SERVICE_NAME from <table_name> ------------------------------------------------- SERVICE_NAME 1 first service 2 second service 39 third of four 427 fourth and final service EOF $ grep -E '^[0-9]+' input_file | sed -E -e 's/^[0-9]+ +//' -e 's/ /_/g' | sed -e ':a' -e 'N;$!ba' -e 's/\n/,/g' first_service,second_service,third_of_four,fourth_and_final_service 
    2
    • "1 first_service,2 second_service" - this is the output that i am getting with this one . looks like the numbers also need to be removedCommentedNov 3, 2022 at 10:18
    • @LeoShukla Thanks for the feedback. Edited. Seems the sed syntax I tried originally needs to be broken up a little.
      – Jim L.
      CommentedNov 3, 2022 at 15:59

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.