DomainsFeatured DomainsFeatured - 6 months ago 20
Linux Question

Process Lines Not Containing String And Output Line Below Matched Line Too

I have a good idea of what I can do here, using

grep -v '^--'
grep -A 1
, however I think I might need to use awk to match instead.

I have data that looks like this:

Random text line 1
Random text line 2
Random text line 3
Random text line 4
Random text line 5

The thing is, I need to run a command - let's call it
on all lines NOT starting with '--' and capture the output along with the line below it.

So the three commands I want to combine are:

grep -v '^--' file.txt | command1 > text-output

grep -A 1 [not sure] > --data-below-text

I could probably do this by storing variables in a
while read line; do
type of command and then store and echo variables. However, I feel like there might be an easier way that is more efficient way to get the following:

Random text line 1, text-output, --Data1
Random text line 2, text-output, --Data2
Random text line 3, text-output, --Data3
Random text line 4, text-output, --Data4
Random text line 5, text-output, --Data5

Of course, if using variables is the only way, I'm open to doing that as well. I just wanted to make sure since I know variables can get dicey if I decide to use the code in parallel down the road. Any direction is greatly appreciated.


here's a way:

  in=$(cat -)
  one=$(echo "$in" | head -n1)
  two=$(echo "$in" | tail -n1)
  result=$(echo "$one" | command1)
  echo "$one, $result, $two"

grep -A1 -v '^--' file.txt | command_wrapper

here's another way:

textlines=$(grep -v '^--' file.txt)
results=$(echo "$textlines" | command1)
datalines=$(grep '^--' file.txt)
paste <(echo $textlines) <(echo $results) <(echo $datalines) |
  tr '\t' ','