Find the answer to your Linux question:
Results 1 to 5 of 5
Hello, I have a script that works pretty good except one part where I try to use a file with one item on each line as an input to sed. ...
  1. #1
    Just Joined!
    Join Date
    Nov 2009
    Posts
    3

    Unhappy bash help with file input to sed and awk command?

    Hello,
    I have a script that works pretty good except one part where I try to use a file with one item on each line as an input to sed. Can someone tell me why this bash script is not functioning. It is something with my while loop that is not working I think.

    sed 's/ //g' $input >$output
    sed 's/filter3/filter2/g' $output >$output2

    cat $output2 |while read line
    do
    sed '/$line/d' $input2 >diditwork.txt
    done

    Many Thanks in Advance.
    Bryan

  2. #2
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    Change
    sed '/$line/d' $input2 >diditwork.txt
    to
    Code:
    CMD="sed '/$line/d' $input2 >diditwork.txt"
    eval $CMD

  3. #3
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    A couple of things.

    First, by using single quotes the $line won't be replaced by its value. Use double quotes.

    Second, redirecting the sed output with '>' will create a new file every time sed is executed. May be you want to append to the file with '>>'?

    Code:
    sed "/$line/d" $input2 >> diditwork.txt

  4. #4
    Just Joined!
    Join Date
    Nov 2009
    Posts
    3
    secondmouse,lomcevak,
    I tried the double quotes and the eval CMD suggestions and that gave me the same result.

    Maybe I can tell you what I am trying to do and you can suggest the best course of action?

    I have a file:
    george=filter2
    george=filter3
    fred=filter2
    fred=filter3
    phil=filter2
    phil=filter3
    bill=filter3
    mark=filter2

    I have a second file with the users who are the duplicate entries I need deleted
    george=filter2
    phil=filter2


    So, there you have it, I thought I was on the right track....
    Anyone's help is surely appreciated

  5. #5
    Just Joined!
    Join Date
    Nov 2009
    Posts
    3
    I tried this suggestion from Andrew on another forum and it worked for me!

    Your problem is on the sed line in your while loop..."$line" is inside
    of single quotes, which means sed is searching for the literal string
    "$line" rather than the variable you intend to be placed there. You
    could try using double-quotes instead, or replace that command with:
    cat >> tmpfile <<HERE
    /$line/d
    HERE
    sed -f tmpfile $input2 > diditwork.txt

    I know that's really ugly, but it should work.
    -Andrew

    --
    Andrew Vandever

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...