Modified copies of the sed / awk string

I am trying to insert modified copies of strings after the original strings.

Here is my file:

random
N:John Doe
random
N:Jane Roe
random
random
N:Name Sirname
random

Here you need my file to look like this:

random
N:John Doe
FN:John Doe
random
N:Jane Roe
FN:Jane Roe
random
random
N:Name Sirname
FN:Name Sirname
random

Any ideas for this? Cannot seem to find the correct sed / awk combination ...

+4
source share
4 answers
sed -n ' p; s/^N:/FN:/p' original.txt

This gives:

random
N:John Doe
FN:John Doe
random
N:Jane Roe
FN:Jane Roe
random
random
N:Name Sirname
FN:Name Sirname
random

. sed -n, , . . , p, . , s/^N:/FN:/p, FN: N: , ( , N:), .

+4

:

sed -e 's/^\(N:.*\)$/\1\nF\1/g' yourfile.txt

, "N:". , \1\nF\1 ( , , "F", ).

, "" "N:".

+2

Ampersand inserts a copy of the full match on the left side into the right side of the sed expression. Thus, using a simple wildcard to place a new line and leading F between copies of your match is all that is needed. For example:

$ sed 's/^N:.*/&\nF&/' /tmp/corpus.txt 
random
N:John Doe
FN:John Doe
random
N:Jane Roe
FN:Jane Roe
random
random
N:Name Sirname
FN:Name Sirname
random
+2
source

Here is the solution awk:

awk '/N:/{print $0"\nF"$0;next}1' file

$ cat file
random
N:John Doe
random
N:Jane Roe
random
random
N:Name Sirname
random

$ awk '/N:/{print $0"\nF"$0;next}1' file
random
N:John Doe
FN:John Doe
random
N:Jane Roe
FN:Jane Roe
random
random
N:Name Sirname
FN:Name Sirname
random
+1
source

Source: https://habr.com/ru/post/1524800/


All Articles