1

Does any one know how to apply sed line by line and not directly on a file text :

for i in $(cat server); do
    Exclude_List="AF|PO" 
    echo $i | egrep $LBB3B
    if [ $? -eq 0 ]; then
    **do my sed on the $i line only then continue on next line**
    fi
done
user1404316
  • 3,028
  • 12
  • 23
Med
  • 125
  • 1
  • 2
  • 13
  • Have no idea what exactly are you looking with that, but you can pass that line to `sed` with _here-string_ `sed .... <<<"$i"` as well as piping to it `echo "$i" | sed .... `. – αғsнιη Mar 21 '18 at 16:21
  • 1
    I edited your question to format the code section. You do that by indenting all lines of code four spaces. As for your question, before you decide you need to use `sed` for something, you need to figure out whether it's the correct tool for the job. `sed` is a ***stream*** *editor*, that's **s**tream **ed**itor. It's not designed for doing what you want. Don't get me wrong; you could wire something up Rube Goldberg style, but don't. Just pick the correct tool for the job. See my answer below. – user1404316 Mar 21 '18 at 16:22

3 Answers3

3

sed already works line by line:

sed -E '/AF|PO/{ ...sed expression to apply when matching... }' server

For example, only print the lines matching that regular expression:

sed -nE '/AF|PO/p' server

The -E flag to sed makes the utility interpret the regular expression as an extended regular expression, and the -n turns off the implicit outputting of every input line.

For doing multiple things to lines matching the expression, enclose these in { ... }. For example, print each matching line twice (and don't print non-matching lines):

sed -nE '/AF|OP/{p;p;}' server
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
1

sed has line addressing capabilities, if that helps you:

$ seq 5 > input
$ sed '3s/.*/jeff/' input
1
2
jeff
4
5
$ sed '3,5s/.*/new/' input
1
2
new
new
new

Instead of using a shell loop to process a text file, consider a tool that more naturally processes text, such as :

$ cat input
AF bar
PO baz
other stuff
more other stuff


$ awk '/(AF)|PO/ { print }' input
AF bar
PO baz

$ awk '/stuff/ && !/more/ { print }' input
other stuff
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
0

That's not what sed is for. You can use the native abilities of the shell do everything "line-by-line".

user1404316
  • 3,028
  • 12
  • 23