LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem with sed (https://www.linuxquestions.org/questions/programming-9/problem-with-sed-785446/)

lemon09 01-28-2010 03:54 PM

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????

pixellany 01-28-2010 04:04 PM

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

lemon09 01-28-2010 04:21 PM

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????????

pixellany 01-28-2010 04:34 PM

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.

pixellany 01-28-2010 04:38 PM

Quote:

Originally Posted by lemon09 (Post 3844163)
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/

lemon09 01-28-2010 04:58 PM

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...

ghostdog74 01-28-2010 07:36 PM

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


pixellany 01-29-2010 07:12 AM

lemon*;
Did you read the tutorial I suggested?

Code:

read -p "enter roll number to delete: " num
sed "/^$num/d" filename > newfilename


colucix 01-29-2010 08:07 AM

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


All times are GMT -5. The time now is 10:55 AM.