Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove all lines that start with #

Status
Not open for further replies.

Michaeljc70

Programmer
Mar 16, 2012
5
US
Let me start by saying I am a programmer, but don't know awk. I need to remove all lines from a txt file that start with #.

I searched high and low and tried about 6 different things, but none worked.

An example of what I tried was awk '!/#/' file.txt > newfile.txt

In all cases, the file didn't change from the original.

Preferably, I would like to keep the file name the same rather than create a new file.

Thanks
 
Hi

Michaeljc70 said:
An example of what I tried was awk '!/#/' file.txt > newfile.txt
Then your problem is not the lack of Awk knowledge, but regular expressions. In which case why not use [tt]index()[/tt] or [tt]substr()[/tt] instead ? They are similar to other languages' [tt]indexOf()[/tt] and [tt]substring()[/tt] functions.

So either
Code:
awk '[teal]![/teal][fuchsia]/^#/[/fuchsia]' file.txt > newfile.txt

[gray]# or[/gray]

awk '[b]index[/b][teal]([/teal][navy]$0[/navy][teal],[/teal][green][i]"#"[/i][/green][teal])!=[/teal][purple]1[/purple]' file.txt > newfile.txt

[gray]# or[/gray]

awk '[b]substr[/b][teal]([/teal][navy]$0[/navy][teal],[/teal][purple]1[/purple][teal],[/teal][purple]1[/purple][teal])!=[/teal][green][i]"#"[/i][/green]' file.txt > newfile.txt
If none works, give us details about the used Awk implementation and post a sample input.
Michaeljc70 said:
Preferably, I would like to keep the file name the same rather than create a new file.
Not possible. Awk is not able to do in-place editing. ( By the way, neither other tools can do it literally. Even Sed, Perl and Ruby only simulates it by creating a new file and renaming it later. )


Feherke.
 
I tried all 3 of those (the first one I had tried before) and all leave the file the same.

I am wondering if it is the implementation I am using. I am using MAWK for Windows.


As for the file, it is just m3u playlist. Some of my devices don't like the # tags.

I am looking to do this in bulk (like an a whole directory). Can I use wildcards?

Here is a sample of the file:

#EXTINF:294,Bitter Sweet; BitterSweet - Dirty Laundry (Skeewiff Remix)
\Bitter Sweet; BitterSweet - The Mating Game\Dirty Laundry (Skeewiff Remix).mp3
#EXTINF:194,Bitter Sweet; BitterSweet - Don't Forget to Breathe
\Bitter Sweet; BitterSweet - The Mating Game\Don't Forget to Breathe.mp3
#EXTINF:465,Bitter:Sweet; BitterSweet - Dirty Laundry (Morgan Page Rem
\Bitter-Sweet; BitterSweet - Promo Only Alternative Club Au\Dirty Laundry (Morgan Page Rem.mp3
#EXTINF:185,Bitter:Sweet; Log - The Bomb
\Bitter-Sweet; Log - Drama\The Bomb.mp3
 
Hi

Michaeljc70 said:
I am wondering if it is the implementation I am using. I am using MAWK for Windows.
How are you running it ? More exactly, where ? In cmd.exe some ugly side effects may appear from the interference between the cmd.exe and Awk syntax.

Personally I prefer to use CygWin. It provides a real GNU environment with Bash and Gawk and everything works as in Linux. Of course, as long as they run in the provided terminal.


Feherke.
 
I am running it from CMD.

I was trying not to install anything. That is why I liked mawk- it is just the .exe.

I guess I could try another variant and see if that works.

Using the > , seems to put a wrench in using wildcards, or is there a way around that? I suppose a batch file could loop through and call awk for each file.
 
Hi

Then better use a script file to avoid syntax conflicts :
Code:
[teal]![/teal][fuchsia]/^#/[/fuchsia]
Code:
awk -f script.awk file.txt > newfile.txt
In this case there is no way to use a wildcard. But you can put cmd.exe to run it in a loop. See thread205-1640995 for such examples.

By the way, after installing CygWin all this could be reduced to :
Code:
sed -i '/^#/d' *

Feherke.
 
I found a version of SED and used that. Worked and I can use wildcards. Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top