Delete all lines beginning with a # from a file
This can be done with a sed one-liner:
sed '/^#/d'
This says, "find all lines that start with # and delete them, leaving everything else."
Remove the lines starting with a character in shell
Use grep
with -E
option for regex (or egrep in short):
grep -E "^[0-9].*" file.txt
Bash - remove all lines beginning with 'P'
Explanation
- use
^
to anchor your pattern to the beginning of the line ; - delete lines matching the pattern using
sed
and thed
flag.
Solution #1
cat file.txt | sed '/^P/d'
Better solution
Use sed
-only:
sed '/^P/d' file.txt > new.txt
How to delete newline and character at the start of line using Bash?
With Perl.
perl -i -0777pe 's/\n"""/"/' new.txt
Output to new.txt
:
"AAA"AAA
"BBB"BBB
"CCC"CCC
How to delete from a text file, all lines that contain a specific string?
To remove the line and print the output to standard out:
sed '/pattern to match/d' ./infile
To directly modify the file – does not work with BSD sed:
sed -i '/pattern to match/d' ./infile
Same, but for BSD sed (Mac OS X and FreeBSD) – does not work with GNU sed:
sed -i '' '/pattern to match/d' ./infile
To directly modify the file (and create a backup) – works with BSD and GNU sed:
sed -i.bak '/pattern to match/d' ./infile
How do you delete all lines that begin with string in unix sh?
grep -v '^string' yourfile.txt > stripped.txt
Remove the lines starting with a character in shell, but preserve the negative values
You can try like this:
grep '^ *[-+]\?[0-9]\+' file.txt > out.txt
(Or)
grep -E '^ *[-+]?[0-9]+' file.txt > out.txt
How can i remove a line only if it is followed by a line that starts with the same character?
If the whole file follows that pattern (some number of lines starting with >
, of which you want only the last, followed by a single line that should always be printed), you could use something like this:
awk '/^>/ { latest=$0 } !/^>/ { if (latest) { print latest; latest="" } print }'
If the line starts with >
, then it is remembered (stored in the variable latest
) but not printed. If the line doesn't start with >
, then it is printed, but only after first printing whatever was most recently stored in latest
.
The conditional means each printed >
line will appear only once, even if there are multiple non->
lines in a row. Since that doesn't happen in your sample data, you may not need the complication, and could use this simpler unconditional version:
awk '/^>/ { latest=$0 } !/^>/ { print latest; print }'
remove newline character after line starting with pattern
You can use the ORS
output record sparator to do that very easily as
awk '$1 !~ /PATTERN/{ORS="\n"} $1 ~ /PATTERN/{ORS=" "} 1'
Example
$ awk '$1 !~ /PATTERN/{ORS="\n"} $1 ~ /PATTERN/{ORS=" "} 1' input
One
PATTERN bla bla bla Three
Four
Or more simply like,
awk 'ORS = $1 ~ /PATTERN/ ? " " : "\n"'
Example
$ awk 'ORS = $1 ~ /PATTERN/ ? " " : "\n"' input
One
PATTERN bla bla bla Three
Four
What it does?
ORS = $1 ~ /PATTERN/ ? " " : "\n"
Sets theORS
to space or newline depending on the pattern match.Now expression always evaluates to true, in which case
awk
will print the entire input record.
Related Topics
Case Statement in a While Loop, Shell Scripting
Does Routing Affect a Socket with a Bound Source Address
Fastest Way to Determine User Permissions in /Etc/Sudoer
How to Get Argv from "Struct Linux_Binprm"
Karma Not Found from Maven After Re-Install
How to Automatically Close The Execution of The 'Qemu' After End of Process
How to Use Xdotool to Enter a Web Console Command
Eclipse-Mars on Linux: Black Background Color in Tooltip's
How to See Output to Stdout After Disown and Logout
Gitlab-Ci Alpine Image: Host Key Verification Failed
Counting Lines Starting with a Certain Word
Would Gcc 4.8 and 4.7 Peacefully Coexist on The Same Machine
Can't Hard Link The Gitconfig File
Sorting in Fortran, Undefined Reference to Qsort_
Grep,How to Search for Exact Pattern