LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 03-14-2005, 10:54 AM   #1
Erhnam
Member
 
Registered: Jan 2005
Posts: 54

Rep: Reputation: 15
[bash] removing or editing lines in file


I have a example file which contains:

test01:12
test02:16
test03:18
test07:20

Now I want to write a bash script that replaces the 12 for test01 to 14. Is it possible to write such a script with bash? Could anyone give me an example command? And how can I remove for example the line which contains test07 from the script using bash.. Is this possible to do that from the CLI? Any help would be great!!
 
Old 03-14-2005, 11:18 AM   #2
Technoslave
Member
 
Registered: Dec 2003
Location: Northern VA
Posts: 493

Rep: Reputation: 30
Only because I'm feeling nice today, I won't say you should 'man sed'

> more change-it.sh
#!/bin/sh

for i in `ls $.net`
do
sed -e 's/604800/666666' \
-e 's/384000/333333' $i > blah.tmp

mv blah.tmp $i
done

That'll do what you want for multiple files and multiple changes, I leave it as an exercise to the reader how to do what he wants with what he has to do.
 
Old 03-14-2005, 11:25 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
And how can I remove for example the line which contains test07 from the script using bash.. Is this possible to do that from the CLI? Any help would be great!!
Code:
grep -v test07 yourfile.txt
 
Old 03-14-2005, 11:26 AM   #4
Technoslave
Member
 
Registered: Dec 2003
Location: Northern VA
Posts: 493

Rep: Reputation: 30
Quote:
Originally posted by Hko
Code:
grep -v test07 yourfile.txt

This will make it so you won't see test07, it doesn't replace anything.

You can remove the line too using the above sed script, once again, left to the reader to figure out how.

Last edited by Technoslave; 03-14-2005 at 11:27 AM.
 
Old 03-14-2005, 11:27 AM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You could use sed -i option to edit file in place also, like
Code:
# replaces the XX for testXX to XX
sed -i -re 's/(testXX)(:.*)/\1:XX/' /home/file

# remove the line which contains testXX
sed -i -e 's/testXX.*//' /home/file

Last edited by keefaz; 03-14-2005 at 11:29 AM.
 
Old 03-14-2005, 11:29 AM   #6
gbonvehi
Senior Member
 
Registered: Jun 2004
Location: Argentina (SR, LP)
Distribution: Slackware
Posts: 3,145

Rep: Reputation: 53
[Beaten]

You can use sed (a simple very simple editor that uses pipelines, see man sed).
A replace example would be:
Code:
sed -e 's/replacethis/forthis/' inthisfile > tothisfile
To delete a line it's just simple:
Code:
sed -e '3d' inthisfile > tothisfile
That basically removes line 3, if you want to remove several lines at once you can separate them with a , like '3,5d'

Hope that helps.

PS: I know this is not directly bash, but it works very well in scripts
 
Old 03-14-2005, 11:31 AM   #7
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Rep: Reputation: 30
Bah, sed is too cryptic for my tastes. Give me a good Perl reg ex script anyday.
 
Old 03-14-2005, 11:31 AM   #8
Erhnam
Member
 
Registered: Jan 2005
Posts: 54

Original Poster
Rep: Reputation: 15
Thanks Thanks!

Only if you don't know on which line of the script the value is.. How can you delete it by knowing the value? like test01?
 
Old 03-14-2005, 03:53 PM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by Technoslave
This will make it so you won't see test07, it doesn't replace anything.
Well, that's true, but only a small detail as this can be fixed the usual way.
Code:
mv yourfile.txt yourfile.org
grep -v test07 yourfile.org >yourfile.txt
rm yourfile.org
 
Old 03-15-2005, 02:34 AM   #10
Erhnam
Member
 
Registered: Jan 2005
Posts: 54

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by keefaz
You could use sed -i option to edit file in place also, like
Code:
# remove the line which contains testXX
sed -i -e 's/testXX.*//' /home/file
Thanks Thanks! This worked for me!! Is there eny way to clean up the empty lines?? After using the remove command I got this (example):

testxx:00
testyy:11

testzz:22
 
Old 03-15-2005, 03:34 AM   #11
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Code:
sed -i -e 's/testXX.*//' -e '1,/^./ { /./!d}' /home/file
 
Old 03-15-2005, 04:45 AM   #12
Erhnam
Member
 
Registered: Jan 2005
Posts: 54

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by keefaz
Code:
sed -i -e 's/testXX.*//' -e '1,/^./ { /./!d}' /home/file
I'm sorry.. This is not working for me:

Code:
[root@server01 home]# cat /home/file
mhuser01:32
ut2004user01:14
ut2004user03:14
ut2004user00:16
bfuser01:14
bfvuser01:14
alluser01:12
[root@server01 home]# sed -i -e 's/ut2004user00.*//' -e '1,/^./ { /./!d}' /home/file
[root@server01 home]# cat /home/file
mhuser01:32
ut2004user01:14
ut2004user03:14

bfuser01:14
bfvuser01:14
alluser01:12
[root@server01 home]#
 
Old 03-15-2005, 07:25 AM   #13
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
My bad, I misread the sed man page
Here is a working solution (and simpler)
Code:
sed -i -e '/^ut2004user00.*/d' /home/file
 
  


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
removing lines from file script iluvatar Programming 9 08-20-2004 05:49 AM
Can't get lines of a file with a Bash script.. barisdemiray Programming 2 08-11-2004 12:42 PM
Removing lines from file Aylar Programming 2 04-22-2004 06:34 AM
[bash] remove lines from a file Drimo Programming 3 03-20-2004 11:16 AM
file editing in a bash script Harpune Programming 4 11-22-2002 11:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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