Remove The Lines Starting with a Character in Shell

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

  1. use ^ to anchor your pattern to the beginning of the line ;
  2. delete lines matching the pattern using sed and the d 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 the ORS 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



Leave a reply



Submit