LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-27-2005, 04:35 AM   #1
simon_qwl
Member
 
Registered: Jul 2004
Posts: 34

Rep: Reputation: 15
shell script: File Manipulation


hi,everyone!i want write a shell script which can delete certain line in a file based on the user's selection.
user will give the string pattern in command line and the shell script will delete any matched lines in a file.i tried to use 'sed' to achieve this,however sed command considers the '$1' is a string pattern rather than a varaible. it will search the file with string pattern '$1'.
thanks a lot for any helps!
 
Old 10-27-2005, 05:10 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Hi,
Something like this ...
Code:
#!/bin/bash

echo
echo ""
echo "Enter string: "
read info
echo

file="/home/file.txt"
stuff=`cat $file | grep -n "$info" | cut -d: -f1`
cat $file | sed -e ''$stuff'd'
 
Old 10-29-2005, 03:40 PM   #3
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
Code:
stuff=`cat $file | grep -n "$info" | cut -d: -f1`
cat $file | sed -e ''$stuff'd'
Please don't be a process hog (and your quoting leaves a bit to be desired ;-) ):
Code:
stuff=`grep -n "$info" $file | cut -d: -f1`
sed -e "/$stuff/d" $file
Simon, if you use double quotes around the $1, you'll find sed will work perfectly.
Code:
sed "/$1/d" file
Learn how to use quotes properly and you'll find scripting becomes much easier.

Last edited by eddiebaby1023; 10-29-2005 at 03:43 PM.
 
  


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
cannot execute script file from shell shahrahulb Linux - General 4 03-04-2008 07:02 AM
File Size Shell Script BlackLabel Programming 7 11-27-2007 07:48 PM
date manipulation in shell peal_ss Programming 4 03-29-2004 04:43 AM
Search for a file using shell script sharathkv Linux - Newbie 2 08-06-2003 05:05 AM
File Maintenance in a shell script? mdscudder Linux - General 1 02-20-2003 05:14 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration