user3584454
user3584454

Reputation: 37

using bash command to remove nextline,space

Is sed command capable of removing space,nextline?

I want to have the following output(see output below)

TESTING     The quick brown fox jumps over the lazy dog

            The quick brown fox jumps over the lazy dog


TESTING02   The quick brown fox jumps over the lazy dog

            The quick brown fox jumps over the lazy dog

TESTING03   The quick brown fox jumps over the lazy dog


            The quick brown fox jumps over the lazy dog


            The quick brown fox jumps over the lazy dog

The output should be the following

TESTING     The quick brown fox jumps over the lazy dog
            The quick brown fox jumps over the lazy dog

TESTING02   The quick brown fox jumps over the lazy dog
            The quick brown fox jumps over the lazy dog

TESTING03   The quick brown fox jumps over the lazy dog
            The quick brown fox jumps over the lazy dog
            The quick brown fox jumps over the lazy dog

Upvotes: 1

Views: 128

Answers (3)

potong
potong

Reputation: 58568

This might work for you (GNU sed):

sed '/\S/!d;1b;/TESTING/i\\' file

Delete any empty lines and insert one before a line containing TESTING unless it's the first.

Upvotes: 1

Jotne
Jotne

Reputation: 41460

If you like to try awk you can do:

awk '/TESTING/ && NR>1 {print ""} NF' file
TESTING     The quick brown fox jumps over the lazy dog
            The quick brown fox jumps over the lazy dog

TESTING02   The quick brown fox jumps over the lazy dog
            The quick brown fox jumps over the lazy dog

TESTING03   The quick brown fox jumps over the lazy dog
            The quick brown fox jumps over the lazy dog
            The quick brown fox jumps over the lazy dog

NF prevents it from printing blank line.
/TESTING/ && NR>1 {print ""} adds one blank line before TESTING line except for first line

Upvotes: 4

NeronLeVelu
NeronLeVelu

Reputation: 10039

for your structure

sed '/^[[:space:]]*$/d
1!{
   /^TESTING/ s//\
&/
  }' YourFile

remove "space line" and ad a new line before TESTING entry (but not for 1st line)

Upvotes: 3

Related Questions