LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-19-2012, 08:55 PM   #1
forethinker
LQ Newbie
 
Registered: Mar 2012
Posts: 4

Rep: Reputation: Disabled
using sed to modify a file and name expansion


Hi I have been exploring with bash for a quite bit of time now, but I still am terrible with the syntax. I am just trying to delete a word that I provide using function.
Code:
dr ()
{
  wordlist = ~/scripts/worldlist.txt
  sed -e /"$1"/d $wordlist >| $wordlist
}
There are two things going wrong here, I think
1) the variable wordlist is not expanded
2) sed does not do the substitution even when I give an absolute address to the file.
3) I want it to delete only the word I supply. e.g. shun to delete shun, not both shun and shunt.

What am I do wrong? I have spent two days on this problem. Thanks for the help.

Last edited by forethinker; 03-19-2012 at 08:58 PM.
 
Old 03-19-2012, 09:40 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by forethinker View Post
Hi I have been exploring with bash for a quite bit of time now, but I still am terrible with the syntax. I am just trying to delete a word that I provide using function.
Code:
dr ()
{
  wordlist = ~/scripts/worldlist.txt
  sed -e /"$1"/d $wordlist >| $wordlist
}
There are two things going wrong here, I think
1) the variable wordlist is not expanded
2) sed does not do the substitution even when I give an absolute address to the file.
3) I want it to delete only the word I supply. e.g. shun to delete shun, not both shun and shunt.

What am I do wrong? I have spent two days on this problem. Thanks for the help.

Hi,
Welcome to LQ!

Ad 1): What makes you think that, is the function just sitting there not doing
anything at all till you press Ctrl-C?

Ad 2): Try using sed's -i option ... redirecting into a file that's being read
from may or may not work as expected. I more frequently find that it doesn't
than that it does.

Ad 3): Use anchors. ^ = beginning of line; $ = end of line.


Cheers,
Tink
 
1 members found this post helpful.
Old 03-19-2012, 10:16 PM   #3
forethinker
LQ Newbie
 
Registered: Mar 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thank you for the quick reply.
Number 2 worked. Thank You!
1. No it does not give me any errors, but nothing happens.
To double check, I put the commands on the prompt manually
Code:
$ bar=shun
$ echo $wordlist
/home/me/scripts/worldlist.txt
$ sed -e /"$bar"/d -i $wordlist
$ cat /home/me/scripts/wordlist.txt | grep shun
shunt
shun
3. Could you please tell me where to place ^ and $? This is the way I tried (and some variants of backslash)
Code:
$ echo /^"$bar"\$/d -i /home/me/scripts/wordlist.txt 
/^shun$/d -i /home/me/scripts/wordlist.txt
The second line is the output, but it does not do anything.

Last edited by forethinker; 03-19-2012 at 10:20 PM.
 
Old 03-19-2012, 11:14 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Does this help?
Code:
$ echo -e "shun\nshunt"
shun
shunt
$ echo -e "shun\nshunt"|sed '/shun/d'
$ echo -e "shun\nshunt"|sed '/^shun$/d' 
shunt

Cheers,
Tink

Last edited by Tinkster; 03-19-2012 at 11:16 PM.
 
1 members found this post helpful.
Old 03-20-2012, 01:34 AM   #5
forethinker
LQ Newbie
 
Registered: Mar 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
No,
I understand your code, it unfortunately does not work with the variables.
Do you happen to know what's going on with my script in part 1?
 
Old 03-20-2012, 01:39 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Code:
$ var=shun
$ echo -e "shun\nshunt"|sed '/^shun$/d' 
shunt
$ echo -e "shun\nshunt"|sed /^${var}\$/d 
shunt
$
Does this help?
 
1 members found this post helpful.
Old 03-20-2012, 01:55 AM   #7
forethinker
LQ Newbie
 
Registered: Mar 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thumbs up You were right!!

Hi Tinkster, all of questions were answered.
I had a bit of mix up with wordlist and worldlist.
Thank you so much for being awesome and thanks for being patient with me.
I hope to contribute to this forum soon!
 
Old 03-20-2012, 04:34 AM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Cool, glad I could be of assistance :}

Looking forward to seeing you partake!


Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to get sed to modify the nth line javascriptninja Programming 1 02-25-2012 10:35 AM
how to modify xml file using sed/awk akhand jyoti Linux - Newbie 3 11-29-2011 03:47 PM
modify a file that contains \\ using sed or awk j-me Linux - Newbie 7 05-14-2010 09:30 AM
SED: wildcard expansion in variables Gracie Belle Linux - Newbie 3 02-20-2008 08:43 AM
modify file access & modify timestamps i2itstud Linux - General 1 05-20-2003 04:34 AM


All times are GMT -5. The time now is 03:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration