SaKer
SaKer

Reputation: 107

sed/grep after a FOR cycle

Sorry I was not sure how to set a proper title but I will explain my problem.

I have a bash script and I have a part of code with a for cycle in an IF statementwhere I'm reading lines from a temp. file and seding/grep/editing them. Everything inside this cycle works fine and as expected, the output is 100% correct.

if ...
for (( i=0; i<${lenght_temp}; i++))
do stuff
done
fi

The thing is, I want to edit this output after it leaves the "for cycle". To be more specific, I need to add a header (so a new first line), footer and edit some words. So I was trying to sed immediately after it leaves the FOR but it doesn't work at all. I was trying something simple as this.

if ...
for (( i=0; i<${lenght_temp}; i++))
do stuff
done
sed something 
fi

I know this might work if I redirect the the output to a temp. file but this is tricky because I'm only allowed to use it once. Can somebody help me?

Thank you.

Upvotes: 0

Views: 87

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207670

You can do this:

#!/bin/bash
if [ 1 -eq 1 ]; then
echo Yes, 1 is 1
fi | sed "s/1/2/g"

Output:

Yes, 2 is 2

To add your title, and footer, try this:

#!/bin/bash
if [ 1 -eq 1 ]; then
echo Yes, 1 is 1
fi | sed -e '1i\
Title
' -e 's/1/2/g' -e '$a\
Footer'

Output:

Title
Yes, 2 is 2
Footer

Upvotes: 2

Related Questions