LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 04:25 PM   #1
ufmale
Member
 
Registered: Feb 2007
Posts: 385

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, 05:22 PM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
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, 05:24 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

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

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, 01:21 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

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

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
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, 09:08 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

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

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

<comment deleted>

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

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
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


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


All times are GMT -5. The time now is 09:29 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration