Ubuntu: Redirect filtered output to file



Question:

I redirect my output into a file and I get something like this:

|  id  | code | color_code |   -------+------+------------+  |11A00B|  15  | 9129102    |  |11A00C|  16  | 9129103    |  (2 rows)    |  id  | code | color_code |   -------+------+------------+  |11B00B|  25  | 9129152    |  |11B00C|  36  | 9129162    |  (2 rows)    |  id  | code | color_code |   -------+------+------------+  |11C00B|  22  | 9129107    |  |11C00C|  9   | 9129108    |  (2 rows)  

How can I convert it into csv style like:

|  id  | code | color_code |   |11A00B|  15  | 9129102    |  |11A00C|  16  | 9129103    |  |11B00B|  25  | 9129152    |  |11B00C|  36  | 9129162    |  |11C00B|  22  | 9129107    |  |11C00C|  9   | 9129108    |  

To get the first file the loop runs and starts sql scripts. All I do is simply > file. But I have no control over the loop. Probably info can already be filtered during the iteration. But this is rather doubtful.


Solution:1

awk:

awk 'NR==1; /^\|[0-9]/'  
  • The default action in awk is to print the record

  • NR==1 matches the first record (line)

  • /^\|[0-9]/ matches any record starting with | followed by a digit

Example:

% cat file.txt                      |  id  | code | color_code |   -------+------+------------+  |11A00B|  15  | 9129102    |  |11A00C|  16  | 9129103    |  (2 rows)    |  id  | code | color_code |   -------+------+------------+  |11B00B|  25  | 9129152    |  |11B00C|  36  | 9129162    |  (2 rows)    |  id  | code | color_code |   -------+------+------------+  |11C00B|  22  | 9129107    |  |11C00C|  9   | 9129108    |  (2 rows)    % awk 'NR==1; /^\|[0-9]/' file.txt  |  id  | code | color_code |   |11A00B|  15  | 9129102    |  |11A00C|  16  | 9129103    |  |11B00B|  25  | 9129152    |  |11B00C|  36  | 9129162    |  |11C00B|  22  | 9129107    |  |11C00C|  9   | 9129108    |  


Solution:2

You can pipe your output through this sed command:

sed '1p;/^|/!d;/^|\s*id/d'  

Explanation:

  • The expression 1p just echoes the first line.
  • The expression /^|/!d removes any line not beginning with |.
  • The expression /^|\s*id/d removes any line beginning with |, then some (or none) whitespace characters followed by id. We have already printed our header using the first expression, so it is OK that this expression discards it now.


Solution:3

You can use sed to print only the stuff you want

sed -n '1p; /^|[0-9]/p' file  

Explanation

-n don't print anything until we ask for it
1p print the first line
^|[0-9]/p print the line if it starts with | followed by a number


Solution:4

Here is a little perl script you can use for this:

#!/usr/bin/perl  use strict;  use warnings;    my$file="file.txt";    open(IN,"<",$file);   my$head=<IN>;   print $head;   while(<IN>)  {    print $_ if $_=~/^\|\S+/;  }   close IN;  

This will print the first line of your input file as header and then loop over the rest of the file and print the content lines.

Note:

I am assuming here that your content rows always start with a pipe symbol, followed by one ore more non-whitespace characters (|\S+). Please leave me a comment if that is not the case, then I can adjust the regex.


Solution:5

your_command | grep "|"  

will get rid of those (2 rows) and empty line. You can add another grep -v "id" which will remove all headers, then you just need to add one header with echo command to the beginning.


Solution:6

How about this little shell-script? It uses user596137's answer with grep and writes the output in that file

#!/bin/bash    FILE=$1  FIRST_LINE=`head -n1 ${FILE}`  INPUT=`cat ${FILE} | grep "|" | grep -v "id"`    echo "$FIRST_LINE" > $FILE; echo "$INPUT" >> $FILE  

Save that script and grant execution permission to it. This script needs a file name as the first parameter.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »