Answer the question
In order to leave comments, you need to log in
How to assemble a team for the SED editor?
I'm trying to build a command for the SED editor through a loop.
Environment: Ubuntu 14 / FreeBSD 8.2
Tools: Large text file, script to process it.
The idea is this: on each pass of the loop, I get 2 numbers, the line number of the beginning of the text fragment and the number of the end of the fragment.
I want to export these fragments at the end of the cycle to another file (ideally deleting from the original file, but later ...), or delete them from the file, as I further tried to implement ...
variable in the body of the cycle:
sedconstructor="$ sedconstructor -e $head_pos,$end_pos d"
at the end of the loop, the variable contains the following value:
echo "$sedconstructor"
-e 1.3 d -e 5.7 d -e 9.11 d -e 13.15 d -e 17 .19d
Perfect, but without '' can't run in sed: sed: -e expression #1, position 3: command missing
Should be like this: -e '1,3 d' -e '5,7 d' -e '9, 11 d' -e '13.15 d' -e '17.19 d'
But I've been struggling for a day - and I can't think of it...
Please tell me - How to revive it.
Here is the script itself:
#!/bin/bash
set -x
touch test.txt
echo {1..20} | sed 's/\ /\n/g' > test.txt
for (( cnt=1; cnt<=20; cnt++ ))
do
head_pos=$cnt
end_pos=$(($cnt+2)) #число отвинта...
cnt=$(($cnt+3))
echo $cnt
sedconstructor="$sedconstructor -e $head_pos,$end_pos d"
done
echo "$sedconstructor"
sed $sedconstructor test.txt > result.txt
Answer the question
In order to leave comments, you need to log in
Answer.
change the variable in the loop to:
sedconstructor="$sedconstructor $head_pos,$end_pos d;"
#remove -e, add ; at the end sed -e "$sedconstructor" test.txt > result.txt
Just remove the space in "...$head_pos,$end_pos d" (to end up
with something like ".... -e 9,11d -e 13,15d ....")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question