SED - Match a line from a File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED - Match a line from a File
# 1  
Old 05-26-2010
SED - Match a line from a File

Hi,
I want to match a line which exists in a file. I have written a test script similar to below -
The content of the file file.txt would be like this -
Code:
/usr/bin/1234.xcf
/usr/bin/3456.xcf
/usr/bin/7897.xcf
/usr/bin/2345.xcf


Code:
out=`sed -n '\/usr\/bin\/7897.xcf/p' file.txt 2>&1`
echo $out ##  Prints the expected line

But, my issue is that I will be getting the file contents in the code inside a variable in a while loop. i.e. like file="/usr/bin/7897.xcf". So, now, do I have to decompose this content at runtime to pass it to sed ?

Or is there a better way to achieve this ?
# 2  
Old 05-26-2010
Quote:
Originally Posted by angshuman_ag
... my issue is that I will be getting the file contents in the code inside a variable in a while loop. i.e. like file="/usr/bin/7897.xcf". So, now, do I have to decompose this content at runtime to pass it to sed ?

Or is there a better way to achieve this ?
Here's an idea:

Code:
$ 
$ 
$ cat file.txt
/usr/bin/1234.xcf
/usr/bin/3456.xcf
/usr/bin/7897.xcf
/usr/bin/2345.xcf
$ 
$ 
$ FILE="/usr/bin/7897.xcf"
$ 
$ grep "$FILE" file.txt
/usr/bin/7897.xcf
$ 
$

tyler_durden
# 3  
Old 05-27-2010
Hi,
Why I need this is so that I can delete the matching line from the file. Or I should be able to replace it with a BLANK.

I saw that if I use option "d" in sed command, it echos all the lines except the given one.

Code:
`sed -n '\/usr\/bin\/7897.xcf/d' file.txt 2>&1`

So, if there is a variable, can I do the same thing with sed ? It will be lot more easier.

---------- Post updated at 11:07 PM ---------- Previous update was at 09:08 PM ----------

One solution could be -

In this example, we tell grep to look for "out dated"--with the space in the middle.
This command searches the file "myfile.new" for the text "out dated"--no matter whether upper-case or lower-case letters have been used--and puts all lines that do not have "out dated" in them into the file "myfile.newer".

Code:
grep -iv "out dated" myfile.new > myfile.newer

Courtsey
HTML Code:
http://www.udel.edu/topics/software/general/editors/unix/vi/delsearch.html
# 4  
Old 05-27-2010
Quote:
Originally Posted by angshuman_ag
...
Why I need this is so that I can delete the matching line from the file. Or I should be able to replace it with a BLANK.

I saw that if I use option "d" in sed command, it echos all the lines except the given one.

Code:
`sed -n '\/usr\/bin\/7897.xcf/d' file.txt 2>&1`

So, if there is a variable, can I do the same thing with sed ? It will be lot more easier.
...
Here's a Perl solution:

Code:
$
$
$ # display the content of the data file
$ cat -n file.txt
     1  /usr/bin/1234.xcf
     2  /usr/bin/3456.xcf
     3  /usr/bin/7897.xcf
     4  /usr/bin/2345.xcf
$
$ FILE="/usr/bin/7897.xcf"
$
$ # replace the line matching pattern $FILE with a blank space
$ # a backup file is created here
$
$ perl -i.bak -pne "s|^$FILE$| |" file.txt
$
$ # verify the update
$
$ cat -n file.txt
     1  /usr/bin/1234.xcf
     2  /usr/bin/3456.xcf
     3
     4  /usr/bin/2345.xcf
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep or sed - printing line only with exact match

Hello. In my script, some command return : q | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | x86_64 | openSUSE-13.2-Kernel_stable_standard | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | i586 | openSUSE-13.2-Kernel_stable_standard | kernel-default ... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Match pattern1 in file, match pattern2, substitute value1 in line

not getting anywhere with this an xml file contains multiple clients set up with same tags, different values. I need to parse the file for client foo, and change the value of tag "64bit" from false to true. cat clients.xml <Client type"FIX"> <ClientName>foo</ClientName>... (3 Replies)
Discussion started by: jack.bauer
3 Replies

3. Shell Programming and Scripting

Awk-sed help : to remove first and last line with pattern match:

awk , sed Experts, I want to remove first and last line after pattern match "vg" : I am trying : # sed '1d;$d' works fine , but where the last line is not having vg entry it is deleting one line of data. - So it should check for the pattern vg if present , then it should delete the line ,... (5 Replies)
Discussion started by: rveri
5 Replies

4. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

5. Shell Programming and Scripting

SED, match a line block

Hello, I want to do a simple substitution using sed but I can't find a solution. Basically, from a Apache conf file, I would like to remove everything included between the <VirtualHost> and </VirtualHost> e.g SSLMutex file:/var/run/ssl_mutex <VirtualHost _default_:443> # A lot of config that... (5 Replies)
Discussion started by: RobertFord
5 Replies

6. Shell Programming and Scripting

Sed scripting, match text within line and replace

New to sed... Have a file foo.txt (below). Need to replace text on 2 lines, but can only feed sed the first few characters of each line (all lines are unique). So, in my example, I have put '$' in place of what I need to figure out how to feed the whole line. What I have thus far: sed -e... (6 Replies)
Discussion started by: boolean2222
6 Replies

7. Shell Programming and Scripting

SED - adding blank line after each Range Match

the following range matching works great but i wish to add a blank line after each range result set... which i've tried and researched to no avail MY INPUT DATA: CURRENT CODE I'M USING: sed -n '/*$/,/;/p' $INPUT_FILE RESULTS I'M GETTING: RESULT I looking to... (5 Replies)
Discussion started by: danmauer
5 Replies

8. Shell Programming and Scripting

sed: find match and delete the line above

I am searching a dhcpd.conf to find the hardware ethernet match, then once the match is found delete just the line above it. For example: testmachine.example { hardware ethernet 00:00:00:00:00:00; fixed address 192.168.1.100; next-server 192.168.1.101; filename "linux-install/pxelinux.0"; }... (3 Replies)
Discussion started by: cstovall
3 Replies

9. Shell Programming and Scripting

Multiple line match using sed

Please help! Input pattern, where ... could be any number of lines struct A { Blah1 Blah2 Blah3 ... } B; output pattern struct AB { Blah1 Blah2 Blah3 ... }; I need help in extracting everything between { and } if it would have been on a single line { \(.*\)} should have worked. (15 Replies)
Discussion started by: SiftinDotCom
15 Replies

10. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies
Login or Register to Ask a Question