LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 05-12-2010, 03:25 PM   #1
ufmale
Member
 
Registered: Feb 2007
Posts: 386

Rep: Reputation: 30
advance sed to remove multile lines.


i have a txt file with couple of comment lines:

Number of title = !num!
#line1
#line2
#line3

I wrote a script with "sed" to replace !num! in this file, which
is very straightforward. However, based on the !num!,
i want to remove the number of "#" based on the !num! value.

is there an easy way to do that with "sed"; otherwise, i will have to write a script to loop through the file.
 
Old 05-12-2010, 04:22 PM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by ufmale View Post
i have a txt file with couple of comment lines:

Number of title = !num!
#line1
#line2
#line3

I wrote a script with "sed" to replace !num! in this file, which
is very straightforward. However, based on the !num!,
i want to remove the number of "#" based on the !num! value.

is there an easy way to do that with "sed"; otherwise, i will have to write a script to loop through the file.
I think this will be much easier and more practical in AWK.

I don't understand this !num! thing, though — is the literal string "!num!" actually in the file, or is it a number? And what do you mean by "replacing" it?
 
Old 05-12-2010, 04:24 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Post a sample of the data before and after the desired changes.
 
Old 05-12-2010, 10:18 PM   #4
ufmale
Member
 
Registered: Feb 2007
Posts: 386

Original Poster
Rep: Reputation: 30
sorry for unclear.. The desired result is that if i provide 2 to replace !num!,
then the result would be

Quote:
Number of title = 2
line1
line2
#line3
If i provide 3, then

Quote:
Number of title = 3
line1
line2
line3
 
Old 05-13-2010, 12:21 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
awk '/^Num/{cnt=$NF}cnt{sub(/^#/,"");print;cnt--}' file
 
Old 05-13-2010, 07:48 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Code:
BEGIN {
	rmlines = 0
}

{
	if ($NF == "!num!")
	{
		$NF = num
		rmlines = num
		print
	}
	else if ($0 ~ /\#.*/)
	{
		if (rmlines == 0)
		{
			print
		}
		else
		{
			rmlines--
		}
	}
	else
	{
		print
	}
}
 
Old 05-13-2010, 08:08 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@MTK358 - I cannot see where you remove the #?
 
Old 05-13-2010, 08:11 AM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Oh, I thought I should remove the whole line.

<comment deleted>

Last edited by MTK358; 05-14-2010 at 09:44 AM.
 
Old 05-14-2010, 09:44 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I think this should do it (untested):

Code:
BEGIN {
	rmlines = 0
}

{
	if ($NF == "!num!")
	{
		$NF = num
		rmlines = num
		print
	}
	else if ($0 ~ /\#.*/)
	{
		if (rmlines == 0)
		{
			print
		}
		else
		{
			print substr($0, 2, length($0))
			rmlines--
		}
	}
	else
	{
		print
	}
}
 
  


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
[SOLVED] Using sed to remove lines with duplicate ID's, but different endings... wapitismith Linux - Newbie 4 05-08-2010 12:30 PM
How to remove lines and parts of lines from python strings? golmschenk Programming 3 11-26-2009 11:29 PM
sed to remove specific lines in a file tekmann33 Linux - Newbie 3 05-21-2009 03:41 PM
Sed command to print matching lines and 2 lines above.. DX398 Programming 12 10-01-2008 08:25 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM

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

All times are GMT -5. The time now is 12:15 AM.

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