LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-09-2010, 12:45 AM   #1
mech123
Member
 
Registered: Dec 2008
Posts: 36

Rep: Reputation: 16
delete multiple lines from file using shell script


ile content :-
<1>
<2>
<3>
<a>
<4>
<5>
</a>
<b>
<6>
<7>
<8>
</b>
<9>

Now whenever find <a> in file then delete from that line until </a> doesnt meet.
ouputFile contents:-
<1>
<2>
<3>
<b>
<6>
<7>
<8>
</b>
<9>
I started to write following script
cat "$1" | while read LINE
do
echo "$LINE"
match=$LINE
if ( [ $LINE = "<a>" ] )
then
echo "Match found"
fi
done

How can i do this,my inputFile contains 10000 lines.
 
Old 06-09-2010, 01:20 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,000
Blog Entries: 11

Rep: Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893
No need for a script, really.

Code:
$ cat del_reg 
<1>
<2>
<3>
<a>
<4>
<5>
</a>
<b>
<6>
<7>
<8>
</b>
<9>
$ sed '/<a>/,/<\/a>/d' del_reg 
<1>
<2>
<3>
<b>
<6>
<7>
<8>
</b>
<9>
If you add a "-i" as a parameter to sed it will do it in-line.



Cheers,
Tink

Last edited by Tinkster; 06-09-2010 at 01:22 AM.
 
1 members found this post helpful.
Old 06-09-2010, 02:03 AM   #3
mech123
Member
 
Registered: Dec 2008
Posts: 36

Original Poster
Rep: Reputation: 16
thanks for reply
actually my reqirement is like this
shell script command ./test.sh filename.txt 2
this "2" specify that whenever <a> comes second time in file then delete it upto </a> ,If it occur first time then dont delete it.
Inuptfile contents :
<1>
<2>
<3>
<a>
<4>
<5>
</a>
<b>
<6>
<7>
<8>
</b>
<a>
<10>
<11>
<12>
</a>
<9>

outputFile :
<1>
<2>
<3>
<a>
<4>
<5>
</a>
<b>
<6>
<7>
<8>
</b>
<9>

Code :
deletedACounter=$2
echo $deletedACounter
count=0
cat "$1" | while read LINE
do
echo "$LINE"
match=$LINE
if ( [ $LINE = "<a>" ] )
then
echo "Match found" #firstTime <a> matched.
count=`expr $count +1`

fi

done
 
Old 06-09-2010, 04:03 AM   #4
mech123
Member
 
Registered: Dec 2008
Posts: 36

Original Poster
Rep: Reputation: 16
waiting for reply
 
Old 06-09-2010, 05:04 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,648

Rep: Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961
How about:
Code:
#!/usr/bin/awk -f

/<a>/ && ++a == num{f=1}

!f{print}

f && /<\/a>/{f=0}
Submit as so:
Code:
./script -v num=2 input_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
How to delete lines and insert another line in some xml files using a shell script? luananeko Linux - Newbie 1 07-20-2009 10:53 PM
Need a bash shell script which will delete lines from file scjohnie Linux - Newbie 1 09-13-2008 09:51 PM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-21-2007 12:14 AM
Need to make multiple lines into one with no extra spacing with shell script. sunksullen Programming 10 05-10-2007 06:20 PM
UNIX shell script: split long command on multiple lines loopoo Linux - Newbie 2 10-23-2006 10:34 AM


All times are GMT -5. The time now is 08:19 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration