LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 01-28-2010, 03:54 PM   #1
lemon09
Member
 
Registered: Jun 2009
Location: kolkata,India
Distribution: Mandriva,openSuse,Mint,Debian
Posts: 259
Blog Entries: 1

Rep: Reputation: 35
Problem with sed


The thing is that the command for sed resembles the following
Code:
sed 'address action' file_name
Now if I want to place another command like grep or cut in the address field how do I do it. Actually I don't know the line number. The user has to give it as an input.

How shall I do that????
 
Old 01-28-2010, 04:04 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Example:

You want the address to be the result of running "ls|grep -o cat"

SED command to print the line containing that result:

Code:
sed -n "/$(ls|grep -o cat)/p" file_name
 
Old 01-28-2010, 04:21 PM   #3
lemon09
Member
 
Registered: Jun 2009
Location: kolkata,India
Distribution: Mandriva,openSuse,Mint,Debian
Posts: 259
Blog Entries: 1

Original Poster
Rep: Reputation: 35
Have a look at the output produced by
Code:
$sed -n "/$(ls|grep a)/p" abc1
sed: -e expression #1, char 5: unterminated address regex
How can I fix it.

Suppose that I have a variable which contains the required line number in a file. Now how can I use this variable as the address for sed command????????
 
Old 01-28-2010, 04:34 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
What does "ls|grep a" produce?

On my system (home folder) it produces 6 filenames, some with periods before the extensiong, and all separated by newlines. Pretty much guaranteed to make the SED addressing go berserk.

In the form I showed, you have to filter the output of the command to get something that is rational to use as an address.
 
Old 01-28-2010, 04:38 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by lemon09 View Post
H

Suppose that I have a variable which contains the required line number in a file. Now how can I use this variable as the address for sed command????????
SED addressing takes numbers and or strings--

eg:
sed -n '1p' filename #prints line 1

sed -n '1,5' filename #prints lines 1-5

sed -n '1,/keyword/' #prints line thru the line containing "keyword"

Go here for a really good SED tutorial: http://www.grymoire.com/Unix/
 
Old 01-28-2010, 04:58 PM   #6
lemon09
Member
 
Registered: Jun 2009
Location: kolkata,India
Distribution: Mandriva,openSuse,Mint,Debian
Posts: 259
Blog Entries: 1

Original Poster
Rep: Reputation: 35
Well let me describe the problem->

Consider the file:
Code:
$ cat student.txt
180:Tridip Neogi:234
190:John Karl:424
160:kartugi Pagi:334
149:Patrick Hootason:424
154:David Hoods:120
The first field is the roll no of a student. Now I have to take a roll no as an input and delete the whole entry making use of SED command.
So can You help me out....



Just as a matter of interest can you tell me some other ways...
 
Old 01-28-2010, 07:36 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
you can just use the shell..no need to call external command

Code:
read -p "Enter number: " number
while IFS=":" read -r  num1 user    num2
do
    case "$num1" in
        $number) ;;
        * ) echo "$num1:$user:$num2";;
    esac
done <"file"
output

Code:
# ./shell.sh
Enter number: 180
190:John Karl:424
160:kartugi Pagi:334
149:Patrick Hootason:424
154:David Hoods:120
OR, using just awk

Code:
awk 'BEGIN{
   OFS=FS=":"
   printf "Enter number: "
   getline num <"-"
} $1!=num  '  file
 
Old 01-29-2010, 07:12 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
lemon*;
Did you read the tutorial I suggested?

Code:
read -p "enter roll number to delete: " num
sed "/^$num/d" filename > newfilename
 
Old 01-29-2010, 08:07 AM   #9
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Just a little note: I'd add a colon in the address, to avoid that (for example) 18 deletes all the lines having 180, 181, 182, 183...
Code:
sed "/^$num:/d" filename > newfilename
 
  


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
sed problem... jong357 Programming 11 05-09-2007 12:27 AM
Sed problem Miro1 Programming 3 04-09-2005 09:02 AM
a sed problem thanhVic Programming 5 03-02-2005 01:05 AM
sed problem fossilet Linux - Software 4 12-26-2004 05:44 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM


All times are GMT -5. The time now is 12:45 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