LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-27-2013, 12:19 PM   #1
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Rep: Reputation: Disabled
delete a line in a file


How can i delete a line please?
For example i have this file.txt
Code:
5=e
4=a
3=c
2=b
1=k
and i wat with another script to delete the line where is the 5 but not only in the result i want to delet it forever
thanks
 
Old 09-27-2013, 12:34 PM   #2
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
If 5=e is the first line always then,
Code:
sed -i '1d' /home/path/to/files.txt
Else, more info.
 
Old 09-27-2013, 12:42 PM   #3
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
If 5=e is the first line always then,
Code:
sed -i '1d' /home/path/to/files.txt
Else, more info.
no is not the fhirs line
i want to delet the line where is finding the string which i give it no mather cuz is fhirs second third fourt or anywhere else i want to delete the entire line where is finding the given string
 
Old 09-27-2013, 12:58 PM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
http://sed.sourceforge.net/sed1line.txt

don't forget you need the "" with the -i on macOSX
Code:
sed -i "" -e '<see link for this bit>' /path/to/sh.db
 
Old 09-27-2013, 02:41 PM   #5
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
don't forget you need the "" with the -i on macOSX
Note to self: sparky90ful is a MacOS user.
 
Old 09-27-2013, 03:10 PM   #6
WarTurkey
Member
 
Registered: Jun 2009
Posts: 67

Rep: Reputation: 16
Could do:

Code:
if grep -V '5=e' ./input_file.txt > ./output_file.txt ; then mv ./output_file.txt ./input_file.txt ; fi
EDIT: Nevermind, I am not sure that this would work on OSX :\

Last edited by WarTurkey; 09-27-2013 at 03:11 PM.
 
Old 09-27-2013, 03:26 PM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
seems a lot of work when

Code:
Var="5=e"
sed -i "" -e "/${Var}/d" /path/to/Input
will do it
, yes I know, no real need for the ${Var}
... unless you are writing a script, and 5=e wasn't always 5=e
 
Old 09-28-2013, 03:46 AM   #8
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
i have did it with
Code:
grep -w "^$key" sh.sh
but now i am stuck in another thing i want to dysplay the value where exist my result of grep for example i have this:
Code:
1=a
2=b
3=c
4=e 
5=f
6=g
7=h
8=i
9=j
10=k
11=l
12=m
13=n
14=o
15=p
16=q
17=r
18=s
19=t
20=u
a=1
b=2
c=3
d=4
e=5
f=6
g=7
h=8
i=9
j=10
k=11
i want to when i grep <1> to print what is after the " = " but i cant do it because my grep print even if he find <1> after the " ="
so i want to search the lines befor the " = " and print what is after " = "
i dont know if you understand but i realy need help

Last edited by sparky90ful; 09-28-2013 at 04:05 AM.
 
Old 09-28-2013, 04:08 AM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by sparky90ful View Post
i have did it with
Code:
grep -w "^$key" sh.sh
the problem with using grep is you need to write a temporary file and then move it to your target file
sometimes it is convenient to do it this way, for instance when working on the command line, trying a few things out.
But for scripts, it is not the correct tool.

grep = global regular expresion print

sed = stream editor

which should you use to edit a file

Last edited by Firerat; 09-28-2013 at 04:09 AM.
 
Old 09-28-2013, 04:15 AM   #10
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
the thing is for example i want to serch 1 so i want cuz he find me the lines where is 1 but before " = " so i dont wnt cuz he find <a=1> or <ddd=321> i wat to find <1=ghfh> or <211=op14> or <421=kj5417> and when he find this print what is after " = " so the result will be " ghf " " op14 " " kj5417 " and that is i hope now is god explained

Last edited by sparky90ful; 09-28-2013 at 04:20 AM.
 
Old 09-28-2013, 04:24 AM   #11
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by sparky90ful View Post
but now i am stuck in another thing i want to dysplay the value where exist my result of grep for example i have this:
Code:
g=7
h=8
i=9
j=10
k=11
i want to when i grep <1> to print what is after the " = " but i cant do it because my grep print even if he find <1> after the " ="
so i want to search the lines befor the " = " and print what is after " = "
i dont know if you understand but i realy need help
I'm going to introduce you to awk

like sed you can have 'awk one liners' , do a web search

awk is very powerful

but here we shall use the basics

Code:
awk -F= '$1 == "g" {print $2}' sh.db
can make it easier to read/understand

Code:
awk -F= '{ if ( $1 == "g" ) {print $2} }' sh.db
-F< field separator >

$1 $2 .... are the fields, don't get them confused with shell script variables
 
Old 09-28-2013, 04:35 AM   #12
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
I'm going to introduce you to awk

like sed you can have 'awk one liners' , do a web search

awk is very powerful

but here we shall use the basics

Code:
awk -F= '$1 == "g" {print $2}' sh.db
can make it easier to read/understand

Code:
awk -F= '{ if ( $1 == "g" ) {print $2} }' sh.db
-F< field separator >

$1 $2 .... are the fields, don't get them confused with shell script variables
i can't understand this.......soo awk=search -F=field separator so i tell him to search a field separator and do wat "g" i replace g with what i search befor field separator??? for my example yo do how with awk?
Code:
the thing is for example i want to serch 1 so i want cuz he find me the lines where is 1 but before " = " so 
i dont wnt cuz he find <a=1> or <ddd=321> 
i wat to find <1=ghfh> or <211=op14> or <421=kj5417> and when he find this print what is after " = " 
so the result will be " ghf " " op14 " " kj5417 " and that is  i hope now is god explained

Last edited by sparky90ful; 09-28-2013 at 04:36 AM.
 
Old 09-28-2013, 04:51 AM   #13
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
this Input
Code:
foo=bar
barbar=foo
foobar=barfoo
Code:
awk -F= '$1 == "foo"{print $2}' Input
gives you


bar

long hand
fields are separated by =
if field 1 is equivalent to "foo" then print field 2


I shall expand the example
Code:
awk -v KEY=foo -F= '$1 == KEY {print $2}'
 
Old 09-28-2013, 04:54 AM   #14
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
i think i get it i test it and i come back with a replay
now i see but the thing is i want to give me not only "bar" but even "barfoo" so foo must be the right word searched or foobar or coolfooten or any who contain tis foo i want what is after =

Last edited by sparky90ful; 09-28-2013 at 04:58 AM.
 
Old 09-28-2013, 04:55 AM   #15
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
simpler grep and cut

Code:
KEY=foo
grep "^${KEY}=" sh.db | cut -d= -f2
 
  


Reply



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
How to delete single line from file using line number. gajananh999 Linux - Newbie 1 08-13-2012 05:25 AM
script to read and delete a line from a file, if the line is 30 days old freakin.raja Linux - Newbie 7 09-17-2011 08:52 AM
Perl question: delete line from text file with duplicate match at beginning of line mrealty Programming 7 04-01-2009 06:46 PM
delete a line containing a pattern and the next line of a text file powah Programming 3 01-31-2007 05:34 PM
Delete line from file nebbus Linux - Newbie 3 06-03-2006 12:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:16 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration