Deleting a line from a file with sed and awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting a line from a file with sed and awk?
# 1  
Old 08-20-2009
Deleting a line from a file with sed and awk?

cat file.txt
Code:
fvnuiehuewf
ruevhxncvkjrh
zxjvurhfuwe
jkhvBEGINvfnvf
ijrgioe

Trying to delete a line that has the pattern "BEGIN"

cat sedtest
Code:
filename=file.txt
pattern=BEGIN

sed "/^$pattern/d" "$filename"

# 2  
Old 08-21-2009
Code:
sed "/$pattern/d" file

# 3  
Old 08-21-2009
Quote:
Originally Posted by danmero
Code:
sed "/$pattern/d" file

Thank you.
How can I save this output in a variable and print that result (from variable)?
# 4  
Old 08-21-2009
Code:
# VAR=$(sed "/$pattern/d" file)
# echo $VAR
fvnuiehuewf ruevhxncvkjrh zxjvurhfuwe ijrgioe
# echo "$VAR"
fvnuiehuewf
ruevhxncvkjrh
zxjvurhfuwe
ijrgioe

# 5  
Old 08-21-2009
Quote:
Originally Posted by danmero
Code:
# VAR=$(sed "/$pattern/d" file)
# echo $VAR
fvnuiehuewf ruevhxncvkjrh zxjvurhfuwe ijrgioe
# echo "$VAR"
fvnuiehuewf
ruevhxncvkjrh
zxjvurhfuwe
ijrgioe

How to print the result using sed and pipe "|" ?
# 6  
Old 08-21-2009
Code:
#!/usr/bin/bash
filename=file.txt
pattern=BEGIN
cat "$filename" | sed "/$pattern/d"

# 7  
Old 08-21-2009
Quote:
Originally Posted by Leo Gutierrez
Code:
#!/usr/bin/bash
filename=file.txt
pattern=BEGIN
cat "$filename" | sed "/$pattern/d"

How to change the contents of that file?That means deleting the line with "BEGIN" pattern from the file?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command within script wrongly deleting the last line

Hi, I have a shell script which has a for loop that scans list of files and do find and replace few variables using sed command. While doing this, it deletes the last line of all input file which is something wrong. how to fix this. please suggest. When i add an empty line in all my input file,... (5 Replies)
Discussion started by: rbalaj16
5 Replies

2. Shell Programming and Scripting

Deleting duplicated chunks in a file using awk/sed

Hi all, I'd always appreciate all helps from this site. I would like to delete duplicated chunks of strings on the same row(?). One chunk is comprised of four lines such as: path name starting point ending point voltage number I would like to delete duplicated chunks on the same... (5 Replies)
Discussion started by: jypark22
5 Replies

3. Shell Programming and Scripting

Using sed for deleting the first word of each line?

sed /'1-2'/&^/ filename suppose there is a file containing three lines , how do we do delete the word from each line? hyter efr frf rerfer efe ewd cdcf evrfgf erfv the output has to look like frf ewd erfv (2 Replies)
Discussion started by: Rajeev Nukala
2 Replies

4. Shell Programming and Scripting

sed command throwing error while deleting a line from a file

Hi all, I ahve a program which has to delete a line in a file... if i run the sed command through shell prompt it works fine. But if run it using code its throwing error. May i know where i am doing wrong. the file has 3 lines # cat /root/.ssh/known_hosts... (4 Replies)
Discussion started by: vivek d r
4 Replies

5. Shell Programming and Scripting

Deleting "user input line number" from a file using sed

Hi I want to delete a line from a txt file for which the line number is user input. Say when user selects 19, the 19th line would be deleted from the file. Can anyone please provide me with a sed one liner for the same... I tried sed -i. The interaction would be like this Enter the line... (4 Replies)
Discussion started by: sudeep.id
4 Replies

6. UNIX for Dummies Questions & Answers

Deleting "user input line number" from a file using sed

Hi I want to delete a line from a txt file for which the line number is user input. Say when user selects 19, the 19th line would be deleted from the file. Can anyone please provide me with a sed one liner for the same... I tried sed -i. The interaction would be like this Enter the line to... (1 Reply)
Discussion started by: sudeep.id
1 Replies

7. Shell Programming and Scripting

sed - deleting each line up to a word

Hi there, I'd like to delete the beginning of a line up until it finds a certain word or character string: in this case, I'd like to delete each line up to the word "mounting". Thanks ;) Susan (12 Replies)
Discussion started by: kitykity
12 Replies

8. Shell Programming and Scripting

Help on Sed/awk/getting line number from file

I Have file1 with below lines : #HostNameSelection=0 :NotUsed #HostNameSelection=1 :Automatic #HostNameSelection=3 :NotForced I have file2 which has similar lines but with different values I want to copy the changes from file1 to file2 ,line by line only if line begins with '#'. for... (7 Replies)
Discussion started by: mvr
7 Replies

9. Shell Programming and Scripting

Deleting the first column with sed,awk or perl

336 brtr 256 hello Output: brtr hello How can i do this with sed,awk or perl? (5 Replies)
Discussion started by: cola
5 Replies

10. Shell Programming and Scripting

deleting particular lines and moving a line up using perl/sed

Hi, I need convert a dump file in the following format : (please note that line numbers are provided for easy look) Original file: 1 2007-10-2482.90 No trade 0 0.00 100000.00 2 100000.00 3 0.00 4 HOLD 5 2007-10-2589.75 Bought 1114 1114 100000.00 0.00 ... (5 Replies)
Discussion started by: sabyasm
5 Replies
Login or Register to Ask a Question