I need to keep only lines in txt file that contain "#" - grep

I need to keep only this lines in my text file, which contain symbol #.
I have something like this for example:
something #
anything else fxgbdfg
car #
325235363456356 # dfsjdbfkjfbfds
958395959 #
sdfsnfjkndsnc3r /
And I need this:
something #
car #
958395959 #
Can somebody tell me how to do that in GREP?

grep \#
Or grep \# filename if you want to use a file rather than stdin

Related

Grep for regular letters and special characters

I'm trying to isolate lines that contain the following: '[Homosapiens]' from a file.
My file looks something like this:
br
blabla
>blabldi[Homosapiens]
>skadlfjkl[Musmusculus]
I only want to isolate the third line.
I have tried the following:
grep -F '\*[Homosapiens]' mytext.txt
and
fgrep '\*[Homosapiens]' mytext.txt
but both are not working.
Can anyone solve this problem?
What about the following:
fgrep "[Homosapiens]" mytext.txt
Or:
grep "\[Homosapiens\]" mytext.txt
Two remarks:
grep (or whatever of its family members fgrep, egrep, ...) search for an entry inside a line of text, so there is no need to try to fit the whole line inside your grep expression.
The square brackets have a special meaning (grep [a-e] means a search for all letters from 'a' to 'e'). Using a backslash in front of a square bracket disables that feature and gives you the opportunity to look for a square bracket.

Change thing in the middle of the string notepad++

My file contains things like this:
http://example.com/main.do?y=yeay
http://example.com/main.do?y=hahahehe
http://example.com/main.do?d=wow
http://example.com/blah/blah/product.do?p=49302
etc...
I want to change them all like following.
http://example.com/main.do#y=yeay.html
http://example.com/main.do#y=hahahehe.html
http://example.com/main.do#d=wow.html
http://example.com/blah/blah/product.do#p=49302.html
These are the links in a html/do/asp files.
How can I change them? Thanks.
I can also use other programs not noteoad++ and i have both macOS and WIndows
THanks
Ctrl+H
Find what: https?\S+?\.do\K\?(\S+)
Replace with: #$1.html
UNCHECK Match case
CHECK Wrap around
CHECK Regular expression
UNCHECK . matches newline
Replace all
Explanation:
https? # http OR https
\S+? # 1 or more non spaces, not greedy
\. # a dot
do # literally "do"
\K # forget all we have seen until this position
\? # question mark
(\S+) # group 1, 1 or more non spaces
Replacement:
# # literally
$1 # content of group 1
.html # literally
Screenshot (before):
Screenshot (after):

assign array value to ENV var on .env file

I need to set an array of strings on my .env file and cant find information about the right syntax. Test for this takes quite a while so I wanted to save some time. Some of this options should work:
MY_ARRAY=[first_string, second_string]
MY_ARRAY=[first_string second_string]
MY_ARRAY=['first_string', 'second_string']
Can someone tell me which?
As far as I know dotenv does not allow setting anything except strings (and multiline strings). The parser syntax is:
LINE = /
\A
(?:export\s+)? # optional export
([\w\.]+) # key
(?:\s*=\s*|:\s+?) # separator
( # optional value begin
'(?:\'|[^'])*' # single quoted value
| # or
"(?:\"|[^"])*" # double quoted value
| # or
[^#\n]+ # unquoted value
)? # value end
(?:\s*\#.*)? # optional comment
\z
/x
The reason behind this is shell and OS support for setting other types of env variables is spotty.
You could use a separator such as commas or pipes (|) and split the string with ENV['FOO'].split('|'). But maybe what you are trying to do should be solved with an initializer which combines ENV vars.

difference of lines to file without diff tags

I just want to take the difference of two files and write them to another without patch tags like + or - or diff tags like > or <. I understand how patches work and how to use the following commands:
diff file1.txt file2.txt | grep ">" > difffile.txt
diff -u file1.txt file2.txt > difffile.patch
patch original.txt < difffile.patch
but when I open my difffile.txt from the first command, I get something like this:
> some line of text
> some other line of text
when what I reallly want is:
some line of text
some other line of text
I thought that maybe indexing the string like
${stringname:2}
would work, but I don't know how to use that with grep or how to index a grep string.
I'm actually parsing html and xml and just want the values differences in some file. I don't know how to do that.
If you just want to remove the first two characters of every line, cut is your friend:
cut -c3- file
Test
$ cat a
hello this is me
and this is you
$ cut -c3- a
llo this is me
d this is you

Using tr to change many files names

I am basically trying to replace all special characters in directory names and files names with a period. I am attempting to use tr, but I am very new and I do not want to mess up all of my music naming and picture naming. I am making the switch from windows to linux and trying to get everything in a nice formatted pattern. I have used tr semi successfully but I would like some pro help from you guys! Thanks in advance! I have looked at the tr man pages but I am just worried about really messing up 12 years of pictures and music file names! The two man characters I am trying to replace are " - " but the naming scheme I've used in windows has been by hand over the years and it varies, so I was hoping to go through everything and replace all cases of "-" or " - " manly but any fat fingering I have done over the years and put in something besides that patter would be great. I am thinking something like:
tr -cd [:alnum:] '.'
would this work?
My main goal is to turn something like
01 - Name Of Song (or any variation of special/punctuation characters)
into
01.Name.Of.Song
You don't want to use the d option since it just deletes the matched characters. And you may want to use the s (squeeze) option. Try this:
tr -cs '[:alnum:]' '.'
You can test it like this:
echo '01 - Name Of Song' | tr -cs '[:alnum:]' '.'
(Ignore the extra period at the end of the output. That's just the newline character and won't appear in filenames ... generally.)
But this is probably not of much use in your task. If you want to do a mass rename, you might use a little perl program like this:
#!/usr/bin/perl
$START_DIRECTORY = "Music";
$RENAME_DIRECTORIES = 1; # boolean (0 or 1)
sub procdir {
chdir $_[0];
my #files = <*>;
for my $file (#files) {
procdir($file) if (-d $file);
next if !$RENAME_DIRECTORIES;
my $oldname = $file;
if ($file =~ s/[^[:alnum:].]+/\./g) {
print "$oldname => $file\n";
# rename $oldname, $file; # may not rename directories(?)
}
}
chdir "..";
}
procdir($START_DIRECTORY);
Run it with the rename command commented out (as above) to test it. Uncomment the rename command to actually rename the files. Caveat emptor. There be dragons. Etc.

Resources